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