3

I have written some code in Java which tests the fitness of two solutions.

I am looking to then compare the two solutions and keep the one which has the best fitness and to discard the other.

For example:

if(fitness1 < fitness2)
    keep fitness1 and discard/ignore fitness2
else
    keep fitness2 and discard/ignore fitness1

How would I go about achieving this? Eventually I hope to have a list (size n) of the best fitness levels. I imagine that I would have to add the best fitness in each iteration to a list of some type?

6
  • Could you be more specific? What kind of variable is the fitness (int, double, etc.)? Commented Mar 25, 2011 at 17:51
  • fitness1 and fitness2 are both double variables. discard just means 'ignore' - I am only looking to keep track of the best fitness level. Commented Mar 25, 2011 at 17:52
  • 4
    This looks like a very very basic Java question. Have you read tutorials about basic Java syntax, especially declaring and assigning variables? Commented Mar 25, 2011 at 17:53
  • 1
    Are greater or lesser numbers better? Your current comparison would imply that a lower fitness score is better than a higher one (which may very well be the case, of course). Commented Mar 25, 2011 at 17:54
  • 1
    What kind of data are fitness1 and fitness2? What do you mean by "discard"? Java has automatic garbage collection, so you don't need to do anything to discard an object; just don't keep a reference to it. Commented Mar 25, 2011 at 17:56

5 Answers 5

6
best = (fitness1 < fitness2) ? fitness1 : fitness2;

?: ternary operator may be useful in this kind of decision making process.

Sign up to request clarification or add additional context in comments.

4 Comments

Which is a convulted and hardly scaling way to reinvent min.
Wish i could +2 this, don't see enough ternary operators in Java :D
Seems like a baroque way of writing best = Math.min(fitness1, fitness2);
I have C/C++ background and ?: was made available in Java especially for people like me. I like it and I'm used to it. :) Of course both methods are OK.
3

I'd say, create an ArrayList<Double> to keep the best values in and use this:

arrayList.add((fitness1 < fitness2) ? fitness1 : fitness2);

That way you'll have a list of the best values.

1 Comment

Though I don't see any duplicate results in the example output, could you show what algorithm you're using to go through the possible fitness values?
1

If your end goal is to have a list of the "top n fitness levels", you'll probably have a list of them coming in, right?

If that's the case, just leverage the capabilities of List:

// takes a list of Doubles, returns 'top' levels
public List<Double> getTopN(List<Double> allLevels, int top) {
    List<Double> sorted = new ArrayList<Double>(allLevels); // defensive copy
    Collections.sort(sorted, new Comparator<Double>() {
        @Override
        public int compare(Double left, Double right) {
            return left.compareTo(right); // assumes no null entries
        }
    });

    return sorted.subList(0, Math.min(sorted.size(), top));
}

Comments

0

Introduce another variable.

type bestFitness

if(fitness1 < fitness2){
   bestFitness = fitness1;
}else{
   bestFitness = fitness2;
}

Then discard both fitness1, fitness2

Or use Math.min

Comments

0

Assuming the fitness is an int

int fitness1 = 5;
int fitness2 = 10;

int bestFit = bestFit(fitness1, fitness2);

//nothing needs to be done to explicitly discard fitness2.  Just keep using bestFit
//and let the GC worry about discarding stuff for you

public static int bestFit(int f1, int f2) { 

   return f1 > f2 ? f1 : f2

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.