$darkmode
DENOPTIM
GAParametersTest.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 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.denovo;
20
21import static org.junit.jupiter.api.Assertions.assertEquals;
22import static org.junit.jupiter.api.Assertions.assertFalse;
23import static org.junit.jupiter.api.Assertions.assertNotNull;
24import static org.junit.jupiter.api.Assertions.assertTrue;
25import static org.junit.jupiter.api.condition.OS.WINDOWS;
26
27import java.io.File;
28import java.util.ArrayList;
29import java.util.logging.Level;
30
31import org.junit.jupiter.api.Test;
32import org.junit.jupiter.api.condition.DisabledOnOs;
33import org.junit.jupiter.api.io.TempDir;
34
35import denoptim.files.FileUtils;
36import denoptim.io.DenoptimIO;
37import denoptim.programs.RunTimeParameters.ParametersType;
38
45public class GAParametersTest
46{
47 private final String SEP = System.getProperty("file.separator");
48
49 @TempDir
50 File tempDir;
51
52//------------------------------------------------------------------------------
53
54 @Test
55 public void testYesNoTrueFalseKeyword() throws Exception
56 {
57 GAParameters gaParams = new GAParameters();
58 gaParams.interpretKeyword("GA-MUTATEDGRAPHCHECKFAILTOLERANT=F");
59 assertFalse(gaParams.mutatedGraphFailedEvalTolerant);
60 gaParams.interpretKeyword("GA-MUTATEDGRAPHCHECKFAILTOLERANT=T");
61 assertTrue(gaParams.mutatedGraphFailedEvalTolerant);
62 gaParams.interpretKeyword("GA-MUTATEDGRAPHCHECKFAILTOLERANT=false");
63 assertFalse(gaParams.mutatedGraphFailedEvalTolerant);
64 gaParams.interpretKeyword("GA-MUTATEDGRAPHCHECKFAILTOLERANT=true");
65 assertTrue(gaParams.mutatedGraphFailedEvalTolerant);
66 gaParams.interpretKeyword("GA-MUTATEDGRAPHCHECKFAILTOLERANT=n");
67 assertFalse(gaParams.mutatedGraphFailedEvalTolerant);
68 gaParams.interpretKeyword("GA-MUTATEDGRAPHCHECKFAILTOLERANT=y");
69 assertTrue(gaParams.mutatedGraphFailedEvalTolerant);
70 gaParams.interpretKeyword("GA-MUTATEDGRAPHCHECKFAILTOLERANT=no");
71 assertFalse(gaParams.mutatedGraphFailedEvalTolerant);
72 gaParams.interpretKeyword("GA-MUTATEDGRAPHCHECKFAILTOLERANT=yes");
73 assertTrue(gaParams.mutatedGraphFailedEvalTolerant);
74 }
75
76//------------------------------------------------------------------------------
77
78 @Test
79 public void testInterpretationOfKeywords() throws Exception
80 {
81 double t = 0.0001;
82 GAParameters gaparams = new GAParameters();
83 gaparams.interpretKeyword(
84 "GA-MULTISITEMUTATIONWEIGHTS=1.1,2.2 , 3.3");
85 double[] r = gaparams.getMultiSiteMutationWeights();
86 assertEquals(3,r.length);
87 assertTrue(Math.abs(1.1-r[0]) < t);
88 assertTrue(Math.abs(2.2-r[1]) < t);
89 assertTrue(Math.abs(3.3-r[2]) < t);
90
91 gaparams.interpretKeyword(
92 "GA-MULTISITEMUTATIONWEIGHTS=1 2 3 4");
93 r = gaparams.getMultiSiteMutationWeights();
94 assertEquals(4,r.length);
95 assertTrue(Math.abs(1.0-r[0]) < t);
96 assertTrue(Math.abs(2.0-r[1]) < t);
97 assertTrue(Math.abs(3.0-r[2]) < t);
98 }
99
100//------------------------------------------------------------------------------
101
102 @Test
103 @DisabledOnOs(WINDOWS)
104 public void testLogging() throws Exception
105 {
106 assertTrue(this.tempDir.isDirectory(),"Should be a directory ");
107
108 GAParameters gaParams = new GAParameters();
109 String uniqueString = "UNIQUESTRING@LOG";
110 int[] expectedCounts = new int[] {0, 0, 1, 2, 3, 4, 5, 6, 6, 6};
111
112 for (int i=0; i<10; i++)
113 {
114 int verbosity = i-4;
115 String pathname = tempDir.getAbsolutePath() + SEP + i;
116 FileUtils.createDirectory(pathname);
117 gaParams.setWorkingDirectory(pathname);
118 gaParams.readParameterLine(ParametersType.GA_PARAMS.getKeywordRoot()
119 +"VERBOSITY="+verbosity);
120 gaParams.processParameters();
121 gaParams.startProgramSpecificLogger("UnitTestLogger");
122 assertNotNull(gaParams.getLogger());
123 gaParams.getLogger().log(Level.SEVERE,"msg-severe "+uniqueString);
124 gaParams.getLogger().log(Level.WARNING,"msg-warning "+uniqueString);
125 gaParams.getLogger().log(Level.INFO,"msg-info "+uniqueString);
126 gaParams.getLogger().log(Level.FINE,"msg-fine "+uniqueString);
127 gaParams.getLogger().log(Level.FINER,"msg-finer "+uniqueString);
128 gaParams.getLogger().log(Level.FINEST,"msg-finest "+uniqueString);
129 String logFileString = gaParams.getLogFilePathname();
130 assertTrue(FileUtils.checkExists(logFileString));
131 String dataFolder = gaParams.getDataDirectory();
132 assertTrue(FileUtils.checkExists(dataFolder));
133 ArrayList<String> lines = DenoptimIO.readList(logFileString, true);
134 int counter = 0;
135 for (String line : lines)
136 {
137 if (line.contains(uniqueString))
138 counter++;
139 }
140 assertEquals(expectedCounts[i],counter);
141 }
142 }
143
144//------------------------------------------------------------------------------
145
146}
static boolean checkExists(String fileName)
Definition: FileUtils.java:241
static boolean createDirectory(String fileName)
Creates a directory.
Definition: FileUtils.java:231
Utility methods for input/output.
static ArrayList< String > readList(String fileName)
Read list of data as text.
Logger startProgramSpecificLogger(String loggerIdentifier)
Starts a logger with the given name.
Logger getLogger()
Get the name of the program specific logger.
String getLogFilePathname()
Gets the pathname to the log file.
Parameters for genetic algorithm.
void interpretKeyword(String key, String value)
Processes a keyword/value pair and assign the related parameters.
boolean mutatedGraphFailedEvalTolerant
Flag that enables the ignoring of mutated graphs that lead to a failure in the evaluation of graphs t...
void processParameters()
Processes currently loaded fields.
void setWorkingDirectory(String pathName)
GA_PARAMS
Parameters pertaining the genetic algorithm.