Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/spatial/Spatial1DSubpopulation.java @ 12700

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

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

File size: 3.9 KB
Line 
1/*
2  Copyright 2006 by Sean Luke and George Mason University
3  Licensed under the Academic Free License version 3.0
4  See the file "LICENSE" for more information
5*/
6
7package ec.spatial;
8
9import ec.*;
10import ec.util.*;
11
12/*
13 * Spatial1DSubpopulation.java
14 *
15 * By: Liviu Panait
16 */
17
18/**
19 *      A Spatial1DSubpopulation is an EC subpopulation that is additionally embedded into
20 *      a one-dimmensional space.
21 *      In a spatially-embedded EA, the subpopulations of individuals are assumed to be
22 *      spatially distributed in some sort of space, be it one-dimmensional, two-
23 *      dimmensional, or whatever else.  The space may or may not be toroidal (although
24 *      it usually is).  Each location in the space has a set of neighboring locations.
25 *      Thus, each individual has an index in the subpopulation, and also a location in
26 *      the space.
27 *
28 *      <p>This public interface provides a method to obtain the indexes of the neighbors
29 *      of a location.
30 *
31 *      <P>This Subpopulation does not include toroidalness in writing out to streams.
32 *
33 <p><b>Parameters</b><br>
34 <table>
35 <tr><td valign=top><tt>toroidal</tt><br>
36 <font size=-1>true (default) or false</font></td>
37 <td valign=top>(Is this space toroidal?)</td></tr>
38
39 *
40 *
41 * @author Liviu Panait
42 * @version 1.0
43 */
44public class Spatial1DSubpopulation extends Subpopulation implements Space
45    {
46    /**
47       This parameter stipulates whether the world is toroidal or not.
48       If missing, its default value is true.
49    */
50    public static final String P_TOROIDAL = "toroidal";
51    public boolean toroidal;
52
53    /**
54       Read additional parameters for the spatially-embedded subpopulation.
55    */
56    public void setup(final EvolutionState state, final Parameter base)
57        {
58        super.setup(state,base);
59
60        // by default, the space is toroidal
61        toroidal = state.parameters.getBoolean(base.push(P_TOROIDAL),null,true);
62
63        }
64
65    /*
66      1D mapping is identity
67    */
68    /*
69      public int locationToIndex( final Object location )
70      {
71      if( location instanceof Integer )
72      return ((Integer)location).intValue();
73      return -1;
74      }
75    */
76
77    /*
78      1D mapping is identity
79    */
80    /*
81      public Object indexToLocation( final int index)
82      {
83      return new Integer(index);
84      }
85    */
86
87    public void setIndex( int threadnum, int index )
88        {
89        if( indexes == null )
90            indexes = new int[threadnum+1];
91        if( threadnum >= indexes.length )
92            {
93            int currentSize = indexes.length;
94            int[] temp = new int[threadnum*2+1];
95            System.arraycopy(indexes,0,temp,0,currentSize);
96            indexes = temp;
97            }
98        indexes[threadnum] = index;
99        }
100
101    public int getIndex( int threadnum )
102        {
103        if( indexes == null || threadnum > indexes.length )
104            return -1;
105        else
106            return indexes[threadnum];
107        }
108
109    // indexed by threadnum
110    int[] indexes;
111
112    /**
113       Returns a the index of a random neighbor.
114    */
115    public int getIndexRandomNeighbor( final EvolutionState state, int threadnum, int distance )
116        {
117        int index = indexes[threadnum];
118
119        int size = individuals.length;
120        if( size == 0 )
121            return index;
122        if( toroidal )
123            {
124            int max = (2*distance+1>size) ? size : (2*distance+1);
125            int rand = state.random[threadnum].nextInt(max);
126            int val= (index+rand-distance);
127            if (val >= 0 && val < size) return val;
128            val = val % size;
129            if (val >= 0) return val;
130            else return val + size;
131            }
132        else
133            {
134            int min = (index-distance<0) ? 0 : (index-distance);
135            int max = (index+distance>=size) ? size : (index+distance);
136            int val = min + state.random[threadnum].nextInt(max-min+1);
137            return val;
138            }
139        }
140    }
Note: See TracBrowser for help on using the repository browser.