1 | /*
|
---|
2 | Copyright 2006 by Sean Luke
|
---|
3 | Licensed under the Academic Free License version 3.0
|
---|
4 | See the file "LICENSE" for more information
|
---|
5 | */
|
---|
6 |
|
---|
7 |
|
---|
8 | package ec.app.sum;
|
---|
9 | import ec.vector.*;
|
---|
10 | import ec.*;
|
---|
11 | import ec.simple.*;
|
---|
12 | import ec.util.*;
|
---|
13 |
|
---|
14 | /*
|
---|
15 | * Sum.java
|
---|
16 | *
|
---|
17 | * Created on Sat Jun 16 23:26:38 EDT 2001
|
---|
18 | * By Sean Luke
|
---|
19 | */
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Sum is a simple example of the ec.Vector package, implementing the
|
---|
23 | * very simple sum problem (fitness = sum over vector).
|
---|
24 | * This is a generalization of the common MaxOnes problem
|
---|
25 | * (fitness = number of 1's in vector).
|
---|
26 |
|
---|
27 | *
|
---|
28 | * @author Sean Luke
|
---|
29 | * @version 1.0
|
---|
30 | */
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 | public class Sum extends Problem implements SimpleProblemForm
|
---|
35 | {
|
---|
36 | public static final String P_SUM = "sum";
|
---|
37 |
|
---|
38 | public Parameter defaultBase()
|
---|
39 | {
|
---|
40 | return super.defaultBase().push(P_SUM);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void evaluate(final EvolutionState state,
|
---|
44 | final Individual ind,
|
---|
45 | final int subpopulation,
|
---|
46 | final int threadnum)
|
---|
47 | {
|
---|
48 | if (ind.evaluated) return;
|
---|
49 |
|
---|
50 | if (!(ind instanceof IntegerVectorIndividual))
|
---|
51 | state.output.fatal("Whoa! It's not an IntegerVectorIndividual!!!",null);
|
---|
52 |
|
---|
53 | IntegerVectorIndividual ind2 = (IntegerVectorIndividual)ind;
|
---|
54 | IntegerVectorSpecies s = (IntegerVectorSpecies)ind2.species;
|
---|
55 |
|
---|
56 | long sum=0;
|
---|
57 | long max=0;
|
---|
58 | for(int x=0; x<ind2.genome.length; x++)
|
---|
59 | {
|
---|
60 | sum += ind2.genome[x];
|
---|
61 | max += (int)(s.maxGene(x)); // perhaps this neededn't be computed over and over again
|
---|
62 | }
|
---|
63 |
|
---|
64 | // Now we know that max is the maximum possible value, and sum is the fitness.
|
---|
65 |
|
---|
66 | // assume we're using SimpleFitness
|
---|
67 | ((SimpleFitness)ind2.fitness).setFitness(state,
|
---|
68 | /// ...the fitness...
|
---|
69 | (float)(((double)sum)),
|
---|
70 | ///... our definition of the ideal individual
|
---|
71 | sum == max);
|
---|
72 |
|
---|
73 | ind2.evaluated = true;
|
---|
74 | }
|
---|
75 | }
|
---|