Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/app/hiff/HIFF.java @ 12147

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

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

File size: 3.2 KB
Line 
1package ec.app.hiff;
2
3import ec.*;
4import ec.util.*;
5import ec.vector.*;
6import ec.simple.*;
7
8/**
9    HIFF implements the Hierarchical If-And-Only-If problem developed by Watson, Hornby and Pollack.
10    See <a href="http://www.cs.brandeis.edu/~richardw/hiff.html">The HIFF Generator</a> for more information
11    and papers.
12
13    <p><b>Parameters</b><br>
14    <table>
15    <tr><td valign=top><i>base</i>.<tt>p</tt><br>
16    <font size=-1>int >= 0 </td>
17    <td valign=top>(number of blocks at each level)</td></tr>
18    <tr><td valign=top><i>base</i>.<tt>k</tt><br>
19    <font size=-1>int >= 0 </td>
20    <td valign=top>(number of hierarchical levels)</td></tr>
21    <tr><td valign=top><i>base</i>.<tt>rc</tt><br>
22    <font size=-1>double </td>
23    <td valign=top>(ratio of block contributions)</td></tr>
24    </table>
25
26    @author Keith Sullivan
27    @version 1.0
28*/
29
30public class HIFF extends Problem implements SimpleProblemForm
31    {
32
33    public static final String P_K = "k";
34    public static final String P_P = "p";
35    public static final String P_RC = "rc";
36       
37    int K, P, Rc;
38               
39    public void setup(EvolutionState state, Parameter base)
40        {
41        super.setup(state, base);
42               
43        K = state.parameters.getInt(base.push(P_K), null, 0);
44        if (K < 0)
45            state.output.fatal("k must be > 0", base.push(P_K));
46               
47        P = state.parameters.getInt(base.push(P_P), null, 0);
48        if (P < 0)
49            state.output.fatal("p must be > 0", base.push(P_P));
50               
51        Rc = state.parameters.getInt(base.push(P_RC), null, 0);
52        if (Rc < 0)
53            state.output.fatal("rc must be > 0", base.push(P_RC));
54        }
55       
56    public void evaluate(final EvolutionState state, final Individual ind, final int subpopulation, final int threadnum)
57        {
58        BitVectorIndividual ind2 = (BitVectorIndividual) ind;
59               
60        double genes[] = new double[ind2.genome.length];
61        for (int i=0; i < genes.length; i++)
62            genes[i] = ((ind2.genome[i]) ? 1 : 0);
63        double fitness = H(genes);
64               
65        ((SimpleFitness)(ind2.fitness)).setFitness( state, (float) fitness, false);
66        ind2.evaluated = true;
67        }
68               
69    double H(double genes[])
70        {
71        double bonus=1, F=0;
72        int last = genes.length;
73               
74        for (int i=0; i < last; i++)
75            F += f(genes[i]) ;
76               
77        for (int i=1; i <= P; i++)
78            {
79            last /= K;
80            bonus *= Rc;
81            for (int j=0; j < last; j++)
82                {
83                genes[j] = t(genes, j*K);
84                F += f(genes[j]) * bonus;
85                }
86            }
87               
88        return F;
89        }
90       
91    double t(double transform[], int first)
92        {
93        int s=0;
94        for (int i=first+1; i < first+K; i++)
95            {
96            if (transform[first] == transform[i])
97                s++;
98            }
99        if (s == (K-1)) return transform[first];
100               
101        return -1;
102        }
103       
104    double f(double b)
105        {
106        if (b != -1) return 1;
107        return 0;
108        }
109    }
Note: See TracBrowser for help on using the repository browser.