aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/parsers/ParameterParser.java
blob: 711b4d278e7654a76fcce082b7bf1f52b32d23aa (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
package jcgp.backend.parsers;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

import jcgp.backend.resources.ModifiableResources;

/**
 * Contains a static method for parsing parameters from a
 * .par file. 
 * 
 * @author Eduardo Pedroni
 *
 */
public abstract class ParameterParser {

	/**
	 * Parses the parameters from a specified CGP parameter file and
	 * modifies the experiment resources appropriately. 
	 * <br><br>
	 * CGP .par files do not follow a very strict convention, so this
	 * parser does its best to cope with format irregularities. Parsing
	 * works even if the parameters are in the wrong order, and unknown
	 * parameters are simply ignored.
	 * 
	 * @param file the .par file to parse.
	 * @param resources a reference to the resources object that must be modified.
	 */
	public static void parse(File file, ModifiableResources resources) {
		// create reader and scanner, print message if file is missing
		FileReader fr;
		try {
			fr = new FileReader(file);
		} catch (FileNotFoundException e) {
			resources.println("[Parser] Error: could not find " + file.getAbsolutePath());
			return;
		}
		
		Scanner in = new Scanner(fr);
		resources.println("[Parser] Parsing file: " + file.getAbsolutePath() + "...");
		
		// parse line by line
		while (in.hasNextLine()) {
			// split if one or more tabs or one or more spaces are found
			String[] splitString = in.nextLine().split("( |\t)+");
			// expected length is 2, anything beyond that should also work
			if (splitString.length >= 2) {
				switch (splitString[1]) {
				case "population_size":
					resources.setPopulationSize(Integer.parseInt(splitString[0]));
					resources.println("[Parser] Population size is now " + resources.populationSize());
					break;

				case "num_generations":
					resources.setGenerations(Integer.parseInt(splitString[0]));
					resources.println("[Parser] Total generations is now " + resources.generations());
					break;

				case "num_runs_total":
					resources.setRuns(Integer.parseInt(splitString[0]));
					resources.println("[Parser] Total runs is now " + resources.runs());
					break;

				case "num_rows":
					resources.setRows(Integer.parseInt(splitString[0]));
					resources.println("[Parser] Row number is now " + resources.rows());
					break;

				case "num_cols":
					resources.setColumns(Integer.parseInt(splitString[0]));
					resources.println("[Parser] Column number is now " + resources.columns());
					break;

				case "levels_back":
					resources.setLevelsBack(Integer.parseInt(splitString[0]));
					resources.println("[Parser] Levels back is now " + resources.levelsBack());
					break;

				case "report_interval":
					resources.setReportInterval(Integer.parseInt(splitString[0]));
					resources.println("[Parser] Report interval is now " + resources.levelsBack());
					break;

				case "global_seed":
					resources.setSeed(Integer.parseInt(splitString[0]));
					resources.println("[Parser] Seed is now " + resources.seed());
					break;

				default:
					break;
				}
			}
		}
		
		in.close();
		resources.println("[Parser] Finished parsing parameters");
	}
}