Facebook
From das, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 218
  1. import Agents.AAgent;
  2. import Agents.Agents;
  3. import PropertiesOwn.AProperties;
  4. import javafx.util.Pair;
  5. import Agents.Meeting;
  6.  
  7. import java.util.Random;
  8.  
  9. public class Simulation {
  10.     private AProperties aProperties;
  11.     private Agents agents;
  12.     private Random generator;
  13.     String output=null;
  14.     Simulation (AProperties aProperties, Agents agents, Random generator) {
  15.         this.agents=agents;
  16.         this.aProperties=aProperties;
  17.         this.generator=generator;
  18.     }
  19.  
  20.     private boolean random (double probability) {
  21.         Double r = generator.nextDouble();
  22.         //System.out.println(r);
  23.         return  r<=probability;
  24.     }
  25.  
  26.     private int randomInt (int a, int b) {
  27.         return generator.nextInt(b-a+1)+a;
  28.     }
  29.  
  30.     private void simulateDay (int day) {
  31.         //System.out.println("dzien: " + day);
  32.         AAgent ne;
  33.         //System.out.println("drugi forEach");
  34.         for (AAgent obj : agents) {
  35.             //System.out.println(obj);
  36.             if (!obj.isDeath() && obj.isSick() && random(aProperties.getŚmiertelność()))
  37.                 obj.die();
  38.             if (!obj.isDeath() && obj.isSick() && random(aProperties.getPrawdWyzdrowienia()))
  39.                 obj.getWell();
  40.             if (!obj.isDeath() && day < aProperties.getLiczbaDni()) {
  41.                 //System.out.println("jestem: " + aProperties.getPrawdSpotkania());
  42.                 while (random(aProperties.getPrawdSpotkania())) {
  43.                     ne = obj.meet();
  44.                     if (ne == null) break;
  45.                     //System.out.println("spotkanie " + ne.getId() + " " + obj.getId());
  46.                     obj.addMeeting(randomInt(day + 1, aProperties.getLiczbaDni()), ne);
  47.                 }
  48.             }
  49.             Meeting p;
  50.             while (!obj.meetings.isEmpty() && obj.meetings.peek().getDay() <= day) {
  51.                 p = obj.meetings.poll();
  52.                 if (!obj.isDeath() && !p.getAgent().isDeath())
  53.                     Agents.meeting(obj, p.getAgent(), random(aProperties.getPrawdZarażenia()));
  54.             }
  55.  
  56.  
  57.         }
  58.     }
  59.  
  60.     public void simulation () {
  61.         StringBuilder s = new StringBuilder();
  62.         for (int i=1; i<=aProperties.getLiczbaDni(); i++) {
  63.             simulateDay(i);
  64.             s.append(agents.showState());
  65.         }
  66.         output = s.toString();
  67.     }
  68.  
  69.     @Override
  70.     public String toString() {
  71.         if (output == null)
  72.             simulation();
  73.         return output;
  74.     }
  75. }
  76.  
  77.  
  78.  
  79. import Agents.Agents;
  80. import Agents.Types;
  81. import Agents.Sociable;
  82. import Agents.Typical;
  83. import PropertiesOwn.AProperties;
  84. import PropertiesOwn.DefaultProperties;
  85. import PropertiesOwn.SimulationConf;
  86.  
  87. import java.io.FileNotFoundException;
  88. import java.io.PrintStream;
  89. import java.io.UnsupportedEncodingException;
  90. import java.util.Random;
  91.  
  92. public class Symulacja {
  93.  
  94.     private static AProperties getConfiguration() {
  95.         String defaultPropertiesPath = System.getProperty("user.dir")+"\\default.properties";
  96.         String simulationConfPath = System.getProperty("user.dir")+"\\simulation-conf.xml";
  97.         DefaultProperties dp = new DefaultProperties();
  98.         dp.read(defaultPropertiesPath);
  99.         SimulationConf sc = new SimulationConf();
  100.         sc.read(simulationConfPath);
  101.         dp.mergeProperties(sc);
  102.         dp.checkProperties();
  103.         return dp;
  104.     }
  105.  
  106.     private static Types initTypes (Random generator) {
  107.         Types types = new Types(generator);
  108.         types.addNewAgent(new Sociable());
  109.         types.addNewAgent(new Typical());
  110.         return types;
  111.     }
  112.  
  113.     private static void output (PrintStream out, AProperties properties, Agents agents, Simulation simulation) {
  114.         out.println("# twoje wyniki powinny zawierać te komentarze");
  115.         out.println(properties);
  116.         out.println("# agenci jako: id typ lub id* typ dla chorego");
  117.         out.println(agents.getBeginStateAgents());
  118.         out.println("# graf");
  119.         out.println(agents.getBeginStateGraph());
  120.         out.println("# liczność w kolejnych dniach");
  121.         out.println(simulation);
  122.         out.close();
  123.     }
  124.  
  125.     public static void main (String[] argv) throws FileNotFoundException, UnsupportedEncodingException {
  126.         AProperties properties = getConfiguration();
  127.         Random generator = new Random(properties.getSeed());
  128.         Types types = initTypes(generator);
  129.         Agents agents = new Agents(properties.getLiczbaAgentów(), properties.getŚrZnajomych(), generator, types);
  130.         Simulation simulation = new Simulation(properties, agents, generator);
  131.         PrintStream out = new PrintStream(properties.getPlikZRaportem(), "UTF-8");
  132.         output(out, properties, agents, simulation);
  133.         //output(System.out, properties, agents, simulation);
  134.     }
  135. }
  136.