/*
* Datei: PanicMaster.java
* Autor(en): Lukas König, Daniel Pathmaperuma
* Java-Version: 6.0
* Erstellt: 2010_10_28
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* =========================================================================
* Copyright note from phys2D:
*
* Phys2D - a 2D physics engine based on the work of Erin Catto.
*
* This source is also provided under the terms of the BSD License.
*
* Copyright (c) 2006, Phys2D
* All rights reserved.
*
* Where the licenses differ from each other, the more restrictive license
* version has to be concerned.
* =========================================================================
*
* In a nutshell (for the EAS framework)
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.users.demos.panicSimulation;
import java.io.File;
import java.util.List;
import eas.plugins.masterScheduler.AbstractDefaultMaster;
import eas.plugins.standard.visualization.chartPlugin.ChartEvent;
import eas.plugins.standard.visualization.chartPlugin.ChartEventStoreAsPDF;
import eas.simulation.ConstantsSimulation;
import eas.simulation.Wink;
import eas.simulation.agent.AbstractAgent;
import eas.simulation.event.EASEvent;
import eas.simulation.spatial.sim2D.physicalSimulation.physicsEngine.net.phys2d.math.Vector2f;
import eas.simulation.spatial.sim2D.physicalSimulation.standardScenes.PanicScene;
import eas.simulation.spatial.sim2D.physicalSimulation.standardScenes.PanicSceneWithColumn;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;
/**
* @author Lukas König
*
*/
public class PanicMaster extends AbstractDefaultMaster<PanicEnvironment> {
/**
*
*/
private static final long serialVersionUID = 1217840944493385278L;
@Override
public List<SingleParameter> getParameters() {
final List<SingleParameter> list = super.getParameters();
list.add(new SingleParameter(
"Column?",
Datatypes.BOOLEAN,
false,
"",
id().toUpperCase()));
list.add(new SingleParameter(
"StoreChartsAsPDF?",
Datatypes.BOOLEAN,
false,
"Stores the charts as pdf every 100 cycles.",
id().toUpperCase()));
return list;
}
@Override
public PanicEnvironment[] generateRunnables(final ParCollection params) {
final PanicEnvironment world = new PanicEnvironment(
0,
params,
new Vector2f(0, 0),
1);
final PanicEnvironment worldWithColumn = new PanicEnvironment(
0,
params,
new Vector2f(0, 0),
1);
final PanicScene panicScene = new PanicScene("Scene of Panic", params);
final PanicSceneWithColumn panicSceneWC = new PanicSceneWithColumn("Scene of Panic", params);
world.addScene(panicScene);
world.reset(55);
worldWithColumn.addScene(panicSceneWC);
worldWithColumn.reset(55);
world.handleEvent(new EASEvent(" "), null);
worldWithColumn.handleEvent(new EASEvent(" "), null);
if (params.getParValueBoolean("Column?")) {
return new PanicEnvironment[] { worldWithColumn };
} else {
return new PanicEnvironment[] { world };
}
}
@Override
public void runDuringSimulation(PanicEnvironment umg, Wink simZyk,
ParCollection params) {
super.runDuringSimulation(umg, simZyk, params);
// Generate chart.
if (simZyk.getLastTick() % 10 == 0) {
double force = 0;
double deadGuys = 0;
double maxForce = 0;
double minForce = Double.MAX_VALUE;
double rescuedGuys = 0;
// Collect chart data.
for (AbstractAgent<?> a : umg.getAgents()) {
try {
PanicAgent pa = (PanicAgent) a;
force += umg.getOutsideForce(pa.id());
if (umg.getOutsideForce(pa.id()) > maxForce) {
maxForce = umg.getOutsideForce(pa.id());
}
if (umg.getOutsideForce(pa.id()) < minForce && !pa.isDead()) {
minForce = umg.getOutsideForce(pa.id());
}
if (pa.isDead()) {
deadGuys++;
}
if (umg.getAgentPosition(pa.id()).x > 155) {
if (!pa.isDead()) {
rescuedGuys++;
}
}
} catch (Exception e) {
}
}
// Create chart events.
ChartEvent event2 = new ChartEvent("Panic Chart 1", "Max. Pressure", maxForce);
ChartEvent event1 = new ChartEvent("Panic Chart 1", "Average Preasure", force / umg.getAgents().size());
ChartEvent event3 = new ChartEvent("Panic Chart 1", "Min. Pressure", minForce);
ChartEvent event4 = new ChartEvent("Panic Chart 2", "Rescued guys", rescuedGuys);
ChartEvent event5 = new ChartEvent("Panic Chart 2", "Dead guys", deadGuys);
event1.setyAxisLabel("Pressure");
event4.setyAxisLabel("Number of agents");
event4.setDrawSmoothSplinesInLineCharts(true);
event5.setDrawSmoothSplinesInLineCharts(true);
umg.getSimTime().broadcastEvent(event1);
umg.getSimTime().broadcastEvent(event2);
umg.getSimTime().broadcastEvent(event3);
umg.getSimTime().broadcastEvent(event4);
umg.getSimTime().broadcastEvent(event5);
// Store charts as pdf.
if (simZyk.getLastTick() % 100 == 0 && params.getParValueBoolean("StoreChartsAsPDF?")) {
umg.getSimTime().broadcastEvent(new ChartEventStoreAsPDF("Panic Chart 1", new File("panic1-" + simZyk.getLastTick() + ".pdf")));
umg.getSimTime().broadcastEvent(new ChartEventStoreAsPDF("Panic Chart 2", new File("panic2-" + simZyk.getLastTick() + ".pdf")));
}
}
}
@Override
public String id() {
return ConstantsSimulation.DEFAULT_MASTER_SCHEDULER_ID + "-panic";
}
}