001    package org.jaga.reproduction.proteinLocation;
002    
003    import org.jaga.reproduction.XOver;
004    import org.jaga.individualRepresentation.proteinLocation.*;
005    import java.util.Arrays;
006    import org.jaga.definitions.*;
007    
008    /**
009     * TODO: Complete these comments.
010     *
011     * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
012     *
013     * <p><u>Company:</u> University College London and JAGA.Org
014     *    (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
015     * </p>
016     *
017     * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
018     *    This program is free software; you can redistribute it and/or modify
019     *    it under the terms of the GNU General Public License as published by
020     *    the Free Software Foundation, ONLY if you include a note of the original
021     *    author(s) in any redistributed/modified copy.<br/>
022     *    This program is distributed in the hope that it will be useful,
023     *    but WITHOUT ANY WARRANTY; without even the implied warranty of
024     *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025     *    GNU General Public License for more details.<br/>
026     *    You should have received a copy of the GNU General Public License
027     *    along with this program; if not, write to the Free Software
028     *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
029     *    or see http://www.gnu.org/licenses/gpl.html</p>
030     *
031     * @author Greg Paperin (greg@jaga.org)
032     *
033     * @version JAGA public release 1.0 beta
034     */
035    
036    public class PolypeptidePatternXOver extends XOver {
037    
038            private int xOverPoints = 1;
039    
040            public PolypeptidePatternXOver() {
041            }
042    
043            public PolypeptidePatternXOver(double xOverProb) {
044                    super(xOverProb);
045            }
046    
047            public PolypeptidePatternXOver(int xOverPoints) {
048                    super();
049                    this.xOverPoints = xOverPoints;
050            }
051    
052            public PolypeptidePatternXOver(int xOverPoints, double xOverProb) {
053                    super(xOverProb);
054                    this.xOverPoints = xOverPoints;
055            }
056    
057            public Individual[] reproduce(Individual[] parents, GAParameterSet params) {
058    
059                    // copy parents:
060                    ProteinLocationClassifierFactory factory = (ProteinLocationClassifierFactory) params.getIndividualsFactory();
061                    Individual [] kids = new Individual [] {
062                                                             factory.createSpecificIndividual(parents[0], params),
063                                                             factory.createSpecificIndividual(parents[1], params)};
064    
065                    // check if XOver happens:
066                    RandomGenerator rnd = params.getRandomGenerator();
067                    if (rnd.nextDouble() < getXOverProbability())
068                            return kids;
069    
070                    // ger the patterns:
071                    PolypeptidePattern [] parP = new PolypeptidePattern [] {
072                                                                             ((ProteinLocationClassifier) kids[0]).getPattern(),
073                                                                             ((ProteinLocationClassifier) kids[1]).getPattern(),
074                                                                    };
075                    int [] lens = new int [] {parP[0].getLength(), parP[1].getLength()};
076                    PolypeptidePattern [] kidP = new PolypeptidePattern [] {
077                                                                             new PolypeptidePattern(), new PolypeptidePattern()
078                                                                    };
079    
080                    // set the xOver points:
081                    int xPointCount = Math.min(lens[0] - 1, lens[1] - 1);
082                    xPointCount = Math.min(xPointCount, this.xOverPoints);
083                    if (1 > xPointCount)
084                            return kids;
085                    int [][] xPoints = new int[2][xPointCount + 1];
086                    for (int i = 0; i < 2; i++)
087                            for (int j = 0; j <= xPointCount; xPoints[i][j++] = -1);
088                    //System.err.println();
089                    for (int i = 0; i < 2; i++) {
090                            for (int j = 0; j < xPointCount; j++) {
091                                    int xOver = rnd.nextInt(1, lens[i]);
092                                    while (contains(xPoints[i], xOver))
093                                            xOver = rnd.nextInt(1, lens[i]);
094    
095                                    //System.err.println(i + " / " + j + " / " + xOver +" / "+ lens[i]);
096                                    xPoints[i][j] = xOver;
097                            }
098                            xPoints[i][xPointCount] = lens[i];
099                            Arrays.sort(xPoints[i]);
100                            //for (int x = 0; x < xPoints[i].length; System.err.print(" " + xPoints[i][x++]));
101                            //System.err.println();
102                    }
103    
104                    // do the xOver:
105                    int tmp;
106                    int [] src = new int [] {0, 1};
107                    int [] srcInd = new int [] {0, 0};
108                    for (int p = 0; p <= xPointCount; p++) {
109                            for (int k = 0; k < 2; k++) {
110                                    for (; srcInd[src[k]] < xPoints[src[k]][p]; srcInd[src[k]]++)
111                                            kidP[k].insertItem(parP[src[k]].getItem(srcInd[src[k]]));
112                            }
113                            tmp = src[0];
114                            src[0] = src[1];
115                            src[1] = tmp;
116                            /*
117                            tmp = srcInd[0];
118                            srcInd[0] = srcInd[1];
119                            srcInd[1] = tmp;
120                            */
121                    }
122    
123    
124                    if (kidP[0].getLength() <= factory.getMaxPatternLength()
125                                    && kidP[1].getLength() <= factory.getMaxPatternLength()) {
126                            for (int i = 0; i < 2; i++)
127                                    ((ProteinLocationClassifier) kids[i]).setPattern(kidP[i]);
128                    }
129    
130                    return kids;
131            }
132    
133            private boolean contains(int [] array, int positiveNum) {
134                    for (int i = 0; array[i] >= 0 && i < array.length; i++)
135                            if (array[i] == positiveNum)
136                                    return true;
137                    return false;
138            }
139    
140            public Class getApplicableClass() {
141                    return ProteinLocationClassifier.class;
142            }
143    
144            public int getXOverPoints() {
145                    return xOverPoints;
146            }
147    
148            public void setXOverPoints(int xOverPoints) {
149                    this.xOverPoints = xOverPoints;
150            }
151    }