Skip to content
Snippets Groups Projects
Candidate.java 2.72 KiB
Newer Older
  • Learn to ignore specific revisions
  • istkabra's avatar
    istkabra committed
    package MainClasses;
    
    
    istkabra's avatar
    istkabra committed
    import java.util.ArrayList;
    
    import java.util.Arrays;
    
    istkabra's avatar
    istkabra committed
    
    
    istkabra's avatar
    istkabra committed
    public class Candidate {
    
    istkabra's avatar
    istkabra committed
    
        int[] isHydrophobic;       // 0 = no | 1 = yes
        int[] outgoingDirection;   // 0 = North | 1 = East | 2 = South | 3 = West
    
    istkabra's avatar
    istkabra committed
        public ArrayList<Vertex> vertexList;
        public double fitness;
    
    istkabra's avatar
    istkabra committed
    
    
    istkabra's avatar
    istkabra committed
        public Candidate(int[] isH, int[] oD) {
    
    istkabra's avatar
    istkabra committed
            this.isHydrophobic = isH;
            this.outgoingDirection = oD;
            this.vertexList = constructVertexes();
    
    
            this.fitness = -1d; // Not calculated yet
    
    istkabra's avatar
    istkabra committed
        }
    
    
    istkabra's avatar
    istkabra committed
        private ArrayList<Vertex> constructVertexes() {
            ArrayList<Vertex> vertexList = new ArrayList<>();
    
    istkabra's avatar
    istkabra committed
            int currentX = 0;
            int currentY = 0;
    
            for (int currentVertex = 0; currentVertex < isHydrophobic.length; currentVertex++) {
                vertexList.add(new Vertex(currentX, currentY,
                        isHydrophobic[currentVertex] == 1, outgoingDirection[currentVertex]));
    
                // Update position
                if (outgoingDirection[currentVertex] == 0) {
                    currentY++;
                } else if (outgoingDirection[currentVertex] == 1) {
                    currentX++;
                } else if (outgoingDirection[currentVertex] == 2) {
                    currentY--;
                } else if (outgoingDirection[currentVertex] == 3) {
                    currentX--;
                }
            }
    
            return vertexList;
        }
    
    
    istkabra's avatar
    istkabra committed
        public ArrayList<Vertex> getVertexList() {
            return vertexList;
        }
    
    
        public void mutateDir(int mutationPlace, int mutation) {
            outgoingDirection[mutationPlace] = mutation;
            this.vertexList = constructVertexes();
        }
    
    
    istkabra's avatar
    istkabra committed
        public void crossover(Candidate partner, int crossoverPlace) {
    
            // Save this gene for a moment while skipping the part that stays
            int[] originalDirections = new int[outgoingDirection.length];
            for (int i = crossoverPlace; i < outgoingDirection.length; i++) {
                originalDirections[i] = outgoingDirection[i];
            }
    
            // Edit these directions
            for (int i = crossoverPlace; i < outgoingDirection.length; i++) {
                outgoingDirection[i] = partner.outgoingDirection[i];
            }
    
            // Edit partners directions
            for (int i = crossoverPlace; i < outgoingDirection.length; i++) {
                partner.outgoingDirection[i] = originalDirections[i];
            }
    
        }
    
    
        @Override
        public String toString() {
            return "Canidate{" +
                    "outgoingDirection=" + Arrays.toString(outgoingDirection) +
                    '}';
        }
    
        public int[] getOutgoing() {
            int[] newOut = new int[this.outgoingDirection.length];
    
            for(int i = 0; i < this.outgoingDirection.length; i++) {
                newOut[i] = this.outgoingDirection[i];
            }
    
            return newOut;
        }
    
    istkabra's avatar
    istkabra committed
    }