Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/app/nk/NK.java @ 9449

Last change on this file since 9449 was 6152, checked in by bfarka, 14 years ago

added ecj and custom statistics to communicate with the okb services #1441

File size: 3.5 KB
Line 
1package ec.app.nk;
2
3import ec.*;
4import ec.simple.*;
5import ec.vector.*;
6import ec.util.*;
7import java.util.*;
8
9/**
10   NK implmements the NK-landscape developed by Stuart Kauffman (in the book <i>The Origins of
11   Order: Self-Organization and Selection in Evolution</a>).  In the NK model, the fitness
12   contribution of each allele depends on how that allele interacts with K other alleles.  Based on
13   this interaction, each gene contributes a random number between 0 and 1.  The individual's
14   fitness is the average of these N random numbers. 
15
16   <p><b>Parameters</b><br>
17   <table>
18   <tr><td valign=top><i>base</i>.<tt>k</tt><br>
19   <font size=-1>int >= 0 && < 31</td>
20   <td valign=top>(number of interacting alleles)</td></tr>
21   <tr><td valign=top><i>base</i>.<tt>adjacent</tt><br>
22   <font size=-1>boolean</font></td>
23   <td valign=top>(should interacting alleles be adjacent to the given allele)</td></tr>
24   </table>
25 
26   @author Keith Sullivan
27   @version 1.0
28*/
29
30
31public class NK extends Problem implements SimpleProblemForm
32    {
33    public static final String P_N = "n";
34    public static final String P_K = "k";
35    public static final String P_ADJACENT="adjacent";
36       
37    int k;
38    boolean adjacentNeighborhoods;
39    HashMap oldValues;
40       
41    public void setup(final EvolutionState state, final Parameter base)
42        {
43        super.setup(state, base);
44               
45        k = state.parameters.getInt(base.push(P_K), null, 1);
46        if ((k < 0) || (k > 31))
47            state.output.fatal("Value of k must be between 0 and 31", base.push(P_K));
48               
49        adjacentNeighborhoods = state.parameters.getBoolean(base.push(P_ADJACENT), null, true);
50        oldValues = new HashMap();
51        }
52       
53    public void evaluate(final EvolutionState state, final Individual ind, final int subpopulation, final int threadnum)
54        {
55        BitVectorIndividual ind2 = (BitVectorIndividual) ind;
56        double fitness =0;
57        int n = ind2.genome.length;
58               
59        for (int i=0; i < n; i++)
60            {
61            boolean tmpInd[] = new boolean[k+1];
62            tmpInd[0] = ind2.genome[i];
63                       
64            double val=0;
65            if (adjacentNeighborhoods)
66                {
67                int offset = n - k/2;
68                for (int j=0; j < k; j++)
69                    {
70                    tmpInd[j+1] = ind2.genome[(j+i + offset) % n];
71                    }
72                }
73            else
74                {
75                int j;
76                for (int l=0; l < k; l++)
77                    {
78                    while ((j = state.random[0].nextInt(k)) == i);
79                    tmpInd[l+1] = ind2.genome[j];
80                    }
81                }
82                       
83            if (oldValues.containsKey(tmpInd))
84                val = ((Double)oldValues.get(tmpInd)).doubleValue();
85            else
86                {
87                double tmp=0;
88                for (int j=0; j < tmpInd.length; j++) 
89                    if (tmpInd[j]) tmp += 1 << j;
90                val = tmp /  Integer.MAX_VALUE;
91                                                               
92                oldValues.put(tmpInd, new Double(val));
93                }
94                       
95            fitness += val;
96            }
97                               
98        fitness /= n;
99        ((SimpleFitness)(ind2.fitness)).setFitness( state, (float) fitness, false);
100        ind2.evaluated = true;
101        }
102    }
Note: See TracBrowser for help on using the repository browser.