$darkmode
DENOPTIM
Monitor.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2021 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.logging;
20
21import java.util.HashMap;
22import java.util.concurrent.atomic.AtomicInteger;
23import java.util.logging.Level;
24import java.util.logging.Logger;
25
26import denoptim.exception.DENOPTIMException;
27import denoptim.io.DenoptimIO;
28
36public class Monitor extends HashMap<CounterID,AtomicInteger>
37{
41 private static final long serialVersionUID = 1L;
42
46 public String name = "noname";
47
51 private String monitorFile = "unset.eamonitor";
52
57 private int dumpStep = 50;
58
62 private boolean dumpData = false;
63
67 public int generationId = 0;
68
72 private int dumpsId = 0;
73
77 private Logger logger;
78
79 private final String NL = System.getProperty("line.separator");
80
81//------------------------------------------------------------------------------
82
86 public Monitor()
87 {
88 super();
89 for (CounterID cid : CounterID.values())
90 {
91 this.put(cid,new AtomicInteger());
92 }
93 }
94
95//------------------------------------------------------------------------------
96
103 public Monitor(String identifier, int genId, String monitorFile,
104 int dumpStep, boolean dumpData, Logger logger)
105 {
106 this();
107 name = identifier;
108 generationId = genId;
109 this.monitorFile = monitorFile;
110 this.dumpStep = dumpStep;
111 this.dumpData = dumpData;
112 this.logger = logger;
113 }
114
115//------------------------------------------------------------------------------
116
117 public void changeBy(CounterID cid, int value)
118 {
119 String dump = "";
120 synchronized (this)
121 {
122 this.get(cid).addAndGet(value);
124 {
125 dumpsId++;
126 if (dumpData && dumpsId >= dumpStep)
127 {
128 dumpsId = 0;
129 dump = getMonitorDataLine("DUMP");
130 }
131 }
132 }
133 if (!dump.equals(""))
134 {
135 try
136 {
137 printSnapshot(dump);
138 } catch (DENOPTIMException e)
139 {
140 logger.log(Level.WARNING,
141 "Unable to print monitor report: "+e.getMessage() + NL
142 + "Monitor report: " + NL + dump);
143 }
144 }
145 }
146
147//------------------------------------------------------------------------------
148
149 public void increase(CounterID cid)
150 {
151 changeBy(cid,1);
152 }
153
154//------------------------------------------------------------------------------
155
156 public void increaseBy(CounterID cid, int value)
157 {
158 changeBy(cid,value);
159 }
160
161//------------------------------------------------------------------------------
162
163 public void decrease(CounterID cid)
164 {
165 changeBy(cid,-1);
166 }
167
168//------------------------------------------------------------------------------
169
170 public void decreaseBy(CounterID cid, int value)
171 {
172 changeBy(cid,-value);
173 }
174
175//------------------------------------------------------------------------------
176
177 public void printHeader(String pathName) throws DENOPTIMException
178 {
179 DenoptimIO.writeData(pathName, getMonitorDataHeader(), true);
180 }
181
182//------------------------------------------------------------------------------
183
184 public void printSummary() throws DENOPTIMException
185 {
187 }
188
189//------------------------------------------------------------------------------
190
191 public void printSnapshot(String snapshot) throws DENOPTIMException
192 {
193 DenoptimIO.writeData(monitorFile, snapshot, true);
194 }
195
196//------------------------------------------------------------------------------
197
202 private String getMonitorDataHeader()
203 {
204 StringBuilder sb = new StringBuilder();
205 sb.append("RecordType MonitorName Generation ");
206 synchronized (this)
207 {
208 for (CounterID cid : CounterID.values())
209 {
210 sb.append(cid).append(" ");
211 }
212 }
213 return sb.toString();
214 }
215
216//------------------------------------------------------------------------------
217
223 private String getMonitorDataLine(String prefix)
224 {
225 StringBuilder sb = new StringBuilder();
226 sb.append(prefix).append(" ");
227 sb.append(name).append(" ");
228 sb.append(generationId).append(" ");
229 synchronized (this)
230 {
231 for (CounterID cid : CounterID.values())
232 {
233 sb.append(this.get(cid).get()).append(" ");
234 }
235 }
236 return sb.toString();
237 }
238
239//------------------------------------------------------------------------------
240
241}
Utility methods for input/output.
static void writeData(String fileName, String data, boolean append)
Write text-like data file.
A collection of counters user to count actions taken by the evolutionary algorithm.
Definition: Monitor.java:37
String name
A name that allows humans to understand what this is a monitor of.
Definition: Monitor.java:46
void decreaseBy(CounterID cid, int value)
Definition: Monitor.java:170
int dumpStep
Number of steps (i.e., attempts to make new candidates) after which we dump data to file,...
Definition: Monitor.java:57
Monitor()
Creates an unnamed monitor.
Definition: Monitor.java:86
String monitorFile
Pathname to a file where to dump data.
Definition: Monitor.java:51
void increase(CounterID cid)
Definition: Monitor.java:149
void increaseBy(CounterID cid, int value)
Definition: Monitor.java:156
void decrease(CounterID cid)
Definition: Monitor.java:163
int dumpsId
Counter controlling dumps.
Definition: Monitor.java:72
String getMonitorDataHeader()
Build a string with the names of all counters.
Definition: Monitor.java:202
void printSnapshot(String snapshot)
Definition: Monitor.java:191
Logger logger
Logger to use.
Definition: Monitor.java:77
String getMonitorDataLine(String prefix)
Build a string with the value of all counters.
Definition: Monitor.java:223
static final long serialVersionUID
Version UID.
Definition: Monitor.java:41
Monitor(String identifier, int genId, String monitorFile, int dumpStep, boolean dumpData, Logger logger)
Creates a named monitor that is marked with the given generation number.
Definition: Monitor.java:103
void changeBy(CounterID cid, int value)
Definition: Monitor.java:117
int generationId
A generation number.
Definition: Monitor.java:67
void printHeader(String pathName)
Definition: Monitor.java:177
boolean dumpData
Flag requesting to dump the monitor data on file.
Definition: Monitor.java:62
Identifier of a counter.
Definition: CounterID.java:29