Commit bfdb1f53 authored by Loris Croce's avatar Loris Croce
Browse files

Parameters config path file

parent e9b1ad76
Pipeline #37203 passed with stages
in 6 minutes and 1 second
...@@ -161,7 +161,7 @@ public class Generator { ...@@ -161,7 +161,7 @@ public class Generator {
dynamics.add(new ProductionDynamics()); dynamics.add(new ProductionDynamics());
dynamics.add(new MarketDynamics()); dynamics.add(new MarketDynamics());
dynamics.add(new FlowDynamics()); dynamics.add(new FlowDynamics());
Simulator simulator = new Simulator(new Parameters(0, dynamics), farmers); Simulator simulator = new Simulator(new Parameters(0, dynamics, "data/in/config/"), farmers);
simulator.serializeXML(Files.newOutputStream(Paths.get("data/input/generator/simulator.xml"))); simulator.serializeXML(Files.newOutputStream(Paths.get("data/input/generator/simulator.xml")));
System.out.println("# patches: " + simulator.geoAreas().get(0).getPatches().size()); System.out.println("# patches: " + simulator.geoAreas().get(0).getPatches().size());
System.out.println("# farmers: " + simulator.getFarmers().size()); System.out.println("# farmers: " + simulator.getFarmers().size());
......
...@@ -7,11 +7,16 @@ public class Parameters { ...@@ -7,11 +7,16 @@ public class Parameters {
private final int rngSeedIndex; private final int rngSeedIndex;
private List<Dynamics> dynamics; private List<Dynamics> dynamics;
//TODO add config file index private String configPath;
public Parameters(int rngSeedIndex, List<Dynamics> dynamics) { public Parameters(int rngSeedIndex, List<Dynamics> dynamics, String configPath) {
this.rngSeedIndex = rngSeedIndex; this.rngSeedIndex = rngSeedIndex;
this.dynamics = dynamics; this.dynamics = dynamics;
this.configPath = configPath;
for (Dynamics dynamic : dynamics) {
dynamic.setConfigPath(configPath);
dynamic.initParameters();
}
} }
public int getRngSeedIndex() { public int getRngSeedIndex() {
......
...@@ -25,4 +25,10 @@ public abstract class Dynamics { ...@@ -25,4 +25,10 @@ public abstract class Dynamics {
public String getTimeStamp() { public String getTimeStamp() {
return timeStamp; return timeStamp;
} }
public void setConfigPath(String configPath) {
this.configPath = configPath;
}
public abstract void initParameters();
} }
...@@ -95,4 +95,9 @@ public class FlowDynamics extends Dynamics { ...@@ -95,4 +95,9 @@ public class FlowDynamics extends Dynamics {
public void initLogger() { public void initLogger() {
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp); logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp);
} }
@Override
public void initParameters() {
}
} }
...@@ -29,6 +29,11 @@ public class MarketDynamics extends Dynamics { ...@@ -29,6 +29,11 @@ public class MarketDynamics extends Dynamics {
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp); logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp);
} }
@Override
public void initParameters() {
}
@Override @Override
public void process(SimulationContext context, List<GeoArea> geoAreas, List<Farmer> farmers, Flow global, public void process(SimulationContext context, List<GeoArea> geoAreas, List<Farmer> farmers, Flow global,
Parameters parameters, TimeUnit timeUnit) { Parameters parameters, TimeUnit timeUnit) {
......
...@@ -28,6 +28,8 @@ public class PopulationDynamics extends Dynamics { ...@@ -28,6 +28,8 @@ public class PopulationDynamics extends Dynamics {
private double P; private double P;
private double PATCH_CHANGE_CHANCE; private double PATCH_CHANGE_CHANCE;
private String transitionFile;
//TODO REPLACE by transition matrix (cultural practice is determined by product tag ?) //TODO REPLACE by transition matrix (cultural practice is determined by product tag ?)
private final CulturalPractice transitionPractice = CulturalPractice.O; private final CulturalPractice transitionPractice = CulturalPractice.O;
private Product transitionProduct = new Product("Soja", Unit.Kg); private Product transitionProduct = new Product("Soja", Unit.Kg);
...@@ -36,11 +38,14 @@ public class PopulationDynamics extends Dynamics { ...@@ -36,11 +38,14 @@ public class PopulationDynamics extends Dynamics {
private Map<String, Map<String, Double>> transitionMatrix; private Map<String, Map<String, Double>> transitionMatrix;
public PopulationDynamics() { public PopulationDynamics() {
configPath = "config/transitions.xml"; }
public void initParameters() {
transitionFile = "transitions.xml";
initLogger(); initLogger();
Yaml yaml = new Yaml(); Yaml yaml = new Yaml();
try { try {
InputStream inputStream = new FileInputStream("config/population.yaml"); InputStream inputStream = new FileInputStream(configPath + "population.yaml");
Please register or sign in to reply
Map<String, Object> config = yaml.load(inputStream); Map<String, Object> config = yaml.load(inputStream);
RETIREMENT_AGE = (int) config.get("retirement_age"); RETIREMENT_AGE = (int) config.get("retirement_age");
MIN_AGE_TAKEOVER = (int) config.get("min_age_takeover"); MIN_AGE_TAKEOVER = (int) config.get("min_age_takeover");
...@@ -54,7 +59,7 @@ public class PopulationDynamics extends Dynamics { ...@@ -54,7 +59,7 @@ public class PopulationDynamics extends Dynamics {
XStream xStream = new XStream(); XStream xStream = new XStream();
XStream.setupDefaultSecurity(xStream); XStream.setupDefaultSecurity(xStream);
xStream.allowTypesByRegExp(new String[]{".*"}); xStream.allowTypesByRegExp(new String[]{".*"});
FileInputStream fileInputStream = new FileInputStream(configPath); FileInputStream fileInputStream = new FileInputStream(configPath + transitionFile);
transitionMatrix = (Map<String, Map<String, Double>>) xStream.fromXML(fileInputStream); transitionMatrix = (Map<String, Map<String, Double>>) xStream.fromXML(fileInputStream);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -41,4 +41,9 @@ public class PopulationDynamicsByAgeClass extends Dynamics { ...@@ -41,4 +41,9 @@ public class PopulationDynamicsByAgeClass extends Dynamics {
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp); logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp);
} }
@Override
public void initParameters() {
}
} }
...@@ -39,4 +39,9 @@ public class ProductionDynamics extends Dynamics { ...@@ -39,4 +39,9 @@ public class ProductionDynamics extends Dynamics {
public void initLogger() { public void initLogger() {
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp); logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp);
} }
@Override
public void initParameters() {
}
} }
...@@ -48,7 +48,7 @@ public class FarmerTest { ...@@ -48,7 +48,7 @@ public class FarmerTest {
} }
Farmer farmer = new Farmer(54, farm); Farmer farmer = new Farmer(54, farm);
Simulator simulator = new Simulator(new Parameters(0, new ArrayList<>()), Collections.singletonList(farmer)); Simulator simulator = new Simulator(new Parameters(0, new ArrayList<>(), ""), Collections.singletonList(farmer));
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
simulator.serializeXML(stream); simulator.serializeXML(stream);
Simulator simulator2 = Simulator.loadSimulator(new ByteArrayInputStream(stream.toByteArray())); Simulator simulator2 = Simulator.loadSimulator(new ByteArrayInputStream(stream.toByteArray()));
......
...@@ -45,7 +45,7 @@ public class GeoAreaTest { ...@@ -45,7 +45,7 @@ public class GeoAreaTest {
farm.addPatch(new Patch(g, 25.3f, CulturalPractice.S, new Product("Chou", Unit.Kg))); farm.addPatch(new Patch(g, 25.3f, CulturalPractice.S, new Product("Chou", Unit.Kg)));
Farmer farmer = new Farmer(30, farm); Farmer farmer = new Farmer(30, farm);
Simulator simulator = new Simulator(new Parameters(0, new ArrayList<>()), Arrays.asList(farmer)); Simulator simulator = new Simulator(new Parameters(0, new ArrayList<>(), ""), Arrays.asList(farmer));
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
simulator.serializeXML(stream); simulator.serializeXML(stream);
Simulator simulator2 = Simulator.loadSimulator(new ByteArrayInputStream(stream.toByteArray())); Simulator simulator2 = Simulator.loadSimulator(new ByteArrayInputStream(stream.toByteArray()));
......
...@@ -35,6 +35,6 @@ public class PatchTest { ...@@ -35,6 +35,6 @@ public class PatchTest {
GeoArea g = new GeoArea(); GeoArea g = new GeoArea();
Product p = new Product("Blé", Unit.Kg, 2f); Product p = new Product("Blé", Unit.Kg, 2f);
Patch patch = new Patch(g, 42f, CulturalPractice.I, p, 42); Patch patch = new Patch(g, 42f, CulturalPractice.I, p, 42);
assertEquals(patch.getYield(), new Float(2f*1.25f)); assertEquals(patch.getYield(), Float.valueOf(2f*1.25f));
} }
} }
...@@ -146,7 +146,7 @@ public class SimulatorMinimalTest { ...@@ -146,7 +146,7 @@ public class SimulatorMinimalTest {
List<Dynamics> dynamics = new ArrayList<>(); List<Dynamics> dynamics = new ArrayList<>();
dynamics.addAll(Arrays.asList(new PopulationDynamics(), new ProductionDynamics(), new MarketDynamics(), dynamics.addAll(Arrays.asList(new PopulationDynamics(), new ProductionDynamics(), new MarketDynamics(),
new FlowDynamics())); new FlowDynamics()));
Simulator simulator = new Simulator(new Parameters(0, dynamics), farmers); Simulator simulator = new Simulator(new Parameters(0, dynamics, "data/in/config/"), farmers);
simulator.serialize("minimal.xml"); simulator.serialize("minimal.xml");
directory = simulator.getTimestampDirectory(); directory = simulator.getTimestampDirectory();
} }
......
...@@ -119,7 +119,8 @@ public class SimulatorTest { ...@@ -119,7 +119,8 @@ public class SimulatorTest {
dynamics.add(new ProductionDynamics()); dynamics.add(new ProductionDynamics());
dynamics.add(new MarketDynamics()); dynamics.add(new MarketDynamics());
dynamics.add(new FlowDynamics()); dynamics.add(new FlowDynamics());
Simulator simulator = new Simulator(new Parameters(0, dynamics), farmers); Simulator simulator = new Simulator(new Parameters(0, dynamics, "data/in/config/"),
farmers);
//simulator.patchesDB(); //simulator.patchesDB();
simulator.serialize("simulator.xml"); simulator.serialize("simulator.xml");
directory = simulator.getTimestampDirectory(); directory = simulator.getTimestampDirectory();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment