- import Agents.AAgent;
- import Agents.Agents;
- import PropertiesOwn.AProperties;
- import javafx.util.Pair;
- import Agents.Meeting;
- import java.util.Random;
- public class Simulation {
- private AProperties aProperties;
- private Agents agents;
- private Random generator;
- String output=null;
- Simulation (AProperties aProperties, Agents agents, Random generator) {
- this.agents=agents;
- this.aProperties=aProperties;
- this.generator=generator;
- }
- private boolean random (double probability) {
- Double r = generator.nextDouble();
- //System.out.println(r);
- return r<=probability;
- }
- private int randomInt (int a, int b) {
- return generator.nextInt(b-a+1)+a;
- }
- private void simulateDay (int day) {
- //System.out.println("dzien: " + day);
- AAgent ne;
- //System.out.println("drugi forEach");
- for (AAgent obj : agents) {
- //System.out.println(obj);
- if (!obj.isDeath() && obj.isSick() && random(aProperties.getŚmiertelność()))
- obj.die();
- if (!obj.isDeath() && obj.isSick() && random(aProperties.getPrawdWyzdrowienia()))
- obj.getWell();
- if (!obj.isDeath() && day < aProperties.getLiczbaDni()) {
- //System.out.println("jestem: " + aProperties.getPrawdSpotkania());
- while (random(aProperties.getPrawdSpotkania())) {
- ne = obj.meet();
- if (ne == null) break;
- //System.out.println("spotkanie " + ne.getId() + " " + obj.getId());
- obj.addMeeting(randomInt(day + 1, aProperties.getLiczbaDni()), ne);
- }
- }
- Meeting p;
- while (!obj.meetings.isEmpty() && obj.meetings.peek().getDay() <= day) {
- p = obj.meetings.poll();
- if (!obj.isDeath() && !p.getAgent().isDeath())
- Agents.meeting(obj, p.getAgent(), random(aProperties.getPrawdZarażenia()));
- }
- }
- }
- public void simulation () {
- StringBuilder s = new StringBuilder();
- for (int i=1; i<=aProperties.getLiczbaDni(); i++) {
- simulateDay(i);
- s.append(agents.showState());
- }
- output = s.toString();
- }
- @Override
- public String toString() {
- if (output == null)
- simulation();
- return output;
- }
- }
- import Agents.Agents;
- import Agents.Types;
- import Agents.Sociable;
- import Agents.Typical;
- import PropertiesOwn.AProperties;
- import PropertiesOwn.DefaultProperties;
- import PropertiesOwn.SimulationConf;
- import java.io.FileNotFoundException;
- import java.io.PrintStream;
- import java.io.UnsupportedEncodingException;
- import java.util.Random;
- public class Symulacja {
- private static AProperties getConfiguration() {
- String defaultPropertiesPath = System.getProperty("user.dir")+"\\default.properties";
- String simulationConfPath = System.getProperty("user.dir")+"\\simulation-conf.xml";
- DefaultProperties dp = new DefaultProperties();
- dp.read(defaultPropertiesPath);
- SimulationConf sc = new SimulationConf();
- sc.read(simulationConfPath);
- dp.mergeProperties(sc);
- dp.checkProperties();
- return dp;
- }
- private static Types initTypes (Random generator) {
- Types types = new Types(generator);
- types.addNewAgent(new Sociable());
- types.addNewAgent(new Typical());
- return types;
- }
- private static void output (PrintStream out, AProperties properties, Agents agents, Simulation simulation) {
- out.println("# twoje wyniki powinny zawierać te komentarze");
- out.println(properties);
- out.println("# agenci jako: id typ lub id* typ dla chorego");
- out.println(agents.getBeginStateAgents());
- out.println("# graf");
- out.println(agents.getBeginStateGraph());
- out.println("# liczność w kolejnych dniach");
- out.println(simulation);
- out.close();
- }
- public static void main (String[] argv) throws FileNotFoundException, UnsupportedEncodingException {
- AProperties properties = getConfiguration();
- Random generator = new Random(properties.getSeed());
- Types types = initTypes(generator);
- Agents agents = new Agents(properties.getLiczbaAgentów(), properties.getŚrZnajomych(), generator, types);
- Simulation simulation = new Simulation(properties, agents, generator);
- PrintStream out = new PrintStream(properties.getPlikZRaportem(), "UTF-8");
- output(out, properties, agents, simulation);
- //output(System.out, properties, agents, simulation);
- }
- }