$darkmode
DENOPTIM
GeneOpsRunnerParameters.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.programs.genetweeker;
20
21import java.lang.reflect.Field;
22import java.util.ArrayList;
23import java.util.List;
24
25import denoptim.exception.DENOPTIMException;
26import denoptim.files.FileUtils;
27import denoptim.programs.RunTimeParameters;
28import denoptim.programs.denovo.GAParameters;
29import denoptim.utils.CrossoverType;
30import denoptim.utils.MutationType;
31
32
40{
44 protected enum Operator {MUTATION,XOVER,CROSSOVER}
45
50
55 protected int[] mutationTarget;
56
61 protected int idTargetAP = -1;
62
67
73 protected int idNewVrt = -1;
74
80 protected int idNewAP = -1;
81
85 protected String inpFileM;
86
90 protected String inpFileF;
91
96
100 protected int maxSwappableChainLength = Integer.MAX_VALUE;
101
106 protected int[] xoverSrcMale;
107
113 protected List<int[]> xoverSubGraphEndMale = new ArrayList<int[]>();
114
118 protected int mapid;
119
124 protected int[] xoverSrcFemale;
125
131 protected List<int[]> xoverSubGraphEndFemale = new ArrayList<int[]>();
132
136 protected int fapid;
137
141 protected String outFileM;
142
146 protected String outFileF;
147
148//-----------------------------------------------------------------------------
149
154 {
156 }
157
158//-----------------------------------------------------------------------------
159
167 public void interpretKeyword(String key, String value)
168 throws DENOPTIMException
169 {
170 String msg = "";
171 switch (key.toUpperCase())
172 {
173 case "OP=":
174 operatorToTest = Operator.valueOf(value.toUpperCase());
175 break;
176 case "INPFILE=":
177 inpFileM = value;
178 break;
179 case "OUTFILE=":
180 outFileM = value;
181 break;
182 case "MUTATIONTARGET=":
183 {
184 String[] parts = value.split(",");
185 mutationTarget = new int[parts.length];
186 for (int i=0; i<parts.length; i++)
187 {
188 mutationTarget[i] = Integer.parseInt(parts[i].trim());
189 }
190 break;
191 }
192 case "APIDONTARGETVERTEX=":
193 idTargetAP = Integer.parseInt(value);
194 break;
195 case "MUTATIONTYPE=":
196 mutationType = MutationType.valueOf(value);
197 break;
198 case "NEWVERTEXMOLID=":
199 idNewVrt = Integer.parseInt(value);
200 break;
201 case "NEWAPID=":
202 idNewAP = Integer.parseInt(value);
203 break;
204 case "WORKDIR=":
205 workDir = value;
206 break;
207 case "INPFILEMALE=":
208 inpFileM = value;
209 break;
210 case "INPFILEFEMALE=":
211 inpFileF = value;
212 break;
213 case "VERTEXMALE=":
214 {
215 String[] parts = value.split(",");
216 xoverSrcMale = new int[parts.length];
217 for (int i=0; i<parts.length; i++)
218 {
219 xoverSrcMale[i] = Integer.parseInt(parts[i].trim());
220 }
221 break;
222 }
223 case "SUBGRAPHENDMALE=":
224 {
225 String[] parts = value.split(",");
226 int[] idList = new int[parts.length];
227 for (int i=0; i<parts.length; i++)
228 {
229 idList[i] = Integer.parseInt(parts[i].trim());
230 }
231 xoverSubGraphEndMale.add(idList);
232 break;
233 }
234 case "SUBGRAPHENDFEMALE=":
235 {
236 String[] parts = value.split(",");
237 int[] idList = new int[parts.length];
238 for (int i=0; i<parts.length; i++)
239 {
240 idList[i] = Integer.parseInt(parts[i].trim());
241 }
242 xoverSubGraphEndFemale.add(idList);
243 break;
244 }
245 case "APMALE=":
246 mapid = Integer.parseInt(value);
247 break;
248 case "XOVERTYPE=":
249 xoverType = CrossoverType.valueOf(value);
250 break;
251 case "CROSSOVERTYPE=":
252 xoverType = CrossoverType.valueOf(value);
253 break;
254 case "VERTEXFEMALE=":
255 {
256 String[] parts = value.split(",");
257 xoverSrcFemale = new int[parts.length];
258 for (int i=0; i<parts.length; i++)
259 {
260 xoverSrcFemale[i] = Integer.parseInt(parts[i].trim());
261 }
262 break;
263 }
264 case "APFEMALE=":
265 fapid = Integer.parseInt(value);
266 break;
267 case "OUTFILEMALE=":
268 outFileM = value;
269 break;
270 case "OUTFILEFEMALE=":
271 outFileF = value;
272 break;
273 default:
274 msg = "Keyword " + key + " is not a known TestOperator-"
275 + "related keyword. Check input files.";
276 throw new DENOPTIMException(msg);
277 }
278 }
279
280//-----------------------------------------------------------------------------
281
288 {
289 String msg = "";
290 if (!workDir.equals(".") && !FileUtils.checkExists(workDir))
291 {
292 msg = "Directory " + workDir + " not found. Please specify an "
293 + "existing directory.";
294 throw new DENOPTIMException(msg);
295 }
296
298 {
299 msg = "Input file '" + inpFileM + "' not found.";
300 throw new DENOPTIMException(msg);
301 }
302
305 {
306 msg = "Input file '" + inpFileF + "' not found.";
307 throw new DENOPTIMException(msg);
308 }
309
311 }
312
313//----------------------------------------------------------------------------
314
321 {
323 {
324 GAParameters gaPars = new GAParameters();
325 setParameters(gaPars);
326 }
328 }
329
330//------------------------------------------------------------------------------
331
338 public String getPrintedList()
339 {
340 StringBuilder sb = new StringBuilder(1024);
341 sb.append(" " + paramTypeName() + " ").append(NL);
342 for (Field f : this.getClass().getDeclaredFields())
343 {
344 try
345 {
346 sb.append(f.getName()).append(" = ").append(
347 f.get(this)).append(NL);
348 }
349 catch (Throwable t)
350 {
351 sb.append("ERROR! Unable to print " + paramTypeName()
352 + " parameters. Cause: " + t);
353 break;
354 }
355 }
356 for (RunTimeParameters otherCollector : otherParameters.values())
357 {
358 sb.append(otherCollector.getPrintedList());
359 }
360 return sb.toString();
361 }
362
363//----------------------------------------------------------------------------
364
365}
static boolean checkExists(String fileName)
Definition: FileUtils.java:241
Collection of parameters controlling the behavior of the software.
Map< ParametersType, RunTimeParameters > otherParameters
Collection of other parameters by type.
boolean containsParameters(ParametersType type)
void setParameters(RunTimeParameters otherParams)
String paramTypeName()
Returns a string defining the type the parameters collected here.
void checkOtherParameters()
Checks any of the parameter collections contained in this instance.
final String NL
New line character.
void processOtherParameters()
Processes any of the parameter collections contained in this instance.
Parameters for genetic algorithm.
Parameters controlling execution of TestOperator.
List< int[]> xoverSubGraphEndFemale
Female VertedID (not index) that represent the end of the subgraph that is swapped by crossover.
List< int[]> xoverSubGraphEndMale
Male VertedID (not index) that represent the end of the subgraph that is swapped by crossover.
CrossoverType xoverType
Type of crossover to perform.
int[] xoverSrcFemale
Female VertexID (not index) on which perform xover.
int idTargetAP
Target attachment point ID for mutation (AP belonging already to the graph).
void checkParameters()
Evaluate consistency of input parameters.
void processParameters()
Processes all parameters and initialize related objects.
MutationType mutationType
Type of mutation to perform.
int[] xoverSrcMale
Male VertedID (not index) on which perform xover.
void interpretKeyword(String key, String value)
Processes a keyword/value pair and assign the related parameters.
int maxSwappableChainLength
Maximum length of the subgraph that is swapped in a crossover operation.
String getPrintedList()
Returns the list of parameters in a string with newline characters as delimiters.
GO_PARAMS
Parameters controlling stand-alone run of genetic operators.
GA_PARAMS
Parameters pertaining the genetic algorithm.
Types of crossover defined.
BRANCH
Swaps the entire branch starting from a given vertex.
Types of mutation defined in relation to what happens to the target vertex (i.e., the actual mutation...