Site Loader

//package geneticAlgo;import java.util.Comparator;import java.util.Arrays;import java.lang.Math;import java.util.Random;import java.util.ArrayList;import java.util.Collections;public class Main {    // returns decimal representation of binary matrix. Note: binary matrix starts with lowest power of 2    public static int bin_to_int(int bin_rep)    {        int int_rep = 0;        for (int i=0; i parents = new ArrayList();        int parents = new int105;        for (int j = 0; j < 8; j++)        {            Random rand = new Random();            int parent = new int5;            for (int k = 0; k < parent.length; k++)            {                parentk = rand.nextInt(2);            }            parentsj = parent;        }        System.out.println("Parents");        print_population(parents);        double average = avg(parents);        System.out.println("Average: " + average);        // sort population by fitness and take 5 fittest individuals        Arrays.sort(parents, (a, b) -> {return (fitness(b)-fitness(a));});        int new_parents = Arrays.copyOfRange(parents, 0, 5);        int generation = 1;        while (average < 29) {            System.out.println("New parents (generation " + generation + "): ");            print_population(new_parents);            int count = 0;            // generate children from crossover            int children = new int205;            for (int j = 0; j < new_parents.length; j++) {                for (int k = j + 1; k < new_parents.length; k++) {                    int generated_children = crossover(new_parentsj, new_parentsk);                    if (generation == 1)                    {                        System.out.println("Crossover. Parent 1: " + Arrays.toString(new_parentsj) +                        " Parent 2: " + Arrays.toString(new_parentsk) + " -> ” +                        “Child 1: ” + Arrays.toString(generated_children0) +                        ” Child2: ” + Arrays.toString(generated_children1));                    }                    childrencount = mutate(generated_children0, .05);                    System.out.println(“Mutation. Generated child: ” + Arrays.toString(generated_children0) +                            ” -> Actual child: ” + Arrays.toString(childrencount));                    count++;                    childrencount = mutate(generated_children1, .05);                    System.out.println(“Mutation. Generated child: ” + Arrays.toString(generated_children1) +                            ” -> Actual child: ” + Arrays.toString(childrencount));                    count++;                }            }            System.out.println(“Children: “);            print_population(children);            average = avg(children);            System.out.println(“Average: ” + average);            Arrays.sort(children, (a, b) -> {                return fitness(b) – fitness(a);            });            new_parents = Arrays.copyOfRange(children, 0, 5);            generation++;        }        int soln = new_parents0;        System.out.println(“Best solution: ” + bin_to_int(soln) + “. Binary rep: ” + Arrays.toString(soln) + ” fitness: ” + fitness(soln));        return bin_to_int(soln);    }    public static int problem2() {        //ArrayList parents = new ArrayList();        int parents = new int105;        for (int j = 0; j < 8; j++) {            Random rand = new Random();            int parent = new int5;            for (int k = 0; k < parent.length; k++) {                parentk = rand.nextInt(2);            }            parentsj = parent;        }        System.out.println("Parents");        print_population2(parents);        double average = avg_fitness(parents);        System.out.println("Average fitness: " + average);        // sort population by fitness and take 5 fittest individuals        Arrays.sort(parents, (a, b) -> {            return (fitness2(b) – fitness2(a));        });        int new_parents = Arrays.copyOfRange(parents, 0, 5);        int generation = 1;        while (average < -10) {            System.out.println("New parents (generation " + generation + "): ");            print_population2(new_parents);            int count = 0;            // generate children from crossover            int children = new int205;            for (int j = 0; j < new_parents.length; j++) {                for (int k = j + 1; k < new_parents.length; k++) {                    int generated_children = crossover(new_parentsj, new_parentsk);                    if (generation == 1) {                        System.out.println("Crossover. Parent 1: " + Arrays.toString(new_parentsj) +                                " Parent 2: " + Arrays.toString(new_parentsk) + " -> ” +                                “Child 1: ” + Arrays.toString(generated_children0) +                                ” Child2: ” + Arrays.toString(generated_children1));                    }                    childrencount = mutate(generated_children0, .05);                    System.out.println(“Mutation. Generated child: ” + Arrays.toString(generated_children0) +                            ” -> Actual child: ” + Arrays.toString(childrencount));                    count++;                    childrencount = mutate(generated_children1, .05);                    System.out.println(“Mutation. Generated child: ” + Arrays.toString(generated_children1) +                            ” -> Actual child: ” + Arrays.toString(childrencount));                    count++;                }            }            System.out.println(“Children: “);            print_population2(children);            average = avg_fitness(children);            System.out.println(“Average fitness: ” + average);            Arrays.sort(children, (a, b) -> {                return fitness2(b) – fitness2(a);            });            new_parents = Arrays.copyOfRange(children, 0, 5);            generation++;        }        int soln = new_parents0;        System.out.println(“Best solution: ” + bin_to_int(soln) + “. Binary rep: ” + Arrays.toString(soln) + ” fitness: ” + fitness(soln));        return bin_to_int(soln);    }    public static void main(String args) {        int soln2 = problem2();    }}

Post Author: admin

x

Hi!
I'm Sonya!

Would you like to get a custom essay? How about receiving a customized one?

Check it out