Commit 45689ef2 authored by Loris Croce's avatar Loris Croce
Browse files

Population byageclass in minimal test

parent 5a632892
Pipeline #38495 passed with stages
in 4 minutes and 29 seconds
......@@ -14,7 +14,7 @@ public class StateDynamics extends Dynamics {
Parameters parameters, TimeUnit timeUnit) throws IOException {
StringBuilder report = new StringBuilder();
report.append("Total farmers : ");
report.append("Total farmers: ");
report.append(farmers.size());
logger.log(context.getTimestep(), report.toString());
......
......@@ -4,8 +4,8 @@ package fr.inrae.agriterix.simulator;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.security.NoTypePermission;
import fr.inrae.agriterix.simulator.dynamics.*;
import fr.inrae.agriterix.simulator.dynamics.transmission.TransmissionTendancy;
import org.junit.*;
import org.yaml.snakeyaml.Yaml;
import java.io.*;
import java.nio.charset.StandardCharsets;
......@@ -144,8 +144,11 @@ public class SimulatorMinimalTest {
farmers.addAll(Arrays.asList(farmer1, farmer2, farmer3, farmer4));
// Dynamics
List<Dynamics> dynamics = new ArrayList<>();
dynamics.addAll(Arrays.asList(new PopulationDynamics(), new ProductionDynamics(), new MarketDynamics(),
new FlowDynamics(), new StateDynamics()));
dynamics.addAll(Arrays.asList(new PopulationDynamicsByAgeClass(5.31966309e-04, 3.02391767e+01,
3.89377418e-01, 7.46514412e+00,4.27098464e+01, 1.58988450e-01,
new TransmissionTendancy(0.5, 0.5, 3,
createTransMatrix(products))), new ProductionDynamics(), new MarketDynamics(),
new FlowDynamics(), new StateDynamics()));
Simulator simulator = new Simulator(new Parameters(0, dynamics, "data/in/config/"), farmers);
simulator.serialize("minimal.xml");
directory = simulator.getTimestampDirectory();
......@@ -155,7 +158,7 @@ public class SimulatorMinimalTest {
public void testDynamics() throws IOException {
Simulator simulator = Simulator.loadSimulator(new FileInputStream(directory + "minimal.xml"));
int n = 30;
int n = 100;
for (int i = 0; i < n; i++) {
simulator.step(TimeUnit.YEAR);
}
......@@ -163,6 +166,25 @@ public class SimulatorMinimalTest {
}
public static Map<String, Map<String, Double>> createTransMatrix(List<Product> products) throws IOException {
// for test minimal
XStream xStream = new XStream();
XStream.setupDefaultSecurity(xStream);
xStream.allowTypesByRegExp(new String[]{".*"});
Map<String, Map<String, Double>> transitions = new ConcurrentHashMap<>();
for (int i = 0; i < products.size(); i++) {
Map<String, Double> transitionsP = new ConcurrentHashMap<>();
for (int j = 0; j < products.size(); j++) {
transitionsP.put(products.get(j).getLabel(), i == j ? 0.9 : 0.1 / (products.size() - 1));
}
transitions.put(products.get(i).getLabel(), transitionsP);
}
return transitions;
}
//@Test
public void createTransMatrix() throws IOException {
// for test minimal
......@@ -196,9 +218,9 @@ public class SimulatorMinimalTest {
String xml = xStream.toXML(transitions);
FileOutputStream fileOutputStream = new FileOutputStream("data/in/config/transitions.xml");
fileOutputStream.write(xml.getBytes(StandardCharsets.UTF_8));
// FileInputStream fileInputStream = new FileInputStream("config/transitions.xml");
// Map<Product, Map<Product, Double>> transitionsM = (Map<Product, Map<Product, Double>>) xStream
// .fromXML(fileInputStream);
// FileInputStream fileInputStream = new FileInputStream("config/transitions.xml");
// Map<Product, Map<Product, Double>> transitionsM = (Map<Product, Map<Product, Double>>) xStream
// .fromXML(fileInputStream);
}
//@Test
......@@ -236,31 +258,31 @@ public class SimulatorMinimalTest {
map2.put(new Product("Blé", Unit.Kg), 0.45);
// 2
// map1.put(0.20, new Product("Patates", Unit.Kg));
// map1.put(0.12, new Product("Carottes", Unit.Kg));
// map1.put(0.08, new Product("Haricots", Unit.Kg));
// map1.put(0.13, new Product("Salades", Unit.Kg));
// map1.put(0.07, new Product("Courgettes", Unit.Kg));
// map1.put(0.05, new Product("Citrouilles", Unit.Kg));
// map1.put(0.15, new Product("Oignons", Unit.Kg));
// map1.put(0.14, new Product("Betteraves", Unit.Kg));
// map1.put(0.06, new Product("Choux", Unit.Kg));
//
// map2.put(new Product("Patates", Unit.Kg), 0.20);
// map2.put(new Product("Carottes", Unit.Kg), 0.12);
// map2.put(new Product("Haricots", Unit.Kg), 0.08);
// map2.put(new Product("Salades", Unit.Kg), 0.13);
// map2.put(new Product("Courgettes", Unit.Kg), 0.07);
// map2.put(new Product("Citrouilles", Unit.Kg), 0.05);
// map2.put(new Product("Oignons", Unit.Kg), 0.15);
// map2.put(new Product("Betteraves", Unit.Kg), 0.14);
// map2.put(new Product("Choux", Unit.Kg), 0.06);
// map1.put(0.20, new Product("Patates", Unit.Kg));
// map1.put(0.12, new Product("Carottes", Unit.Kg));
// map1.put(0.08, new Product("Haricots", Unit.Kg));
// map1.put(0.13, new Product("Salades", Unit.Kg));
// map1.put(0.07, new Product("Courgettes", Unit.Kg));
// map1.put(0.05, new Product("Citrouilles", Unit.Kg));
// map1.put(0.15, new Product("Oignons", Unit.Kg));
// map1.put(0.14, new Product("Betteraves", Unit.Kg));
// map1.put(0.06, new Product("Choux", Unit.Kg));
//
// map2.put(new Product("Patates", Unit.Kg), 0.20);
// map2.put(new Product("Carottes", Unit.Kg), 0.12);
// map2.put(new Product("Haricots", Unit.Kg), 0.08);
// map2.put(new Product("Salades", Unit.Kg), 0.13);
// map2.put(new Product("Courgettes", Unit.Kg), 0.07);
// map2.put(new Product("Citrouilles", Unit.Kg), 0.05);
// map2.put(new Product("Oignons", Unit.Kg), 0.15);
// map2.put(new Product("Betteraves", Unit.Kg), 0.14);
// map2.put(new Product("Choux", Unit.Kg), 0.06);
Stocks stocks1 = new Stocks();
Stocks stocks2 = new Stocks();
// System.out.println("-----------------------");
// System.out.println(map2);
// System.out.println("-----------------------");
// System.out.println(map2);
int n = 10;
......@@ -269,27 +291,27 @@ public class SimulatorMinimalTest {
stocks2.add(simulator.getContext().nextMapObjectWithDistributionInKeysBis(map2), 1f / n);
}
// System.out.println("n = " + n);
// System.out.println("-----------------------");
// System.out.println("transitions1 : " + stocks1);
// System.out.println("n = " + n);
// System.out.println("-----------------------");
// System.out.println("transitions1 : " + stocks1);
Map<Product, Double> errors1 = new HashMap<>();
for (Map.Entry<Product, Double> entry : map2.entrySet()) {
errors1.put(entry.getKey(), Math.abs(stocks1.get(entry.getKey()) - map2.get(entry.getKey())));
}
// System.out.println("errors1: " + errors1);
// System.out.println("total errors 1: " + errors1.values().stream().mapToDouble(d -> d).sum());
// System.out.println("-----------------------");
// System.out.println("transitions2 : " + stocks2);
// System.out.println("errors1: " + errors1);
// System.out.println("total errors 1: " + errors1.values().stream().mapToDouble(d -> d).sum());
// System.out.println("-----------------------");
// System.out.println("transitions2 : " + stocks2);
Map<Product, Double> errors2 = new HashMap<>();
for (Map.Entry<Product, Double> entry : map2.entrySet()) {
errors2.put(entry.getKey(), Math.abs(stocks2.get(entry.getKey()) - map2.get(entry.getKey())));
}
// System.out.println("errors2: " + errors2);
// System.out.println("total errors 2: " + errors2.values().stream().mapToDouble(d -> d).sum());
// System.out.println("-----------------------");
// System.out.println("errors2: " + errors2);
// System.out.println("total errors 2: " + errors2.values().stream().mapToDouble(d -> d).sum());
// System.out.println("-----------------------");
}
......
Markdown is supported
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