-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulationFramework.java
More file actions
409 lines (356 loc) · 11.8 KB
/
SimulationFramework.java
File metadata and controls
409 lines (356 loc) · 11.8 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package com.masim.utils;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;
import jade.lang.acl.ACLMessage;
import jade.wrapper.AgentController;
import jade.wrapper.ControllerException;
import jade.wrapper.StaleProxyException;
import jade.core.*;
import com.SimpleBDI.core.SimpleBDIEngine;
import com.masim.core.CooperationStrategy;
import com.masim.core.MachineLearningUtls;
import com.masim.core.SourceTYpe;
import com.masim.core.StartContainer;
import com.masim.emissionAgents.EmissionUnit;
import com.masim.emissionAgents.SourceConfigInfo;
import com.masim.gameTheory.PrisonnerDelimmaUtl;
import com.masim.ui.Console;
/**
* This class is a singleton, giving access to the simulation parameters
*
* @author Sabri Ghazi
*
*/
public class SimulationFramework {
public static boolean usePredifinedScenario = true;
public static int selectedScenario = 0;
public static int useGausianModel = 1;
public static boolean cumulatifPenalty = true;;
public static int first = 0;
public static final long SIMSPEED = 5;
public static int numberOfSource = 160;
public static int nbrUncontrolledSRCSPM10=0;
public static int nbrUncontrolledSRCSSOX=0;
public static int nbrUncontrolledSRCSCOX=0;
public static int nbrUncontrolledSRCSNOX=0;
public static int nbrPM10SRCS = 80;
public static int nbrSOXSRCS = 80;
public static int nbrCOXSRCS = 80;
public static int nbrNOXSRCS = 80;
public static int nbrControlledSRCS;
public static boolean withPenalities = true;;
public static int AirQualityIndexGoal = 2;
public static double maxValPM10 = 50;
public static double maxValSOx = 350;
public static double maxValCOx = 10;
public static double maxValNOx = 200;
public static double maxValO3 = 120;
public static double SusPendfactor = 2.0;
public static double EmessionRate = 1000;
static Environment env;
static SimProcessState currentSimProcessState;
public static CooperationStrategy currentStrategy = CooperationStrategy.GREEDY;
public static Random r = new Random();
public static HashMap<String, SourceConfigInfo> sourcesInfo = new HashMap<String, SourceConfigInfo>();
public static ArrayList<String> listOfAgentName = new ArrayList<String>();
// Initialise the Framwork.
public synchronized static void initFramework() {
env = new Environment();
// Create the BDI Engine.
//SimpleBDIEngine.init();
Console.frmLogMSG("Environment is created...");
}
public synchronized static void loadEnvironmentData() {
if (env != null)
env.loadData();
}
public static Environment getCurrentEnv() {
return env;
}
/**
*
* @return the current state of the simulation see @see SimProcessState
*/
public static SimProcessState getCurrentSimProcessState() {
if (SimulationFramework.getCurrentEnv().T == SimulationFramework
.getCurrentEnv().max_T) {
currentSimProcessState = SimProcessState.Stoped;
}
return currentSimProcessState;
}
public static void setCurrentSimProcessState(SimProcessState sps) {
currentSimProcessState = sps;
}
/**
*
* @param sci
* @throws Exception
*/
public static void AddSource(SourceConfigInfo sci) throws Exception {
SimulationFramework.sourcesInfo.put(sci.getSourceName(), sci);
// Creating the agent which will control it.
StartContainer.createAgent(sci.getSourceName(),
"com.masim.emissionAgents.PointSource");
}
public static void addUncontrolledSource(int nbr, PollutantType pType, double er){
double i = 1;
int x, y;
while (i <= nbr) {
SourceConfigInfo sci = new SourceConfigInfo();
sci.setSourceName("US" + i + pType);
sci.setUncontrolled(true);
sci.setEmisionRate(er );
sci.setSuspended(false);
sci.setMaxEmissionRate(er);
sci.setMinEmissionRate((er/2) / 25);
x = r.nextInt(600);
y = r.nextInt(500);
sci.setP(pType);
Location l = new Location(x, y);
sci.setGeoLocation(l);
sci.setIsLocatedOnCbox(null);
sci.setType(SourceTYpe.Point);
try {
SimulationFramework.AddSource(sci);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
i++;
}
}
public static void StartSimulation() {
SimulationFramework.setCurrentSimProcessState(SimProcessState.Started);
if (SimulationFramework.usePredifinedScenario) {
nbrControlledSRCS=nbrCOXSRCS+nbrNOXSRCS+nbrPM10SRCS+nbrSOXSRCS;
ADDsampleSources(nbrCOXSRCS, PollutantType.COx, EmessionRate,false);
ADDsampleSources(nbrNOXSRCS, PollutantType.NOx, EmessionRate / 2,false);
ADDsampleSources(nbrPM10SRCS, PollutantType.PM10, EmessionRate,false);
ADDsampleSources(nbrSOXSRCS, PollutantType.SOx, EmessionRate,false);
//addUncontrolledSource(nbrUncontrolledSRCSPM10, PollutantType.PM10, 10000);
//addUncontrolledSource(nbrUncontrolledSRCSCOX, PollutantType.COx, 4000);
//addUncontrolledSource(nbrUncontrolledSRCSNOX, PollutantType.NOx, 4000);
//addUncontrolledSource(nbrUncontrolledSRCSSOX, PollutantType.SOx, 4000);
}
setMaxPollutionLevelByAirquaityIndex();
Console.frmLogMSG(" Number of controlled sources :"+nbrControlledSRCS);
Console.frmLogMSG(" Number of uncontrolled sources:"+nbrUncontrolledSRCSPM10);
Console.frmLogMSG(" Number Of PM10 sources :"
+ SimulationFramework.nbrPM10SRCS);
Console.frmLogMSG(" Number Of COX sources :"
+ SimulationFramework.nbrCOXSRCS);
Console.frmLogMSG(" Number Of SOX sources :"
+ SimulationFramework.nbrSOXSRCS);
Console.frmLogMSG(" Number Of NOX sources :"
+ SimulationFramework.nbrNOXSRCS);
Console.frmLogMSG(" Simulation Time :"
+ SimulationFramework.getCurrentEnv().getMax_T());
Console.frmLogMSG("Penaly is " + SimulationFramework.withPenalities);
Console.frmLogMSG("Cumulative Penaly is " + SimulationFramework.cumulatifPenalty);
Console.frmLogMSG("MAX Emission Rate is: "
+ SimulationFramework.EmessionRate);
Console.frmLogMSG("Use Gaussian Dispersion Model : "
+ SimulationFramework.useGausianModel);
Console.frmLogMSG("Global Goal is Air Quality index =: "
+ SimulationFramework.AirQualityIndexGoal);
Console.frmLogMSG("<START_LOCAL_SIM> SENT");
}
/**
* Stop the negotiation, the simulation is ended.
*/
public static void StopNegociationbetWeenEmissionSources() {
if (SimulationFramework.currentStrategy == CooperationStrategy.NEGOTIATION)
PrisonnerDelimmaUtl.StopGame();
// else (SimulationFramework.currentStrategy==CooperationStrategy.NAIVE)
}
/**
* Automatically add sample source.
*/
public static void ADDsampleSources(int nbr, PollutantType pType, double er, boolean uncontrolled) {
double i = 1;
int x, y;
while (i <= nbr) {
SourceConfigInfo sci = new SourceConfigInfo();
sci.setSourceName("S" + i + pType);
sci.setUncontrolled(false);
sci.setEmisionRate(er / 2);
sci.setSuspended(false);
sci.setMaxEmissionRate(er);
sci.setMinEmissionRate((er/2) / 25);
x = r.nextInt(600);
y = r.nextInt(500);
sci.setP(pType);
Location l = new Location(x, y);
sci.setGeoLocation(l);
sci.setIsLocatedOnCbox(null);
sci.setType(SourceTYpe.Point);
try {
SimulationFramework.AddSource(sci);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
i++;
}
}
/**
* Kill all agents controlling a sources of all pollutant type.
*/
public static void KillSourceAgent() {
KillSourceAgents(nbrCOXSRCS, PollutantType.COx);
KillSourceAgents(nbrNOXSRCS, PollutantType.NOx);
KillSourceAgents(nbrPM10SRCS, PollutantType.PM10);
KillSourceAgents(nbrSOXSRCS, PollutantType.SOx);
}
/**
* Kill sources controllers agents.
*/
public static void KillSourceAgents(int nbr, PollutantType pType) {
double i = 1;
while (i <= nbr) {
try {
StartContainer.currentAgentContainer.getAgent("S" + i + pType)
.kill();
} catch (StaleProxyException e) {
// TODO Auto-generated catch block
Console.frmLogMSG(e.getMessage());
e.printStackTrace();
} catch (ControllerException e) {
// TODO Auto-generated catch block
Console.frmLogMSG(e.getMessage());
e.printStackTrace();
}
i++;
}
}
public static void KillGameManagerAgent() {
try {
StartContainer.currentAgentContainer.getAgent("GameManager").kill();
} catch (Exception e) {
Console.frmLogMSG(e.getMessage());
e.printStackTrace();
}
}
public static void setMaxPollutionLevelByAirquaityIndex() {
maxValPM10 = MachineLearningUtls.PM10[AirQualityIndexGoal - 1][1];
maxValSOx = MachineLearningUtls.SO2VAL[AirQualityIndexGoal - 1][1];
maxValCOx = 10;
maxValNOx = MachineLearningUtls.NO2VAL[AirQualityIndexGoal - 1][1];
maxValO3 = MachineLearningUtls.O3[AirQualityIndexGoal - 1][1];
;
}
public static void setScenario() {
switch (SimulationFramework.selectedScenario) {
case 0: {
numberOfSource = 40;
nbrPM10SRCS = 10;
nbrSOXSRCS = 10;
nbrCOXSRCS = 10;
nbrNOXSRCS = 10;
break;
}
case 1: {
numberOfSource = 80;
nbrPM10SRCS = 20;
nbrSOXSRCS = 20;
nbrCOXSRCS = 20;
nbrNOXSRCS = 20;
break;
}
case 2: {
numberOfSource = 160;
nbrPM10SRCS = 40;
nbrSOXSRCS = 40;
nbrCOXSRCS = 40;
nbrNOXSRCS = 40;
break;
}
case 3: {
numberOfSource = 240;
nbrPM10SRCS = 60;
nbrSOXSRCS = 60;
nbrCOXSRCS = 60;
nbrNOXSRCS = 60;
break;
}
case 4: {
numberOfSource = 400;
nbrPM10SRCS = 100;
nbrSOXSRCS = 100;
nbrCOXSRCS = 100;
nbrNOXSRCS = 100;
break;
}
case 5: {
numberOfSource = 600;
nbrPM10SRCS = 150;
nbrSOXSRCS = 150;
nbrCOXSRCS = 150;
nbrNOXSRCS = 150;
break;
}
case 6: {
numberOfSource = 1000;
nbrPM10SRCS = 250;
nbrSOXSRCS = 250;
nbrCOXSRCS = 250;
nbrNOXSRCS = 250;
break;
}
case 7: {
numberOfSource = 2000;
nbrPM10SRCS = 500;
nbrSOXSRCS = 500;
nbrCOXSRCS = 500;
nbrNOXSRCS = 500;
break;
}
default:
break;
}
}
public static void save() {
try {
String currentDir = System.getProperty("user.dir");
java.util.Date d = new Date();
String fileName = currentDir + "\\results-Simulation-Session-N"
+ SimulationFramework.currentStrategy + "PenaltyIS"
+ SimulationFramework.withPenalities
+ System.currentTimeMillis() + ".csv";
PrintWriter writer = new PrintWriter(fileName, "UTF-8");
writer.println("------------------------------------------");
writer.println(" Simulation results.");
writer.println(" Number Of PM10 sources :"
+ SimulationFramework.nbrPM10SRCS);
writer.println(" Number Of COX sources :"
+ SimulationFramework.nbrCOXSRCS);
writer.println(" Number Of SOX sources :"
+ SimulationFramework.nbrSOXSRCS);
writer.println(" Number Of NOX sources :"
+ SimulationFramework.nbrNOXSRCS);
writer.println(" Simulation Time :"
+ SimulationFramework.getCurrentEnv().getMax_T());
writer.print("Penaly is " + SimulationFramework.withPenalities);
writer.print("MAX Emission Rate is: "
+ SimulationFramework.EmessionRate);
writer.print("Use Gaussian Dispersion Model : "
+ SimulationFramework.useGausianModel);
writer.print("Global Goal is Air Quality index =: "
+ SimulationFramework.AirQualityIndexGoal);
writer.println("------------------------------------------");
writer.println(Console.current.txtLog.toString());
writer.close();
Console.frmLogMSG("Results are saved in " + fileName);
}
catch (IOException exp) {
Console.frmLogMSG(exp.getMessage());
System.out.print(exp.getMessage());
}
}
}