2

This code should compare two fitnesses, use the best one to find the solution and then it uses the best one in the next iteration. However the problem I get is that it is just using the newest fitness regardless of whether it is bigger or smaller. Can anyone help me spot if there are any mistakes in my code, thanks!

This was a little tricky to explain, so if anyone needs more clarification please ask and I'll post up my entire project, though I believe that the error has something to do with this small section of code:

public static ScalesSolution RMHC(ArrayList<Double> weights, int n, int iter) {
    ScalesSolution sol = new ScalesSolution(n);
    ScalesSolution oldSol = new ScalesSolution(sol.GetSol());
    for (int i = 0; i < iter; i++) {
        System.out.println("Iteration number: " + i);
        System.out.println("Old Solution : ");
        oldSol.println();
        double f = oldSol.ScalesFitness(weights);
        System.out.println("Old Fitness: ");
        System.out.println(f);
        // the new solution after copying the string from scalesolution
        sol.SmallChange();
        System.out.println("New Solution : ");
        sol.println();
        double f1 = sol.ScalesFitness(weights);
        System.out.println("New Fitness: ");
        System.out.println(f1);
        if (oldSol.ScalesFitness(weights) > sol.ScalesFitness(weights)) {               
            oldSol = new ScalesSolution(sol.GetSol());
        }
    }
    return (oldSol);
}

Here is SmallChange:

public void SmallChange() {
    int n = scasol.length();
    Random rand = new Random();
    int p = (rand.nextInt(n));
    String x;

    x = scasol.substring(0, p);
    if (scasol.charAt(p) == '0') {
        x += '1';
    } else {
        x += '0';
    }
    x += scasol.substring(p + 1, n);
    scasol = x;
}

Here is ScalesFitness and ScalesSolution:

public ScalesSolution(int n) {
    scasol = RandomBinaryString(n);
}

// This is the fitness function for the Scales problem
// This function returns -1 if the number of weights is less than the size of the current solution 
// Exercise 3

public static double ScalesFitness(ArrayList<Double> weights) {
    int n = scasol.length();                // Assigns the length of scasol to n
    double lhs = 0.0;                       //  Initialises lhs to 0.0, type double
    double rhs = 0.0;                       //  Initialises rhs to 0.0, type double
    if (n > weights.size())                 // If statement, compares n and weight size
        return (-1);                        // Returns -1 when the if statement is true

    // Code goes here
    for (int i = 0; i < n; i++) {           // For loop which goes from i=0 to n
        if (scasol.charAt(i) == '0') {      // If statement which checks if the character at position i is equal to a 0
            lhs += weights.get(i);          // Adds weight at position i to lhs
        } else {                            // If the character in position i is not a 0 do the following
            rhs += weights.get(i);          // Adds the weight at position i to rhs
        }
    }
    return (Math.abs(lhs - rhs));           // Calculates the absolute value of lhs-rhs and returns the value
}
JimmyK
  • 4,801
  • 8
  • 35
  • 47
  • But this solely depends on sol.SmallChange() right? Is this monotonically increasing function ? – sundar Jan 11 '13 at 12:17
  • @sundar `SmalChange` isn't even a function---the return value is ignored. – Marko Topolnik Jan 11 '13 at 12:23
  • @MarkoTopolnik But this may mutate the state of `sol` object which may impact `ScalesFitness` result right? – sundar Jan 11 '13 at 12:25
  • Right, so the function in question is `ScalesFitness`, but "monotonically increasing" is a property of unary functions only, so your question is meaningless in the world of fitness maximization. – Marko Topolnik Jan 11 '13 at 12:27
  • I added the SmallChange method so you guys can take a look at it, in the output it shows the old and new fitness/solution so I think SmallChange works? – JimmyK Jan 11 '13 at 12:27
  • 1
    @JimmyK could you plz share ScalesFitness method as well and the corresponding ScalesSolution constructor? – sundar Jan 11 '13 at 12:29
  • Added ScalesFitness and ScalesSolution top my post – JimmyK Jan 11 '13 at 12:32
  • @JimmyK Your `SmallChange` found to increase the size of `scasol` by 1 in new object. Whereas for old and new `ScalesSolution` corresponding `ScalesFitness` method, we are passing same `weights` (i.e length is going to be same). In that case, `n > weights.size()` will be always true for new objects and hence retuns -1 for new. This becomes trivial check in `oldSol.ScalesFitness(weights) > sol.ScalesFitness(weights)` – sundar Jan 11 '13 at 12:50
  • @MarkoTopolnik I m not sure about fitness maximization problem but responding based on the code published in the question. – sundar Jan 11 '13 at 12:52
  • @sundar do you mean to say that because n>weights.size() always returns -1 that the code will always return the newest fitness solution? – JimmyK Jan 11 '13 at 12:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22584/discussion-between-sundar-and-jimmyk) – sundar Jan 11 '13 at 12:58

0 Answers0