/*
* File name: ConquererMaster.java (package eas.users.lukas.demos.conquerer)
* Author(s): Lukas König
* Java version: 6.0
* Generation date: 19.03.2012 (16:38:03)
*
* (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.
*
* In a nutshell
* -------------
* 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.wsc2012.rescuer;
import java.awt.Color;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.RadialGradientPaint;
import java.awt.geom.Point2D;
import java.util.List;
import java.util.Random;
import org.jfree.data.statistics.HistogramType;
import eas.math.geometry.Vector2D;
import eas.plugins.masterScheduler.AbstractDefaultMaster;
import eas.plugins.standard.visualization.chartPlugin.ChartEvent;
import eas.simulation.ConstantsSimulation;
import eas.simulation.Wink;
import eas.simulation.agent.GenericSensor;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.simulation.spatial.sim2D.standardScenes.ObstacleScene;
import eas.simulation.spatial.sim2D.standardScenes.Scene2D;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;
/**
* @author Lukas König
*/
public class RescuerMaster extends AbstractDefaultMaster<RescuerContinousEnv> {
/**
*
*/
private static final long serialVersionUID = 8010746578368816169L;
@Override
public List<SingleParameter> getParameters() {
List<SingleParameter> list = super.getParameters();
list.add(new SingleParameter("NumberOfInjured", Datatypes.INTEGER, 10, "", this.id().toUpperCase()));
list.add(new SingleParameter("NumberOfRescuing", Datatypes.INTEGER, 10, "", this.id().toUpperCase()));
return list;
}
@Override
public RescuerContinousEnv[] generateRunnables(ParCollection params) {
RescuerContinousEnv runnable = new RescuerContinousEnv(0, params);
double agentSize = 1;
Random rand = new Random(params.getSeed());
Scene2D<AbstractAgent2D<?>> scene1 = new ObstacleScene(params);
scene1.fitInBoundingRectangle(new Vector2D(-20, -20), new Vector2D(80, 20), Scene2D.FITTING_MODE_FIT_UPPER_LEFT);
runnable.addScene(scene1);
Scene2D<AbstractAgent2D<?>> scene2 = new ObstacleScene(params);
scene2.fitInBoundingRectangle(new Vector2D(-5, -5), new Vector2D(5, 5), Scene2D.FITTING_MODE_FIT_UPPER_LEFT);
runnable.addScene(scene2);
runnable.removeAgent(7);
double factor = 12;
int startSize = runnable.getAgents().size();
while (runnable.getAgents().size() < 4 + startSize) {
runnable.addCollidingAgent(new RockAgent(0, runnable, params, rand), new Vector2D((rand.nextDouble() - 0.5) * factor, (rand.nextDouble() - 0.5) * factor), 0);
}
startSize = runnable.getAgents().size();
while (runnable.getAgents().size() < params.getParValueInt("NumberOfRescuing") + startSize) {
Vector2D pos = new Vector2D(rand.nextDouble() * 5 - 10, rand.nextDouble() * 5 - 10);
AbstractDesasterAgent agent = new RescuerAgent(runnable.getAgents().size(), runnable, params, rand);
agent.implantBrain(new RescueBrain(agent));
agent.addSensor(new GenericSensor<String, RescuerContinousEnv, AbstractDesasterAgent>() {
/**
*
*/
private static final long serialVersionUID = -6371084161247712072L;
@Override
public String sense(RescuerContinousEnv env,
AbstractDesasterAgent agent) {
return (float) env.getGradientAtCoordinate(env.getAgentPosition(agent.id())).x + "/" + (float) env.getGradientAtCoordinate(env.getAgentPosition(agent.id())).y;
}
@Override
public String id() {
return "HeightGradientSensor";
}
});
runnable.addCollidingAgent(agent, pos, rand.nextDouble() * 360, new Vector2D(agentSize, agentSize));
}
startSize = runnable.getAgents().size();
while (runnable.getAgents().size() < params.getParValueInt("NumberOfInjured") + startSize) {
Vector2D pos = new Vector2D(rand.nextGaussian(), rand.nextGaussian());
AbstractDesasterAgent agent = new InjuredAgent(runnable.getAgents().size(), runnable, params, rand);
agent.implantBrain(new RescueBrain(agent));
runnable.addCollidingAgent(agent, pos, rand.nextDouble() * 360, new Vector2D(agentSize, agentSize));
}
return new RescuerContinousEnv[] {runnable};
}
@Override
public String id() {
return ConstantsSimulation.DEFAULT_MASTER_SCHEDULER_ID + "-WSC2012-ContinousConquerer";
}
double i = 0;
@Override
public synchronized void runDuringSimulation(
RescuerContinousEnv env,
Wink simZyk,
ParCollection params) {
env.step(simZyk);
if (simZyk.getLastTick() % 1 == 0) {
ChartEvent event;
int i = 0;
double[] values = new double[params.getParValueInt("NumberOfRescuing")];
for (AbstractAgent2D<?> agent : env.getAgents()) {
try {
values[i] = env.getAgentPosition(((RescuerAgent) agent).id()).distance(Vector2D.NULL_VECTOR);
i++;
} catch (Exception e) {}
}
event = new ChartEvent(
"Distance histogram",
"Series1",
values,
25);
event.setHistType(HistogramType.RELATIVE_FREQUENCY);
event.setxAxisLabel("Distance to injured");
event.setyAxisLabel("Number of Agents");
env.getSimTime().broadcastEvent(event);
i = 0;
double[] values2 = new double[values.length];
for (AbstractAgent2D<?> agent : env.getAgents()) {
try {
values2[i] = env.getAgentAngle(agent.id()) / 36;
i++;
} catch (Exception e) {}
}
event = new ChartEvent(
"Distance histogram",
"Series2",
values2,
25);
event.setHistType(HistogramType.RELATIVE_FREQUENCY);
event.setxAxisLabel("Distance to injured");
event.setyAxisLabel("Number of Agents");
env.getSimTime().broadcastEvent(event);
event = new ChartEvent(
"PIE-CHART",
new double[] {i + new Random().nextDouble(), i * 2 + new Random().nextDouble(), i * 4 + new Random().nextDouble()},
new String[] {"Point 1", "Point 2", "Point 3"});
// event.setPaintChart3D(true);
env.getSimTime().broadcastEvent(event);
event = new ChartEvent(
"PIE-CHART",
new double[] {2},
new String[] {"Point 4"});
i += 1;
event.setExplodedSegmentPie("Point 2", 0.3);
RadialGradientPaint p1 = new RadialGradientPaint(
new Point2D.Float(0, 0),
80,
new float[] {0.3f, 1},
new Color[] {Color.red, Color.yellow},
CycleMethod.REFLECT);
event.setSegmentPaint("Point 2", p1);
env.getSimTime().broadcastEvent(event);
}
for (AbstractAgent2D<?> agent : env.getAgents()) {
try {
((InjuredAgent) agent).getPars();
} catch (Exception e) {
agent.step(simZyk);
}
}
}
// public static void main(String[] args) {
// Matrix m = new Matrix(25, 25);
// int h = 1;
//
// int i = 0, j = 0;
// int richtung = 0;
// int links = 0;
// int rechts = m.getColumnCount() - 1;
// int oben = 0;
// int unten = m.getRowCount() - 1;
//
// while (i != 12 || j != 12) {
// m.set(i, j, h);
// h ++;
//
// switch (richtung) {
// case 0:
// if (i < rechts) {
// i++;
// } else {
// richtung = (richtung + 1) % 4;
// rechts--;
// j++;
// }
// break;
// case 1:
// if (j < unten) {
// j++;
// } else {
// richtung = (richtung + 1) % 4;
// unten--;
// i--;
// }
// break;
// case 2:
// if (i > links) {
// i--;
// } else {
// richtung = (richtung + 1) % 4;
// links++;
// j--;
// }
// break;
// case 3:
// if (j > oben) {
// j--;
// } else {
// richtung = (richtung + 1) % 4;
// oben++;
// i++;
// }
// break;
// default:
// break;
// }
// }
//
// m.set(12, 12, h);
//
//// for (double i = 0; i < m.getColumnCount(); i++) {
//// for (double j = 0; j < m.getRowCount(); j++) {
//// m.set((int) i,
//// (int) j,
//// h++);
////// -(i * i - m.getColumnCount() * i) + -(j * j - m.getRowCount() * j));
//// }
//// }
//
// System.out.println("heightProfile " + m.toString());
// m.storeMatrix(new File("sharedDirectory/heightMatrix.txt"), null);
// }
}