001    package org.jaga.fitnessEvaluation.multiplexer;
002    
003    
004    /**
005     * TODO: Complete these comments.
006     *
007     * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
008     *
009     * <p><u>Company:</u> University College London and JAGA.Org
010     *    (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
011     * </p>
012     *
013     * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
014     *    This program is free software; you can redistribute it and/or modify
015     *    it under the terms of the GNU General Public License as published by
016     *    the Free Software Foundation, ONLY if you include a note of the original
017     *    author(s) in any redistributed/modified copy.<br/>
018     *    This program is distributed in the hope that it will be useful,
019     *    but WITHOUT ANY WARRANTY; without even the implied warranty of
020     *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
021     *    GNU General Public License for more details.<br/>
022     *    You should have received a copy of the GNU General Public License
023     *    along with this program; if not, write to the Free Software
024     *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
025     *    or see http://www.gnu.org/licenses/gpl.html</p>
026     *
027     * @author Greg Paperin (greg@jaga.org)
028     *
029     * @version JAGA public release 1.0 beta
030     */
031    
032    public class Multiplexer {
033    
034            private int dataLines = 4;
035            private int ctrlLines = 2;
036            private int totalLines = 6;
037    
038            public Multiplexer() {}
039    
040            public Multiplexer(int dataLines) {
041                    this.dataLines = dataLines;
042                    this.ctrlLines = calculateCtrlLines();
043                    this.totalLines = (this.dataLines + this.ctrlLines);
044            }
045    
046            private int calculateCtrlLines() {
047                    double log = Math.log(dataLines) / Math.log(2); // i.e. log(2, dL)
048                    if (((int) log) == log)
049                            return (int) log;
050                    else
051                            throw new IllegalArgumentException("Multiplexer must contain 2^n data-lines, "
052                                                                                               + "where n - Integer. " + dataLines
053                                                                                               + " data-lines does not satisfy this condition");
054            }
055    
056            // Input: [cN, ..., c1, c0, d0, d1, d2, ..., dN]
057            public boolean evaluate(boolean [] input) {
058                    if (input.length != totalLines) {
059                            throw new IllegalArgumentException("Input has " + input.length
060                                                                                               + " values, but it must contain "
061                                                                                               + totalLines
062                                                                                               + " values (" + ctrlLines
063                                                                                               + " control lines + "
064                                                                                               + dataLines + " data lines)");
065                    }
066                    int factor = 1;
067                    int line = 0;
068                    for (int i = ctrlLines - 1; i >= 0; i--, factor <<= 1) {
069                            if (input[i])
070                                    line += factor;
071                    }
072                    boolean value = input[ctrlLines + line];
073                    return value;
074            }
075    
076            public int getDataLines() {
077                    return dataLines;
078            }
079    
080            public int getCtrlLines() {
081                    return ctrlLines;
082            }
083            public int getTotalLines() {
084                    return totalLines;
085            }
086    
087            /*
088            //TEST:
089            public static void main(String[] args) {
090                    Multiplexer multi = new Multiplexer(4);
091                    System.out.println("[0, 0, 1, 0, 0, 0] => "
092                                                       + multi.evaluate(new boolean[] {false, false, true, false, false, false}));
093                    System.out.println("[0, 1, 1, 0, 0, 0] => "
094                                                       + multi.evaluate(new boolean[] {false, true, true, false, false, false}));
095                    System.out.println("[1, 1, 0, 0, 0, 1] => "
096                                                       + multi.evaluate(new boolean[] {true, true, false, false, false, true}));
097            }
098            */
099    
100    }