Commit 3aed33f9 authored by Loris Croce's avatar Loris Croce
Browse files

fix transmatrix

parent 653aa13a
Pipeline #36987 passed with stages
in 4 minutes and 15 seconds
<fr.inrae.agriterix.simulator.Product-array>
<fr.inrae.agriterix.simulator.Product>
<label>Blé</label>
<unit>Kg</unit>
<tags/>
<yield>1.0</yield>
</fr.inrae.agriterix.simulator.Product>
<fr.inrae.agriterix.simulator.Product>
<label>Carottes</label>
<unit>Kg</unit>
<tags/>
<yield>1.0</yield>
</fr.inrae.agriterix.simulator.Product>
<fr.inrae.agriterix.simulator.Product>
<label>Raisins</label>
<unit>Kg</unit>
<tags/>
<yield>1.0</yield>
</fr.inrae.agriterix.simulator.Product>
</fr.inrae.agriterix.simulator.Product-array>
\ No newline at end of file
<concurrent-hash-map>
<entry>
<string>Verger</string>
<concurrent-hash-map>
<entry>
<string>Verger</string>
<double>0.2</double>
</entry>
<entry>
<string>Légumes</string>
<double>0.3</double>
</entry>
<entry>
<string>Fourrage</string>
<double>0.3</double>
</entry>
<entry>
<string>Maïs</string>
<double>0.1</double>
</entry>
<entry>
<string>Blé</string>
<double>0.1</double>
</entry>
</concurrent-hash-map>
</entry>
<entry>
<string>Légumes</string>
<concurrent-hash-map>
<entry>
<string>Verger</string>
<double>0.5</double>
</entry>
<entry>
<string>Légumes</string>
<double>0.2</double>
</entry>
<entry>
<string>Fourrage</string>
<double>0.1</double>
</entry>
<entry>
<string>Maïs</string>
<double>0.1</double>
</entry>
<entry>
<string>Blé</string>
<double>0.1</double>
</entry>
</concurrent-hash-map>
</entry>
<entry>
<string>Fourrage</string>
<concurrent-hash-map>
<entry>
<string>Verger</string>
<double>0.3</double>
</entry>
<entry>
<string>Légumes</string>
<double>0.1</double>
</entry>
<entry>
<string>Fourrage</string>
<double>0.2</double>
</entry>
<entry>
<string>Maïs</string>
<double>0.1</double>
</entry>
<entry>
<string>Blé</string>
<double>0.3</double>
</entry>
</concurrent-hash-map>
</entry>
<entry>
<string>Maïs</string>
<concurrent-hash-map>
<entry>
<string>Verger</string>
<double>0.2</double>
</entry>
<entry>
<string>Légumes</string>
<double>0.2</double>
</entry>
<entry>
<string>Fourrage</string>
<double>0.3</double>
</entry>
<entry>
<string>Maïs</string>
<double>0.2</double>
</entry>
<entry>
<string>Blé</string>
<double>0.1</double>
</entry>
</concurrent-hash-map>
</entry>
<entry>
<string>Blé</string>
<concurrent-hash-map>
<entry>
<string>Verger</string>
<double>0.1</double>
</entry>
<entry>
<string>Légumes</string>
<double>0.1</double>
</entry>
<entry>
<string>Fourrage</string>
<double>0.1</double>
</entry>
<entry>
<string>Maïs</string>
<double>0.3</double>
</entry>
<entry>
<string>Blé</string>
<double>0.4</double>
</entry>
</concurrent-hash-map>
</entry>
</concurrent-hash-map>
\ No newline at end of file
...@@ -4,7 +4,7 @@ import java.util.*; ...@@ -4,7 +4,7 @@ import java.util.*;
public class Product { public class Product {
private final String label; private String label;
private final Unit unit; private final Unit unit;
//@XStreamImplicit //@XStreamImplicit
private List<Tag> tags; private List<Tag> tags;
...@@ -45,6 +45,10 @@ public class Product { ...@@ -45,6 +45,10 @@ public class Product {
return label + " " + unit + " " + tags; return label + " " + unit + " " + tags;
} }
public void setLabel(String label) {
this.label = label;
}
public String getLabel() { public String getLabel() {
return label; return label;
} }
......
...@@ -33,7 +33,7 @@ public class PopulationDynamics extends Dynamics { ...@@ -33,7 +33,7 @@ public class PopulationDynamics extends Dynamics {
private Product transitionProduct = new Product("Soja", Unit.Kg); private Product transitionProduct = new Product("Soja", Unit.Kg);
private Map<Product, Map<Product, Double>> transitionMatrix; private Map<String, Map<String, Double>> transitionMatrix;
public PopulationDynamics() { public PopulationDynamics() {
configPath = "config/transitions.xml"; configPath = "config/transitions.xml";
...@@ -55,7 +55,7 @@ public class PopulationDynamics extends Dynamics { ...@@ -55,7 +55,7 @@ public class PopulationDynamics extends Dynamics {
XStream.setupDefaultSecurity(xStream); XStream.setupDefaultSecurity(xStream);
xStream.allowTypesByRegExp(new String[]{".*"}); xStream.allowTypesByRegExp(new String[]{".*"});
FileInputStream fileInputStream = new FileInputStream(configPath); FileInputStream fileInputStream = new FileInputStream(configPath);
transitionMatrix = (Map<Product, Map<Product, Double>>) xStream.fromXML(fileInputStream); transitionMatrix = (Map<String, Map<String, Double>>) xStream.fromXML(fileInputStream);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -154,8 +154,13 @@ public class PopulationDynamics extends Dynamics { ...@@ -154,8 +154,13 @@ public class PopulationDynamics extends Dynamics {
List<Patch> patchesToRemove = new ArrayList<>(); List<Patch> patchesToRemove = new ArrayList<>();
for (Patch patch : newcomer.getFarm().getPatches()) { for (Patch patch : newcomer.getFarm().getPatches()) {
if (context.nextDouble() >= PATCH_CHANGE_CHANCE) { if (context.nextDouble() >= PATCH_CHANGE_CHANCE) {
transitionProduct = context.nextMapObjectWithDistributionInKeysBis(transitionMatrix if (transitionMatrix.get(patch.getProduction()
.get(patch.getProduction().getProduct())); .getProduct().getLabel()) == null) {
System.out.println(transitionMatrix);
System.out.println(patch.getProduction().getProduct());
}
transitionProduct.setLabel(context.nextMapObjectWithDistributionInKeysBis(transitionMatrix
.get(patch.getProduction().getProduct().getLabel())));
patchesChanged.add(new Patch(patch.getGeoArea(), patch.getSurface(), transitionPractice, patchesChanged.add(new Patch(patch.getGeoArea(), patch.getSurface(), transitionPractice,
transitionProduct)); transitionProduct));
patchesToRemove.add(patch); patchesToRemove.add(patch);
...@@ -232,8 +237,8 @@ public class PopulationDynamics extends Dynamics { ...@@ -232,8 +237,8 @@ public class PopulationDynamics extends Dynamics {
List<Patch> patchesToRemove = new ArrayList<>(); List<Patch> patchesToRemove = new ArrayList<>();
for (Patch patch : current.getFarm().getPatches()) { for (Patch patch : current.getFarm().getPatches()) {
if (context.nextDouble() >= PATCH_CHANGE_CHANCE) { if (context.nextDouble() >= PATCH_CHANGE_CHANCE) {
transitionProduct = context.nextMapObjectWithDistributionInKeysBis(transitionMatrix transitionProduct.setLabel(context.nextMapObjectWithDistributionInKeysBis(transitionMatrix
.get(patch.getProduction().getProduct())); .get(patch.getProduction().getProduct().getLabel())));
patchesChanged.add(new Patch(patch.getGeoArea(), patch.getSurface(), transitionPractice, patchesChanged.add(new Patch(patch.getGeoArea(), patch.getSurface(), transitionPractice,
transitionProduct)); transitionProduct));
patchesToRemove.add(patch); patchesToRemove.add(patch);
......
// Waiting for generator for proper configuration // Waiting for generator for proper configuration
//package fr.inrae.agriterix.simulator; package fr.inrae.agriterix.simulator;
//
//import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.CborException;
//import fr.inrae.agriterix.simulator.dynamics.*; import com.thoughtworks.xstream.XStream;
// import fr.inrae.agriterix.simulator.dynamics.*;
//import java.io.*;
// import java.io.*;
//import org.apache.commons.lang3.RandomStringUtils;
//import org.junit.*; import org.apache.commons.lang3.RandomStringUtils;
// import org.junit.*;
//import java.nio.file.FileSystems;
//import java.nio.file.Files; import java.nio.charset.StandardCharsets;
//import java.nio.file.Path; import java.nio.file.FileSystems;
//import java.util.*; import java.nio.file.Files;
//import java.util.concurrent.atomic.AtomicInteger; import java.nio.file.Path;
// import java.util.*;
//public class SimulatorTest { import java.util.concurrent.ConcurrentHashMap;
// import java.util.concurrent.atomic.AtomicInteger;
// private static final long seed = 42;
// private static final Random random = new Random(seed); public class SimulatorTest {
// private static final AtomicInteger counter = new AtomicInteger();
// private String directory = ""; private static final long seed = 42;
// // for names private static final Random random = new Random(seed);
// private int s = 1; private static final AtomicInteger counter = new AtomicInteger();
// private String directory = "";
// public SimulatorTest() { // for names
// private int s = 1;
// }
// public SimulatorTest() {
// @BeforeClass
// public static void setUpClass() { }
// }
// @BeforeClass
// @AfterClass public static void setUpClass() {
// public static void tearDownClass() { }
// }
// @AfterClass
// @Before public static void tearDownClass() {
// public void setUp() throws IOException { }
// final int PATCH_MAX = 300;
// final int FARM_ER_MAX = 100; @Before
// final int GEOAREA_MAX = 5; public void setUp() throws IOException {
// final int FLOW_MAX = 10; final int PATCH_MAX = 30;
// // Tagging products like this to prevent xstream bugs final int FARM_ER_MAX = 6000;
// Product bleTag = new Product("Blé", Unit.Kg); final int GEOAREA_MAX = 1;
// bleTag.addTag(Tag.B); final int FLOW_MAX = 50;
// bleTag.addTag(Tag.D);
// Product carottesTag = new Product("Carottes", Unit.Kg); // Tagging products like this to prevent xstream bugs
// carottesTag.addTag(Tag.B); Product pouletTag = new Product("Poulet", Unit.A);
// Product lentillesTag = new Product("Lentilles", Unit.Kg); pouletTag.addTag(Tag.D);
// lentillesTag.addTag(Tag.D); Product boeufTag = new Product("Boeuf", Unit.A);
// lentillesTag.addTag(Tag.B); boeufTag.addTag(Tag.A);
// lentillesTag.addTag(Tag.L); boeufTag.addTag(Tag.H);
// Product pouletTag = new Product("Poulet", Unit.A); Product moutonTag = new Product("Mouton", Unit.A);
// pouletTag.addTag(Tag.D); moutonTag.addTag(Tag.B);
// Product boeufTag = new Product("Boeuf", Unit.A); moutonTag.addTag(Tag.H);
// boeufTag.addTag(Tag.A);
// boeufTag.addTag(Tag.H); Product ble = new Product("Blé", Unit.Kg);
// Product moutonTag = new Product("Mouton", Unit.A); Product bleT = new Product("Blé", Unit.Kg);
// moutonTag.addTag(Tag.B); bleT.addTag(Tag.B);
// moutonTag.addTag(Tag.H); bleT.addTag(Tag.D);
// List<Product> products = Arrays.asList(new Product("Blé", Unit.Kg), new Product("Pomme de terre", Product mais = new Product("Maïs", Unit.Kg);
// Unit.Kg), new Product("Lentille", Unit.Kg), new Product("Carottes", Unit.Kg), Product fourrage = new Product("Fourrage", Unit.Kg);
// new Product("Carottes", Unit.Kg), new Product("Houblon", Unit.Kg), Product legumes = new Product("Légumes", Unit.Kg);
// new Product("Oignons", Unit.Kg), new Product("Lait", Unit.L), new Product("Raisin", Product legumesT = new Product("Légumes", Unit.Kg);
// Unit.Kg), bleTag, carottesTag, lentillesTag); legumesT.addTag(Tag.A);
// List<Product> transformed = Arrays.asList(new Product("Vin", Unit.L), new Product("Bière", Unit.L), Product verger = new Product("Verger", Unit.Kg);
// new Product("Pain", Unit.L), new Product("Nuggets", Unit.L), new Product("Fromage", Product vergerT = new Product("Verger", Unit.Kg);
// Unit.Kg), new Product("Soupe", Unit.L), new Product("Conserves", Unit.Kg)); vergerT.addTag(Tag.B);
// List<Farm> farms = new ArrayList<>();
// List<Farmer> farmers = new ArrayList<>(); List<Product> products = Arrays.asList(ble, mais, fourrage, legumes, verger, vergerT, legumesT, bleT);
// List<Product> animals = Arrays.asList(new Product("Boeuf", Unit.A), new Product("Mouton", Unit.A),
// new Product("Poulet", Unit.A), pouletTag, boeufTag, moutonTag); List<Product> transformed = Arrays.asList(new Product("Jus", Unit.L), new Product("Bière", Unit.L),
// for (int i = 0; i < GEOAREA_MAX; i++) { new Product("Pain", Unit.Kg), new Product("Nuggets", Unit.Kg), new Product("Soupe",
// List<Patch> patches = new ArrayList<>(); Unit.L), new Product("Conserves", Unit.Kg));
// List<Flow> flows = generateFlows(products, transformed, randomInt(1, FLOW_MAX), s); List<Farm> farms = new ArrayList<>();
// GeoArea geoArea = new GeoArea(flows, patches, RandomStringUtils.random(8, 0, 0, true,
// false, "abcdefghijklmnopqrstuvwxyz".toCharArray(), new Random(s++))); List<Farmer> farmers = new ArrayList<>();
// for (int j = 0; j < FARM_ER_MAX; j++) {
// Farm farm = new Farm(geoArea); List<Product> animals = Arrays.asList(new Product("Boeuf", Unit.A), new Product("Mouton", Unit.A),
// for (int k = 0; k < PATCH_MAX; k++) { new Product("Poulet", Unit.A), pouletTag, boeufTag, moutonTag);
// Patch patch = new Patch(geoArea, randomFloat(1, 150), CulturalPractice
// .values()[new Random().nextInt(CulturalPractice.values().length)], for (int i = 0; i < GEOAREA_MAX; i++) {
// randomProduct(products), farm, counter.getAndIncrement()); List<Patch> patches = new ArrayList<>();
// patches.add(patch); List<Flow> flows = generateFlows(products, transformed, randomInt(1, FLOW_MAX), s);
// } GeoArea geoArea = new GeoArea(flows, patches, RandomStringUtils.random(8, 0, 0, true,
// Product animalChosen = animals.get(random.nextInt(animals.size())); false, "abcdefghijklmnopqrstuvwxyz".toCharArray(), new Random(s++)));
// Product foodChosen = farm.getPatches().get(random.nextInt(farm.patchesSize())) for (int j = 0; j < FARM_ER_MAX; j++) {
// .getProduction().getProduct(); Farm farm = new Farm(geoArea);
// Map<Product, Float> required = new HashMap<>(); for (int k = 0; k < PATCH_MAX; k++) {
// required.put(animalChosen, random.nextFloat() * 10); Patch patch = new Patch(geoArea, randomFloat(1, 150), CulturalPractice
// required.put(foodChosen, random.nextFloat() * 100); .values()[new Random().nextInt(CulturalPractice.values().length)],
// Map<Product, Float> produced = new HashMap<>(); randomProduct(products), farm, counter.getAndIncrement());
// produced.put(new Product("Viande de " + animalChosen.getLabel(), Unit.Kg), random patches.add(patch);
// .nextFloat() * 100); }
// Stocks animalsChosen = new Stocks(); Product animalChosen = animals.get(random.nextInt(animals.size()));
// animalsChosen.add(animalChosen, random.nextFloat() * 200); Product foodChosen = farm.getPatches().get(random.nextInt(farm.patchesSize()))
// Livestock livestock = new Livestock(required, produced, animalsChosen); .getProduction().getProduct();
// farm.setLivestock(livestock); Map<Product, Float> required = new HashMap<>();
// farms.add(farm); required.put(animalChosen, random.nextFloat() * 10);
// Farmer farmer = new Farmer(randomInt(18, 70), farm); required.put(foodChosen, random.nextFloat() * 100);
// farmers.add(farmer); Map<Product, Float> produced = new HashMap<>();
// } produced.put(new Product("Viande de " + animalChosen.getLabel(), Unit.Kg), random
// } .nextFloat() * 100);
// Stocks animalsChosen = new Stocks();
// List<Dynamics> dynamics = new ArrayList<>(); animalsChosen.add(animalChosen, random.nextFloat() * 200);
// dynamics.add(new PopulationDynamics()); Livestock livestock = new Livestock(required, produced, animalsChosen);
// dynamics.add(new ProductionDynamics()); farm.setLivestock(livestock);
// dynamics.add(new MarketDynamics()); farms.add(farm);
// dynamics.add(new FlowDynamics()); Farmer farmer = new Farmer(randomInt(18, 70), farm);
// Simulator simulator = new Simulator(new Parameters(0, dynamics), farms, farmers); farmers.add(farmer);
// //simulator.patchesDB(); }
// simulator.serialize("simulator.xml"); }
// directory = simulator.getTimestampDirectory();
// } List<Dynamics> dynamics = new ArrayList<>();
// dynamics.add(new PopulationDynamics());
// @After dynamics.add(new ProductionDynamics());
// public void tearDown() { dynamics.add(new MarketDynamics());
// } dynamics.add(new FlowDynamics());
// Simulator simulator = new Simulator(new Parameters(0, dynamics), farms, farmers);
// @Test //simulator.patchesDB();
// public void testDynamics() throws IOException { simulator.serialize("simulator.xml");
// Simulator simulator = Simulator.loadSimulator(new FileInputStream(directory + "simulator.xml")); directory = simulator.getTimestampDirectory();
// int n = 50; }
// for (int i = 0; i < n; i++) {
// simulator.step(TimeUnit.YEAR); @After
// } public void tearDown() {
// } }
//
// public static List<String> loadProducts(String filename) throws IOException { @Test
// //TODO refractor to load a products list from a file with units. public void testDynamics() throws IOException {
// BufferedReader abc = new BufferedReader(new FileReader(filename)); Simulator simulator = Simulator.loadSimulator(new FileInputStream(directory + "simulator.xml"));
// List<String> products = new ArrayList<>(); int n = 50;
// String line; for (int i = 0; i < n; i++) {
// while ((line = abc.readLine()) != null) { simulator.step(TimeUnit.YEAR);
// products.add(line); }
// } }
// abc.close();
// return products; public static List<String> loadProducts(String filename) throws IOException {
// } //TODO refractor to load a products list from a file with units.
// BufferedReader abc = new BufferedReader(new FileReader(filename));
// List<String> products = new ArrayList<>();
// public static List<Flow> generateFlows(List<Product> products, List<Product> transformed, int flowMax, int s) { String line;
// int inputMax = 4; while ((line = abc.readLine()) != null) {
// int outputMax = 3; products.add(line);
// List<Flow> flows = new ArrayList<>(); }
// abc.close();
// for (int i = 0; i < flowMax; i++) { return products;
// }
// List<Flow.Input> inputs = new ArrayList<>();
// List<Product> outputs = new ArrayList<>();
// public static List<Flow> generateFlows(List<Product> products, List<Product> transformed, int flowMax, int s) {
// for (int j = 0; j < inputMax; j++) { int inputMax = 4;
// inputs.add(new Flow.Input(randomProduct(products), randomFloat(1, 10) * 10)); int outputMax = 3;
// } List<Flow> flows = new ArrayList<>();
// for (int j = 0; j < outputMax; j++) {
// outputs.add(randomProduct(transformed)); for (int i = 0; i < flowMax; i++) {
// }
// List<Flow.Input> inputs = new ArrayList<>();
// Stocks quantities = new Stocks(); List<Product> outputs = new ArrayList<>();
// Map<Product, Float> required = new HashMap<>();
// Map<Product, Float> produced = new HashMap<>(); for (int j = 0; j < inputMax; j++) {
// inputs.add(new Flow.Input(randomProduct(products), randomFloat(1, 10) * 10));
// for (Flow.Input input : inputs) { }
// required.put(new Product(input.getLabel(), input.getUnit()), random.nextFloat()); for (int j = 0; j < outputMax; j++) {
// } outputs.add(randomProduct(transformed));
// for (Product output : outputs) { }
// produced.put(new Product(output.getLabel(), output.getUnit()), random.nextFloat());
// } Stocks quantities = new Stocks();
// Map<Product, Float> required = new HashMap<>();
// Ratio ratio = new Ratio(required, produced); Map<Product, Float> produced = new HashMap<>();
// flows.add(new Flow(inputs, quantities, outputs, ratio, RandomStringUtils.random(8, 0, 0, true, false,
// "abcdefghijklmnopqrstuvwxyz".toCharArray(), new Random(s++)))); for (Flow.Input input : inputs) {
// } required.put(new Product(input.getLabel(), input.getUnit()), random.nextFloat());
// return flows; }
// } for (Product output : outputs) {
// produced.put(new Product(output.getLabel(), output.getUnit()), random.nextFloat());
// @Test }
// public void toDB() throws FileNotFoundException {
// Simulator simulator = Simulator.loadSimulator(new FileInputStream(directory + "simulator.xml")); Ratio ratio = new Ratio(required, produced);
// int patchesNB = 0; flows.add(new Flow(inputs, quantities, outputs, ratio, RandomStringUtils.random(8, 0, 0, true, false,
// for (GeoArea geoArea : simulator.geoAreas()) { "abcdefghijklmnopqrstuvwxyz".toCharArray(), new Random(s++))));
// for (Patch patch : geoArea.getPatches()) { }
// patchesNB++; return flows;
// } }
// }
// //System.out.println(patchesNB); //@Test
// try { public void toDB() throws FileNotFoundException {
// simulator.patchesCBOR(); Simulator simulator = Simulator.loadSimulator(new FileInputStream(directory + "simulator.xml"));
// } catch (IOException e) {