Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: what's wrong with my code

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what's wrong with my code

    public class gabinary
    {
    public static int POPSIZE =20; /* population size */
    public static int MAXGENS= 1000; /* max. number of generations */
    public static int NVARS =10; /* no. of problem variables */
    public static float PXOVER= 0.8f; /* probability of crossover */
    public static float PMUTATION =0.75f; /* probability of mutation */
    public static int TRUE =1;
    public static int FALSE =0;

    public static int generation; /* current generation no. */
    public static int cur_best;
    public static genotype population[]= new genotype[POPSIZE+1];
    genotype newpopulation[]= new genotype[POPSIZE+1];
    public gabinary()
    {

    }

    public void initialize()
    {
    int i, j;
    /* Initialise variables */
    for (j=1; j < POPSIZE; j++)

    for (i=0; i < NVARS; i++) {
    System.out.println( "\n popsize \n"+POPSIZE);
    for (j=1; j < POPSIZE; j++) {
    population[j].fitness = 0;
    population[j].rfitness = 0;
    population[j].cfitness = 0;
    population[j].gene[i] = randval();
    }
    }

    }

    /************************************************** *******************/
    /* */
    /* Random value generator: Generates a random binary digit */
    /* */
    /************************************************** *******************/
    public float randval()
    {
    Random randg = new Random();
    float rand = randg.nextFloat(100);
    return (rand) ;
    }
    /************************************************** *******************/
    /* */
    /* Evaluate function: This takes a user defined function. */
    /* Each time this is changed, the code has to be recompiled. */
    /* The current function is : a simple 'sum of bits'. */
    /* */
    /************************************************** *******************/
    public void evaluate ()
    {
    int mem;
    int i;

    for (mem=0; mem < POPSIZE; mem++) {
    population[mem].fitness = 0;

    for (i=0; i < NVARS; i++)
    population[mem].fitness += population[mem].gene[i];
    }
    }

    /************************************************** *******************/
    /* */
    /* Keep_the_best function: This function keeps track of the */
    /* best member of the population. Note that the last entry in */
    /* the array Population holds a copy of the best individual */
    /* */
    /************************************************** *******************/

    public void keep_the_best()
    {
    int mem;
    int i;
    cur_best = 0; /* stores the index of the best individual */

    for (mem=0; mem < POPSIZE; mem++) {
    if (population[mem].fitness > population[POPSIZE].fitness) {
    cur_best = mem;
    population[POPSIZE].fitness = population[mem].fitness;
    }
    }

    /* Once the best member in the population has been found, copy its genes */
    for (i=0; i < NVARS; i++)
    population[POPSIZE].gene[i] = population[cur_best].gene[i];

    }

    /************************************************** *******************/
    /* */
    /* Elitist function: The best member of the previous generation */
    /* is stored as the last in the array. If the best member of */
    /* the current generation is worse then the best member of the */
    /* previous generation, the latter would replace the worst */
    /* member of the current population. */
    /* */
    /************************************************** *******************/

    public void elitist()
    {
    int i;
    float best, worst; /* best and worst fitness values */
    int best_mem=0, worst_mem=0; /* indexes of the best and worst member */

    best = population[0].fitness;
    worst = population[0].fitness;

    for (i=0; i < POPSIZE-1; ++i) {

    if (population[i].fitness > population[i+1].fitness) {

    if (population[i].fitness >= best) {
    best = population[i].fitness;
    best_mem = i;
    }

    if (population[i+1].fitness <= worst) {
    worst = population[i+1].fitness;
    worst_mem = i + 1;
    }
    }
    else {

    if (population[i].fitness <= worst) {
    worst = population[i].fitness;
    worst_mem = i;
    }

    if (population[i+1].fitness >= best) {
    best = population[i+1].fitness;
    best_mem = i + 1;
    }

    }
    }

    /* if the best individual from the new population is better than */
    /* the best individual from the previous population, then */
    /* copy the best from the new population; else replace the */
    /* worst individual from the current population with the */
    /* best one from the previous generation. */

    if (best >= population[POPSIZE].fitness) {

    for (i=0; i < NVARS; i++) {
    population[POPSIZE].gene[i] = population[best_mem].gene[i];
    }

    population[POPSIZE].fitness = population[best_mem].fitness;
    }
    else {

    for (i=0; i < NVARS; i++) {
    population[worst_mem].gene[i] = population[POPSIZE].gene[i];
    }

    population[worst_mem].fitness = population[POPSIZE].fitness;

    }
    }


    /************************************************** *******************/
    /* */
    /* Selection function: A standard proportional (i.e. roulette */
    /* wheel) selection for maximization problems incorporating the */
    /* elitist model - makes sure that the best member survives. */
    /* */
    /************************************************** *******************/

    public void select()
    {
    int mem, i, j, k;
    float sum = 0;
    float p;

    /* Find total fitness of population */
    for (mem=0; mem < POPSIZE; mem++) {
    sum += population[mem].fitness;
    }

    /* Calculate relative fitness */
    for (mem=0; mem < POPSIZE; mem++) {
    population[mem].rfitness = population[mem].fitness / sum;
    }
    population[0].cfitness = population[0].rfitness;

    /* Calculate cumulative fitness */
    for (mem=1; mem < POPSIZE; mem++) {
    population[mem].cfitness = population[mem-1].cfitness +
    population[mem].rfitness ;
    }

    /* finally, select survivors using cumulative fitness */
    for (i=0; i < POPSIZE; i++) {
    Random rand=new Random();
    float f=rand.nextFloat();

    // p = rand() % 1000 / 1000.0;
    p = f;
    if (p < population[0].cfitness)
    newpopulation[i] = population[0];

    else {

    for (j=0; j < POPSIZE; j++)
    if (p >= population[j].cfitness && p < population[j+1].cfitness)
    newpopulation[i] = population[j+1];
    }
    }

    /* Once a new population is created, copy it back */
    for (i=0; i < POPSIZE; i++)
    population[i] = newpopulation[i];


    }

    /************************************************** *******************/
    /* */
    /* Crossover selection: selects 2 parents that take part in */
    /* the crossover. */
    /* */
    /************************************************** *******************/

    public void crossover()
    {
    int i, mem, one=0;
    int first = 0; /* count of the number of members chosen */
    float x;

    for (mem=0; mem < POPSIZE; ++mem) {

    Random rand=new Random();
    x=rand.nextFloat();

    // x = rand() % 1000 / 1000.0;

    if (x < PXOVER) {
    ++first;
    if (first % 2 == 0)
    Xover (one, mem);
    else
    one = mem;
    }
    }
    }


    /************************************************** ***********************/
    /* */
    /* Crossover: performs uniform crossover of the two selected parents. */
    /* */
    /************************************************** ***********************/

    public void Xover (int one, int two)
    {
    int i;

    for (i = 0; i < NVARS; i++) {
    if (randval() == 1)
    swap (population[one].gene[i], population[two].gene[i]);

    }
    }

    /************************************************** *******************/
    /* */
    /* Swap: A simple swap procedure that helps in swapping 2 variables */
    /* */
    /************************************************** *******************/

    public void swap (float x, float y)
    {
    float temp;

    temp = x;
    x = y;
    y = temp;
    }


    /************************************************** *******************/
    /* */
    /* Mutation: Random uniform mutation. A variable selected for */
    /* mutation is replaced by a an inverted binary digit. */
    /* */
    /************************************************** *******************/

    public void mutate()
    {
    int i, j;
    float lbound, hbound;
    float x;

    for (i=0; i < POPSIZE; i++) {
    for (j=0; j < NVARS; j++) {
    Random rand=new Random();
    x=rand.nextFloat();
    // x = rand() % 1000 / 1000.0;

    if (x < PMUTATION) {

    /* Perform Mutation */
    if (population[i].gene[j] == 1)
    population[i].gene[j] = 0;
    else
    population[i].gene[j] = 1;
    }
    }
    }
    }



    /************************************************** *******************/
    /* */
    /* Report function: Reports progress of the simulation. Data */
    /* dumped into the output file are separated by commas */
    /* */
    /************************************************** *******************/

    public void report()
    {
    int i;
    float best_val; /* best population fitness */
    float avg; /* avg population fitness */
    float stddev; /* std. deviation of population fitness */
    float sum_square; /* sum of square for std. calc */
    float square_sum; /* square of sum for std. calc */
    float sum; /* total population fitness */

    sum = 0.0f;
    sum_square = 0.0f;

    for (i=0; i < POPSIZE; i++) {
    sum += population[i].fitness;
    sum_square += population[i].fitness * population[i].fitness;
    }

    avg = sum / (float) POPSIZE;
    square_sum = avg * avg * (float) POPSIZE;
    //stddev = Math.sqrt((float)((sum_square - square_sum) / (POPSIZE - 1)));

    stddev = ((sum_square - square_sum) / (POPSIZE - 1));


    best_val = population[POPSIZE].fitness;

    System.out.println( generation+" "+best_val+" "+ avg+" "+ stddev);
    }



    /************************************************** *******************/
    /* */
    /* Main function: Each generation involves selecting the best */
    /* members, performing crossover and mutation and then */
    /* evaluating the resulting population, until the terminating */
    /* condition is satisfied. */
    /* */
    /************************************************** *******************/

    public static void main(String[] args)
    {
    int i;

    int generation = 0;

    gabinary g=new gabinary();
    System.out.println (" generation best average standard ");
    System.out.println(" number value fitness deviation ");

    g.initialize();
    g.evaluate();
    g.keep_the_best();

    while (generation < MAXGENS) {
    generation++;
    g.select();
    g.crossover();
    g.mutate();
    g.report();
    g.evaluate();
    g.elitist();
    }


    System.out.println(" Simulation completed");
    System.out.println( " Best Member: ");

    for (i=0; i < NVARS; i++) {
    System.out.println( i+ " "+ population[POPSIZE].gene[i]);
    }


    System.out.println(" Best fitness "+ population[POPSIZE].fitness);


    System.out.println("Success!");
    }

    }
    /************************************************** **************************/


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    411
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: what's wrong with my code

    Please use code tags
     
    when posting code.

    What problems are you having? Are you getting an output?

    A few things:
    gabinary g=new gabinary();
    That is meaningless - your constructor is empty and as it stands your
    instances are being initilized by the default constructor.

    NB: C-style comments /* */ do not need to be writen like you have done.
    It is much nicer to encase large comments in a single pair of the delimiters.
    If you want to use single line comments use //

    Wishes Ada xx
    Last edited by Ada Lovelace; July 1st, 2014 at 06:04 AM. Reason: Typo
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,310
    My Mood
    Mellow
    Thanks
    253
    Thanked 780 Times in 765 Posts

    Default Re: what's wrong with my code

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please ask a question, post errors you want help with, and/or let us know what you need help with. We typically won't debug your code for you, but we will help you with insights and techniques that will help you debug your own code.

Similar Threads

  1. What is wrong with my code
    By samapple789 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 16th, 2014, 11:04 AM
  2. What's wrong with my code?
    By techflyer in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 13th, 2013, 03:54 PM
  3. What is wrong with my code?
    By docthoi in forum Java Theory & Questions
    Replies: 13
    Last Post: February 3rd, 2013, 05:33 PM
  4. What is wrong with my code?
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 28th, 2013, 05:44 PM
  5. need help .. what is wrong with my code.
    By baig-sh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 10th, 2011, 07:28 PM

Tags for this Thread