aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/resources/Resources.java
blob: e1438f110ddaae79b1e24cb17dc897ff93a2d81a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package jcgp.backend.resources;

import java.util.Random;

import jcgp.backend.function.Function;
import jcgp.backend.function.FunctionSet;
import jcgp.backend.resources.parameters.IntegerParameter;

/**
 * 
 * The resources class encapsulates all of the resources based on which the program operates.
 * Each instance of JCGP contains a single instance of Resources, which gets passed to the selected
 * modules as the program executes.
 * 
 * @author Eduardo Pedroni
 *
 */
public class Resources {
	protected IntegerParameter rows, columns, inputs, outputs, populationSize,
			levelsBack, currentGeneration, generations, currentRun, runs,
			arity, seed, reportInterval;

	protected Random numberGenerator = new Random();
	protected FunctionSet functionSet;
	// GUI console
	protected Console console;
	
	/**
	 * @return the rows
	 */
	public int rows() {
		return rows.get();
	}

	/**
	 * @return the columns
	 */
	public int columns() {
		return columns.get();
	}

	/**
	 * @return the inputs
	 */
	public int inputs() {
		return inputs.get();
	}

	/**
	 * @return the outputs
	 */
	public int outputs() {
		return outputs.get();
	}

	/**
	 * @return the populationSize
	 */
	public int populationSize() {
		return populationSize.get();
	}

	/**
	 * @return the levelsBack
	 */
	public int levelsBack() {
		return levelsBack.get();
	}

	/**
	 * @return the nodes
	 */
	public int nodes() {
		return columns.get() * rows.get();
	}

	/**
	 * @return the currentGeneration
	 */
	public int currentGeneration() {
		return currentGeneration.get();
	}

	/**
	 * @return the generations
	 */
	public int generations() {
		return generations.get();
	}

	/**
	 * @return the currentRun
	 */
	public int currentRun() {
		return currentRun.get();
	}

	/**
	 * @return the runs
	 */
	public int runs() {
		return runs.get();
	}

	/**
	 * @return the arity
	 */
	public int arity() {
		return arity.get();
	}

	/**
	 * @return the seed
	 */
	public int seed() {
		return seed.get();
	}

	/**
	 * @return the report interval
	 */
	public int reportInterval() {
		return reportInterval.get();
	}
	
	/*
	 * Utility functions
	 */
	public int getRandomInt(int limit) {
		return numberGenerator.nextInt(limit);
	}
	
	public double getRandomDouble(int limit) {
		return numberGenerator.nextDouble() * limit;
	}
	
	public double getRandomDouble() {
		return numberGenerator.nextDouble();
	}
	
	/*
	 * FunctionSet functions
	 */
	public Function getRandomFunction() {
		Function f = functionSet.getAllowedFunction(numberGenerator.nextInt(functionSet.getAllowedFunctionCount()));
		return f;
	}

	public Function getFunction(int index) {
		return functionSet.getAllowedFunction(index);
	}
	
	/**
	 * @return the functionSet
	 */
	public FunctionSet getFunctionSet() {
		return functionSet;
	}
	
	public int getFunctionIndex(Function function) {
		for (int i = 0; i < functionSet.getTotalFunctionCount(); i++) {
			if (function == functionSet.getFunction(i)) {
				return i;
			}
		}
		// not found, default to 0
		return 0;
	}
	
	/*
	 * Console functionality
	 * These are affected by parameter report interval
	 */
	public void reportln(String s) {
		if (reportInterval.get() > 0) {
			if (currentGeneration.get() % reportInterval.get() == 0) {
				System.out.println(s);
				if (console != null) {
					console.println(s);
				}
			}
		}
	}
	
	public void report(String s) {
		if (reportInterval.get() > 0) {
			if (currentGeneration.get() % reportInterval.get() == 0) {
				System.out.print(s);
				if (console != null) {
					console.print(s);
				}
			}
		}
	}
	
	/*
	 * Console functionality
	 * These are not affected by parameter report interval
	 */
	public void println(String s) {
		System.out.println(s);
		if (console != null) {
			console.println(s);
		}
	}
	
	public void print(String s) {
		System.out.print(s);
		if (console != null) {
			console.print(s);
		}
	}
}