Results 1 to 16 of 16
  1. #1
    V1C3M4N is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default Advanced extends and implements

    I have two classes, GeneticAlgorithm and GeneticAlgorithmDemo, I can post the source if anyone wants but suffice to say, GeneticAlgorithm defines several methods and several abstract methods. GeneticAlgorithmDemo extends GeneticAlgorithm and of course provides implementations of the abstract methods in GeneticAlgorithm.

    GeneticAlgorithm defines a two dimensional array of Objects, i also have an interface called GAObject which demands implementation of some methods.

    Because I dont know what types of Objects I want to use in the two dimensional array in the subclass I cant define what type of object (other than object itself) populates the array in the superclass, I know that i want the contents of the array in the subclass to be free to define its own superclass but i do want the objects (whatever they may be) to be forced to implement the GAObject interface.

    Here is what I ask, is there a way to force a subclass to define a variable (an array) which overrides the corresponding variable in the superclass AND that whatever populates that array (a custom defined object that is free to extend whatever it wants, but is forced to implement the interface GAObject)

    Any ideas???

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    First off, no. You can't force a subclass to hide (it won't override, it'll hid) a variable fo the parent. If you find yourself needing that then your model is wrong.

    Why not simply make the parent array an array of GAObjects?
    GAObject[][]?

  3. #3
    V1C3M4N is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    but I dont what to make a superclass called GAObject i want it to be an interface so that the class that will be in the array can freely inherit whatever it wants.

    Will declaring GAObject[][] array; in the superclass
    And declaring array = new GAInteger[][]; in the subclass
    mean that GAInteger is allowed even though it doesnt EXTEND GAObject but IMPLEMENT it

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    I didn't say make it a superclass.
    You can simply define your array as GAObject[][], which will only allow objects that implement the GAObject interface. That's (part of) what interfaces are for.

    No need to initialise the array as an array of GAInteger[][]. Leave the initialisation to the parent (new GAObject[x][y]).

  5. #5
    V1C3M4N is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    Well how can I provide an implementation of a class that imlements GAObject and use that, because im just going to have no method bodies for methods that need invoking

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Java Code:
    setArrayValue(1,2,new GAInteger(3));
    
    ...where setArrayValue is in the GeneticAlgorithm class...
    
    public (or protected) void setArrayValue(int x, int y, GAObject ga) {
        array[x][y] = ga;
    }
    Like that.
    Of course, not knowing what it is you're doing there may well (and probably are) better ways to do this.

  7. #7
    V1C3M4N is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    But like i said I dont want the genetic algorithm class to know what objects are going to be stored in the array from the subclass of the genetic algorithm your approach requires that the method setArrayValue takes a parameter of GAInteger but i dont want any instance of GAInteger to be initialised in the superclass, only placed in the array of the superclass within the subclass. that inherits the array

    The array does need to be defined (but prob not initialised) in the superclass because it is referenced in the non-abstract methods there. It does absolutely need to be initialised in the subclass.

  8. #8
    V1C3M4N is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    package geneticAlgorithm;

    import java.util.Arrays;
    import java.util.LinkedList;

    /**
    * A genetic algorithm is a method of artificial intelligence that derives its behaviour from three metaphors of evolution, these are
    * reproduction, crossover and mutation. Genetic algorithms are used when it is accepted that a perfect solution may never be reached
    * or may not exist, instead one of the many good solutions are acceptable
    *
    * A genetic algorithm starts with many randomly generated potential solutions, the fitness of each of these solutions, that is their
    * closeness to the perfect solution is determined and the best half are used to construct more potential solutions. The best potential
    * solutions are combined to give the same number of potential solutions as there was before the best were selected, the combining
    * itself ensures that elements are taken from multiple potential solutions to give subsequent solutions, it is not known whether each
    * individual element is better than others available. The subsequent potential solutions are intended to be better than the previous
    * but this is not guaranteed. The subsequent potential solutions then have an element of mutation applied to them, combining many
    * potential solutions in many different configurations may work, but only if elements required for a good solution existed in the
    * first iteration, mutation adds an element of change after combining, one or more elements in one or more potential solutions can be
    * randomly altered with the intention that some of these alterations will better the potential solution
    *
    * This process executes iteratively until a potential solution is found that exceeds a predetermined performance threshold or until no
    * further improvements can be made
    *
    * @author V1C3M4N
    */
    public abstract class GeneticAlgorithm
    {
    public double vFitnessGoal; // The minimum required performance level

    public GAObject[][] vArrays; // The current potential solutions

    public double[] vFitness; // The fitness score for each potential solution

    public LinkedList <Double> vFitnessHistory; // The history of the best fitness scores

    /**
    * Creates the first iteration of potential solutions, the inheriting class must implement a randomise method for the type of data
    * being computed
    */
    public abstract void randomise();

    /**
    * Executes the iterations of the algorithm until a solution is found
    */
    public void solve()
    {
    while(true) // Until the problem is solved
    {
    fitness();

    if(solved() || unsolvable()) // If the problem is solved
    {
    break;
    }

    reproduce();

    mutate();
    }
    }

    /**
    * Determines the fitness of each of the current potential solutions, the inheriting class must implement a fitness method for the
    * type of data being computed
    */
    public abstract void fitness();

    /**
    * Determines if any of the current potential solutions have exceeded a performance threshold
    *
    * @return True if one of the arrays has solved the problem
    */
    public boolean solved()
    {
    for(int count = 0; count < vFitness.length; count++) // For each of the arrays of elements
    {
    if(vFitness[count] > vFitnessGoal) // If the fitness score is above the specified threshold
    {
    System.out.println(vFitness[count] + "\t" + Arrays.toString(vArrays[count]));

    return true;
    }
    }

    return false;
    }

    /**
    * Determines whether the problem is solvable, based upon the progression of the genetic algorithm, the history of the highest
    * fitness scores of the last one-hundred generations of potential solutions are analysed to determine if the genetic algorithm
    * has stagnated
    *
    * @return True if the problem is unsolvable
    */
    public boolean unsolvable()
    {
    double highest = 0;
    int high = 0;

    for(int count = 0; count < vFitness.length; count++) // For each of the fitness scores
    {
    if(vFitness[count] > highest) // If this fitness score is higher than the record for this generation
    {
    highest = vFitness[count];
    high = count;
    }
    }

    vFitnessHistory.add(highest);

    if(vFitnessHistory.size() > 100) // If the number of fitness scores recorded is more than one-hundred
    {
    vFitnessHistory.removeFirst();
    }

    double meanFirst = 0;
    double meanSecond = 0;

    for(int count = 0; count < vFitnessHistory.size(); count++) // For each of the previous best fitness scores
    {
    if(count < vFitnessHistory.size() / 2) // For the first half of the previous fitness scores
    {
    meanFirst = meanFirst + vFitnessHistory.get(count);
    }
    else
    {
    meanSecond = meanSecond + vFitnessHistory.get(count);
    }
    }

    meanFirst = meanFirst / (vFitnessHistory.size() / 2);
    meanSecond = meanSecond / (vFitnessHistory.size() - (vFitnessHistory.size() / 2));

    // If the average of the first half of the fitness scores is higher than the second half with some allowed variation
    if(meanFirst > meanSecond + (meanFirst / 10))
    {
    System.out.println(vFitness[high] + "\t" + Arrays.toString(vArrays[high]));

    return true;
    }

    return false;
    }

    /**
    * Extracts the highest fitting current potential solutions as defined in the <code>vFitness</code> array by the
    * <code>fitness()</code> method and passes them to the <code>crossover(Object[][] pArrays)</code> method.
    */
    public void reproduce()
    {
    Object[][] arrays = new Object[vArrays.length / 2][];

    for(int countA = 0; countA < vArrays.length / 2; countA++) // For half the number of current potential solutions
    {
    double highest = 0;
    int high = 0;

    for(int countB = 0; countB < vArrays.length; countB++) // For each of the current potential solutions
    {
    if(vFitness[countB] > highest) // If the current fitness score is higher than the recorded
    {
    highest = vFitness[countB];
    high = countB;
    }
    }

    arrays[countA] = vArrays[high];
    vFitness[high] = 0;
    }

    crossover(arrays);
    }

    /**
    * Combines the potential solutions that are passed as a parameter into further potential solutions
    *
    * @param pArrays The potential solutions to be combined
    */
    public void crossover(Object[][] pArrays)
    {
    // For each of the best current potential solutions, increasing by two
    for(int countX = 0; countX < pArrays.length; countX = countX + 2)
    {
    for(int countY = 0; countY < pArrays[countX].length; countY++) // For each of the elements in the potential solution
    {
    if(countY < pArrays[countX].length / 2) // For the first half of the elements
    {
    vArrays[(countX * 2) + 0][countY] = ((GAObject) pArrays[countX + 0][countY]).deepClone();
    vArrays[(countX * 2) + 1][countY] = ((GAObject) pArrays[countX + 1][countY]).deepClone();
    }
    else
    {
    vArrays[(countX * 2) + 0][countY] = ((GAObject) pArrays[countX + 1][countY]).deepClone();
    vArrays[(countX * 2) + 1][countY] = ((GAObject) pArrays[countX + 0][countY]).deepClone();
    }

    if(countY % 2 == 0) // If the element position is even
    {
    vArrays[(countX * 2) + 2][countY] = ((GAObject) pArrays[countX + 0][countY]).deepClone();
    vArrays[(countX * 2) + 3][countY] = ((GAObject) pArrays[countX + 1][countY]).deepClone();
    }
    else
    {
    vArrays[(countX * 2) + 2][countY] = ((GAObject) pArrays[countX + 1][countY]).deepClone();
    vArrays[(countX * 2) + 3][countY] = ((GAObject) pArrays[countX + 0][countY]).deepClone();
    }
    }
    }
    }

    /**
    * Performs mutations on the data, the inheriting class must implement a mutate method for the type of data being computed
    */
    public abstract void mutate();
    }

    abstract interface GAObject
    {
    public abstract GAObject deepClone();

    public abstract void randomise();

    public abstract void mutate();

    public abstract String toString();
    }

  9. #9
    V1C3M4N is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    package geneticAlgorithm;

    import java.util.LinkedList;

    import com.V1C3M4N.miscTools.Cloner;

    /**
    * Demonstration of the genetic algorithm. Fixed size arrays of integers are randomly constructed, the goal is to use the genetic
    * algorithm so that the total of the random numbers is higher than a predetermined value. For example, if eight arrays of ten integers
    * within the range zero to ten are randomly generated, then the total of each array would be on average fifty, it would be unlikely
    * that the total would be more than ninety. The genetic algorithm aims to create, from combining and mutating the generated arrays, a
    * solution where the total is more than ninety.
    *
    * @author V1C3M4N
    */
    public class GeneticAlgorithmDemo extends GeneticAlgorithm
    {
    /**
    * Main method creates an object of the genetic algorithm demo and proceeds with attempting to find a solution
    *
    * @param args
    */
    public static void main(String[] args)
    {
    GeneticAlgorithmDemo gad = new GeneticAlgorithmDemo();

    gad.randomise();

    gad.solve();
    }

    /**
    * Constructor initialises the arrays
    */
    public GeneticAlgorithmDemo()
    {
    vFitnessGoal = 90;

    vArrays = new GAInteger[100][10];

    vFitness = new double[100];

    vFitnessHistory = new LinkedList <Double>();
    }

    /**
    * Randomises the elements of the array into integers between zero and ten
    */
    public void randomise()
    {
    for(int countX = 0; countX < vArrays.length; countX++) // For each of the arrays of elements
    {
    for(int countY = 0; countY < vArrays[countX].length; countY++) // For each of the elements
    {
    ((GAInteger) vArrays[countX][countY]).randomise();
    }
    }
    }

    /**
    * Determines the fitness of each array, the fitness here is how high the total of each array is
    */
    public void fitness()
    {
    for(int countX = 0; countX < vArrays.length; countX++) // For each of the arrays of elements
    {
    double total = 0;

    for(int countY = 0; countY < vArrays[countX].length; countY++) // For each of the elements
    {
    total = total + ((GAInteger) vArrays[countX][countY]).getValue();
    }

    vFitness[countX] = total;
    }
    }

    /**
    * Performs a mutation on each of the arrays, that is only one element in each array is changed to another random integer
    */
    public void mutate()
    {
    for(int count = 0; count < vArrays.length; count++) // For each of the arrays of elements
    {
    ((GAInteger) vArrays[count][(int) (Math.random() * vArrays[count].length)]).mutate();
    }
    }
    }

    class GAInteger implements GAObject
    {
    private int vMin = 0; // The minimum of the range for the values in the potential solutions
    private int vMax = 10; // The maximum of the range for the values in the potential solutions

    private int vValue;

    public GAInteger()
    {

    }

    public GAObject deepClone()
    {
    return (GAObject) Cloner.deepCloneBySerialisation(this);
    }

    public void randomise()
    {
    vValue = (int) ((Math.random() * vMax) + vMin) + 1;
    }

    public void mutate()
    {
    vValue = (int) ((Math.random() * vMax) + vMin) + 1;
    }

    public int getValue()
    {
    return vValue;
    }
    }

  10. #10
    V1C3M4N is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    I sevearly doubt you'll know what the hell my code is trying to do at this moment but it immediately runs into an error at

    ((GAInteger) vArrays[countX][countY]).randomise();

    in GeneticAlgorithmDemo

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Quote Originally Posted by V1C3M4N View Post
    But like i said I dont want the genetic algorithm class to know what objects are going to be stored in the array from the subclass of the genetic algorithm your approach requires that the method setArrayValue takes a parameter of GAInteger but i dont want any instance of GAInteger to be initialised in the superclass, only placed in the array of the superclass within the subclass. that inherits the array
    No it doesn't. It takes a parameter of GAObject. The call contained a GAInteger. That call would be in the subclass (or wherever, doesn't really matter).

    Quote Originally Posted by V1C3M4N View Post
    The array does need to be defined (but prob not initialised) in the superclass because it is referenced in the non-abstract methods there. It does absolutely need to be initialised in the subclass.
    And that array is defined in the parent. It does not need to be initialised in the subclasses if all it contains are things that implement GAObject.

    This is basic OO stuff.

    Java Code:
    public abstract class MyAbstract {
        public static interface GAObject {
            // lots of method definitions in here
        }
        private GAObject[][] array;
        protected MyAbstract(int x, int y) {
            // Made up some initialisation here, just to fill in the example.
            array = new GAObject[x][y];
        }
        protected void setArrayValue(int x, int y, GAObject ga) {
            array[x][y] = ga;
        }
        public void doSomething();
    }
    public class MyClass extends MyAbstract {
        public static class GAInteger implements GAObject {
            // implement all the methods here.
        }
        public MyClass() {
            super(1,2);
        }
        public void doSomething() {
            setArrayValue(0,1,new GAInteger());
        }
    }
    As for the code you wrote, Can you stick code tags around it, because unformatted stuff is hard to read.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Quote Originally Posted by V1C3M4N View Post
    I sevearly doubt you'll know what the hell my code is trying to do at this moment but it immediately runs into an error at

    ((GAInteger) vArrays[countX][countY]).randomise();

    in GeneticAlgorithmDemo
    What error?
    And you do not need to cast to GAInteger, since GAObject has a randomise method. Which is what I've been trying to say all along.

  13. #13
    V1C3M4N is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    package geneticAlgorithm;

    import java.util.Arrays;
    import java.util.LinkedList;

    /**
    * A genetic algorithm is a method of artificial intelligence that derives its behaviour from three metaphors of evolution, these are
    * reproduction, crossover and mutation. Genetic algorithms are used when it is accepted that a perfect solution may never be reached
    * or may not exist, instead one of the many good solutions are acceptable
    *
    * A genetic algorithm starts with many randomly generated potential solutions, the fitness of each of these solutions, that is their
    * closeness to the perfect solution is determined and the best half are used to construct more potential solutions. The best potential
    * solutions are combined to give the same number of potential solutions as there was before the best were selected, the combining
    * itself ensures that elements are taken from multiple potential solutions to give subsequent solutions, it is not known whether each
    * individual element is better than others available. The subsequent potential solutions are intended to be better than the previous
    * but this is not guaranteed. The subsequent potential solutions then have an element of mutation applied to them, combining many
    * potential solutions in many different configurations may work, but only if elements required for a good solution existed in the
    * first iteration, mutation adds an element of change after combining, one or more elements in one or more potential solutions can be
    * randomly altered with the intention that some of these alterations will better the potential solution
    *
    * This process executes iteratively until a potential solution is found that exceeds a predetermined performance threshold or until no
    * further improvements can be made
    *
    * @author V1C3M4N
    */
    public abstract class GeneticAlgorithm
    {
    public double vFitnessGoal; // The minimum required performance level

    public GAInterface[][] vArrays; // The current potential solutions

    public double[] vFitness; // The fitness score for each potential solution

    public LinkedList <Double> vFitnessHistory; // The history of the best fitness scores

    /**
    * Creates the first iteration of potential solutions, the inheriting class must implement a randomise method for the type of data
    * being computed
    */
    public abstract void randomise();

    /**
    * Executes the iterations of the algorithm until a solution is found
    */
    public void solve()
    {
    while(true) // Until the problem is solved
    {
    fitness();

    if(solved() || unsolvable()) // If the problem is solved
    {
    break;
    }

    reproduce();

    mutate();
    }
    }

    /**
    * Determines the fitness of each of the current potential solutions, the inheriting class must implement a fitness method for the
    * type of data being computed
    */
    public abstract void fitness();

    /**
    * Determines if any of the current potential solutions have exceeded a performance threshold
    *
    * @return True if one of the arrays has solved the problem
    */
    public boolean solved()
    {
    for(int count = 0; count < vFitness.length; count++) // For each of the arrays of elements
    {
    if(vFitness[count] > vFitnessGoal) // If the fitness score is above the specified threshold
    {
    System.out.println(vFitness[count] + "\t" + Arrays.toString(vArrays[count]));

    return true;
    }
    }

    return false;
    }

    /**
    * Determines whether the problem is solvable, based upon the progression of the genetic algorithm, the history of the highest
    * fitness scores of the last one-hundred generations of potential solutions are analysed to determine if the genetic algorithm
    * has stagnated
    *
    * @return True if the problem is unsolvable
    */
    public boolean unsolvable()
    {
    double highest = 0;
    int high = 0;

    for(int count = 0; count < vFitness.length; count++) // For each of the fitness scores
    {
    if(vFitness[count] > highest) // If this fitness score is higher than the record for this generation
    {
    highest = vFitness[count];
    high = count;
    }
    }

    vFitnessHistory.add(highest);

    if(vFitnessHistory.size() > 100) // If the number of fitness scores recorded is more than one-hundred
    {
    vFitnessHistory.removeFirst();
    }

    double meanFirst = 0;
    double meanSecond = 0;

    for(int count = 0; count < vFitnessHistory.size(); count++) // For each of the previous best fitness scores
    {
    if(count < vFitnessHistory.size() / 2) // For the first half of the previous fitness scores
    {
    meanFirst = meanFirst + vFitnessHistory.get(count);
    }
    else
    {
    meanSecond = meanSecond + vFitnessHistory.get(count);
    }
    }

    meanFirst = meanFirst / (vFitnessHistory.size() / 2);
    meanSecond = meanSecond / (vFitnessHistory.size() - (vFitnessHistory.size() / 2));

    // If the average of the first half of the fitness scores is higher than the second half with some allowed variation
    if(meanFirst > meanSecond + (meanFirst / 10))
    {
    System.out.println(vFitness[high] + "\t" + Arrays.toString(vArrays[high]));

    return true;
    }

    return false;
    }

    /**
    * Extracts the highest fitting current potential solutions as defined in the <code>vFitness</code> array by the
    * <code>fitness()</code> method and passes them to the <code>crossover(Object[][] pArrays)</code> method.
    */
    public void reproduce()
    {
    GAInterface[][] arrays = new GAInterface[vArrays.length / 2][];

    for(int countA = 0; countA < vArrays.length / 2; countA++) // For half the number of current potential solutions
    {
    double highest = 0;
    int high = 0;

    for(int countB = 0; countB < vArrays.length; countB++) // For each of the current potential solutions
    {
    if(vFitness[countB] > highest) // If the current fitness score is higher than the recorded
    {
    highest = vFitness[countB];
    high = countB;
    }
    }

    arrays[countA] = vArrays[high];
    vFitness[high] = 0;
    }

    crossover(arrays);
    }

    /**
    * Combines the potential solutions that are passed as a parameter into further potential solutions
    *
    * @param pArrays The potential solutions to be combined
    */
    public void crossover(GAInterface[][] pArrays)
    {
    // For each of the best current potential solutions, increasing by two
    for(int countX = 0; countX < pArrays.length; countX = countX + 2)
    {
    for(int countY = 0; countY < pArrays[countX].length; countY++) // For each of the elements in the potential solution
    {
    if(countY < pArrays[countX].length / 2) // For the first half of the elements
    {
    vArrays[(countX * 2) + 0][countY] = (GAInterface) pArrays[countX + 0][countY].deepClone();
    vArrays[(countX * 2) + 1][countY] = (GAInterface) pArrays[countX + 1][countY].deepClone();
    }
    else
    {
    vArrays[(countX * 2) + 0][countY] = (GAInterface) pArrays[countX + 1][countY].deepClone();
    vArrays[(countX * 2) + 1][countY] = (GAInterface) pArrays[countX + 0][countY].deepClone();
    }

    if(countY % 2 == 0) // If the element position is even
    {
    vArrays[(countX * 2) + 2][countY] = (GAInterface) pArrays[countX + 0][countY].deepClone();
    vArrays[(countX * 2) + 3][countY] = (GAInterface) pArrays[countX + 1][countY].deepClone();
    }
    else
    {
    vArrays[(countX * 2) + 2][countY] = (GAInterface) pArrays[countX + 1][countY].deepClone();
    vArrays[(countX * 2) + 3][countY] = (GAInterface) pArrays[countX + 0][countY].deepClone();
    }
    }
    }
    }

    /**
    * Performs mutations on the data, the inheriting class must implement a mutate method for the type of data being computed
    */
    public abstract void mutate();
    }

    abstract interface GAInterface
    {
    public abstract Object deepClone();

    public abstract void randomise();

    public abstract void mutate();

    public abstract String toString();
    }

  14. #14
    V1C3M4N is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    package geneticAlgorithm;

    import java.io.Serializable;
    import java.util.LinkedList;

    import com.V1C3M4N.miscTools.Cloner;

    /**
    * Demonstration of the genetic algorithm. Fixed size arrays of integers are randomly constructed, the goal is to use the genetic
    * algorithm so that the total of the random numbers is higher than a predetermined value. For example, if eight arrays of ten integers
    * within the range zero to ten are randomly generated, then the total of each array would be on average fifty, it would be unlikely
    * that the total would be more than ninety. The genetic algorithm aims to create, from combining and mutating the generated arrays, a
    * solution where the total is more than ninety.
    *
    * @author V1C3M4N
    */
    public class GeneticAlgorithmDemo extends GeneticAlgorithm implements Serializable
    {
    private static final long serialVersionUID = 1L;

    /**
    * Main method creates an object of the genetic algorithm demo and proceeds with attempting to find a solution
    *
    * @param args
    */
    public static void main(String[] args)
    {
    GeneticAlgorithmDemo gad = new GeneticAlgorithmDemo();

    gad.randomise();

    gad.solve();
    }

    /**
    * Constructor initialises the arrays
    */
    public GeneticAlgorithmDemo()
    {
    vFitnessGoal = 90;

    vArrays = new GAInteger[100][10];

    vFitness = new double[100];

    vFitnessHistory = new LinkedList <Double>();
    }

    /**
    * Randomises the elements of the array into integers between zero and ten
    */
    public void randomise()
    {
    for(int countX = 0; countX < vArrays.length; countX++) // For each of the arrays of elements
    {
    for(int countY = 0; countY < vArrays[countX].length; countY++) // For each of the elements
    {
    vArrays[countX][countY] = new GAInteger();
    vArrays[countX][countY].randomise();
    }
    }
    }

    /**
    * Determines the fitness of each array, the fitness here is how high the total of each array is
    */
    public void fitness()
    {
    for(int countX = 0; countX < vArrays.length; countX++) // For each of the arrays of elements
    {
    double total = 0;

    for(int countY = 0; countY < vArrays[countX].length; countY++) // For each of the elements
    {
    total = total + ((GAInteger) vArrays[countX][countY]).getValue();
    }

    vFitness[countX] = total;
    }
    }

    /**
    * Performs a mutation on each of the arrays, that is only one element in each array is changed to another random integer
    */
    public void mutate()
    {
    for(int count = 0; count < vArrays.length; count++) // For each of the arrays of elements
    {
    ((GAInteger) vArrays[count][(int) (Math.random() * vArrays[count].length)]).mutate();
    }
    }
    }

    class GAInteger implements GAInterface, Serializable
    {
    private static final long serialVersionUID = 1L;

    private int vMin = 0; // The minimum of the range for the values in the potential solutions
    private int vMax = 10; // The maximum of the range for the values in the potential solutions

    private int vValue;

    public Object deepClone()
    {
    return Cloner.deepCloneBySerialisation(this);
    }

    public void randomise()
    {
    vValue = (int) ((Math.random() * vMax) + vMin) + 1;
    }

    public void mutate()
    {
    vValue = (int) ((Math.random() * vMax) + vMin) + 1;
    }

    public int getValue()
    {
    return vValue;
    }

    public String toString()
    {
    return Integer.toString(vValue);
    }
    }

  15. #15
    V1C3M4N is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    Nevermind, changed a few castings, cant remember which ones but now it works.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    This cast here:
    ((GAInteger) vArrays[count][(int) (Math.random() * vArrays[count].length)]).mutate();
    is the only one left I think.
    You've done the randomise ones.
    :)

Similar Threads

  1. A good project in Advanced JAVA
    By vedarthk in forum Advanced Java
    Replies: 16
    Last Post: 08-08-2010, 08:21 PM
  2. Advanced Looping
    By nuggetman4 in forum New To Java
    Replies: 1
    Last Post: 05-10-2010, 06:54 AM
  3. Java Advanced Imaging -> RGB values
    By PHANTOMIAS in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2010, 01:43 PM
  4. Arrays Problem (Advanced Java...Need Help)
    By Zebra in forum New To Java
    Replies: 9
    Last Post: 05-02-2008, 01:26 AM
  5. Implements MyClass extends JFrame
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 03:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •