aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/Utilities.java
blob: 394a4812eca2b1518936ab7b3f5f25a1566c8390 (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
package jcgp;

import java.util.Random;

import jcgp.function.Function;
import jcgp.function.FunctionSet;
import jcgp.population.*;

public class Utilities {
	
	private static Random numberGenerator;
	private static FunctionSet functionSet;
	
	public static void setResources(Random numberGenerator, FunctionSet functionSet) {
		Utilities.numberGenerator = numberGenerator;
		Utilities.functionSet = functionSet;
	}

	public static int getRandomInt(int limit){
		return numberGenerator.nextInt(limit);
	}

	public static double getRandomDouble(int limit){
		return numberGenerator.nextDouble() * limit;
	}

	/**
	 * @param chromosome the chromosome to choose from
	 * @return a random input
	 */
	public static Input getRandomInput(Chromosome chromosome){
		return chromosome.getInput(getRandomInt(Parameters.getInputs()));
	}

	/**
	 * Returns a random allowed node respecting levels back.
	 * 
	 * This method will NOT pick inputs.
	 * 
	 * @param chromosome the chromosome to pick from
	 * @param column the column to use as reference
	 * @return a random node
	 */
	public static Node getRandomNode(Chromosome chromosome, int column){
		// work out the allowed range obeying levels back
		int allowedColumns = ((column >= Parameters.getLevelsBack()) ? Parameters.getLevelsBack() : column);
		int offset = column - allowedColumns;

		// pick a random allowed column and row
		int randomColumn = (getRandomInt(allowedColumns) + offset);
		int randomRow = (getRandomInt(Parameters.getRows()));

		return chromosome.getNode(randomRow, randomColumn);
	}

	/**
	 * Returns a random allowed node.
	 * 
	 * This method will NOT pick inputs.
	 * 
	 * @param chromosome the chromosome to pick from
	 * @param column the column to use as reference
	 * @param levelsBack whether or not to respect levels back
	 * @return a random node
	 */
	public static Node getRandomNode(Chromosome chromosome, int column, boolean levelsBack){
		if (levelsBack) {
			return getRandomNode(chromosome, column);
		} else {
			// pick any random column before the given column
			int randomColumn = (getRandomInt(column));
			// pick a random rowgetColumns
			int randomRow = (getRandomInt(Parameters.getRows()));
			return chromosome.getNode(randomRow, randomColumn);
		}
	}


	/**
	 * pick from any column - use this for setting outputs
	 * 
	 * @param chromosome
	 * @return
	 */
	public static Node getRandomNode(Chromosome chromosome) {
		return chromosome.getNode(getRandomInt(Parameters.getRows()), getRandomInt(Parameters.getColumns()));
	}

	public static Function getRandomFunction() {
		return functionSet.getFunction(Utilities.getRandomInt(functionSet.getFunctionCount()));
	}

	public static Function getFunction(int index) {
		return functionSet.getFunction(index);
	}

}