//package public static int fitness(int[] bin_rep) { return (int)

//package geneticAlgo;import java.util.Comparator;import java.util.Arrays;import java.lang.Math;import java.

util.Random;import java.util.

Best services for writing your paper according to Trustpilot

Premium Partner
From $18.00 per page
4,8 / 5
4,80
Writers Experience
4,80
Delivery
4,90
Support
4,70
Price
Recommended Service
From $13.90 per page
4,6 / 5
4,70
Writers Experience
4,70
Delivery
4,60
Support
4,60
Price
From $20.00 per page
4,5 / 5
4,80
Writers Experience
4,50
Delivery
4,40
Support
4,10
Price
* All Partners were chosen among 50+ writing services by our Customer Satisfaction Team

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

pow(2, i);        }        return int_rep;    }    // mutates each element of the binary representation with probability p    public static int mutate(int bin_rep, double p)    {        int cutoff = (int) (1e6 * p);        Random rand = new Random();        for (int i = 0; i < bin_rep.length; i++)        {            // mutate if random generator below cutoff            if (rand.nextInt((int) 1e6) < cutoff)            {                bin_repi = 1-bin_repi;            }        }        return bin_rep;    }    // perform single point crossover on 2 parents.    public static int crossover(int parent1, int parent2)    {        int child1 = new intparent1.

length;        int child2 = new intparent2.length;        // determine single crossover point after which we swap elements in parents for children        Random rand = new Random();        int crossPoint = rand.nextInt(parent1.length);        // old values        System.

arraycopy(parent1, 0, child1, 0, crossPoint);        System.arraycopy(parent2, 0, child2, 0, crossPoint);        // crossover values        System.arraycopy(parent2, crossPoint, child1, crossPoint, parent2.length-crossPoint);        System.arraycopy(parent1, crossPoint, child2, crossPoint, parent1.length-crossPoint);        int children = new int {child1, child2};        return children;    }    // return fitness of solution for problem 1    public static int fitness(int bin_rep)    {        return (int) Math.pow(bin_to_int(bin_rep), 2);    }    // return fitness of solution for problem 2    public static int fitness2(int bin_rep){        return (int) ((-1) * Math.

pow(bin_to_int(bin_rep) – 9, 2));    }    public static double avg(int children) {    // return average of members of population        int tot = 0;        for (int child : children)        {            tot += bin_to_int(child);        }        return ((float) tot)/children.length;    }    // return average fitness of population    public static double avg_fitness(int children)    {        int tot_fitness = 0;        for (int child : children)        {            tot_fitness += fitness2(child);        }        return ((float) tot_fitness)/children.length;    }    // print out population members, binary representation, and fitness    public static void print_population(int population)    {        for (int member : population) {            System.out.println(“Member: ” + bin_to_int(member) + ” Representation: ” + Arrays.toString(member) +                    ” fitness: ” + fitness(member));        }    }    // print out populatio members, binary representation, and fitness for problem 2    public static void print_population2(int population)    {        for (int member : population) {            System.

out.println(“Member: ” + bin_to_int(member) + ” Representation: ” + Arrays.toString(member) +                    ” fitness: ” + fitness2(member));        }    }    // Genetic algorithm for first problem    public static int problem1()    {        //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_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();    }}