Commit 21b4df6f authored by Loris Croce's avatar Loris Croce
Browse files

Farmer ageclasses log state dynamics

parent b4fbc276
Pipeline #40811 passed with stages
in 4 minutes and 42 seconds
package fr.inrae.agriterix.generator;
import co.nstant.in.cbor.CborException;
import com.thoughtworks.xstream.XStream;
import fr.inrae.agriterix.simulator.CulturalPractice;
import fr.inrae.agriterix.simulator.Farm;
......@@ -14,11 +15,7 @@ import fr.inrae.agriterix.simulator.Simulator;
import fr.inrae.agriterix.simulator.Stocks;
import fr.inrae.agriterix.simulator.TimeUnit;
import fr.inrae.agriterix.simulator.Unit;
import fr.inrae.agriterix.simulator.dynamics.Dynamics;
import fr.inrae.agriterix.simulator.dynamics.FlowDynamics;
import fr.inrae.agriterix.simulator.dynamics.MarketDynamics;
import fr.inrae.agriterix.simulator.dynamics.PopulationDynamicsByAgeClass;
import fr.inrae.agriterix.simulator.dynamics.ProductionDynamics;
import fr.inrae.agriterix.simulator.dynamics.*;
import fr.inrae.agriterix.simulator.dynamics.transmission.TransmissionTendancy;
import java.io.File;
import java.io.IOException;
......@@ -172,13 +169,13 @@ public class Generator {
return transitions;
}
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException, CborException {
// farmers ages from MSA for France, starting at 18 year old
int[] ages_2018 = {1, 76, 268, 579, 1079, 1493, 1973, 2432, 3059, 3592, 4236, 4717, 5440, 5904, 6419, 6945, 7574, 7847, 8414, 8704, 8992, 8782, 8821, 9069, 9236, 9741, 10569, 11458, 12058, 12623, 12927, 13351, 13637, 14446, 15461, 16145, 17113, 17017, 17163, 17660, 17350, 17190, 16147, 12294, 10037, 6746, 5509, 4670, 3720, 2830, 2401, 2181, 1856, 1584, 1209, 790, 665, 595, 496, 390, 333, 346, 303, 246, 238, 205, 201, 175, 146, 125, 92, 82, 75, 55, 50, 36, 36, 26, 25, 18, 13, 10, 11};
GeoArea geoarea = new GeoArea("Puy-de-Dome");
RandomStreamBase rng = new MRG32k3a();
List<Farmer> farmers = generate_farmers(ages_2018, geoarea, 6160);
List<Product> products = Generator.generate_patches(rng, "data/input/generator/patches/patches.shp", geoarea, farmers);
List<Product> products = Generator.generate_patches(rng, "data/in/generator/patches/patches.shp", geoarea, farmers);
for (Product product : products) {
geoarea.getFlows().add(createFlow(product, 100.f));
}
......@@ -190,8 +187,9 @@ public class Generator {
dynamics.add(new ProductionDynamics());
dynamics.add(new MarketDynamics());
dynamics.add(new FlowDynamics());
dynamics.add(new StateDynamics());
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/in/generator/simulator.xml")));
System.out.println("# patches: " + simulator.geoAreas().get(0).getPatches().size());
System.out.println("# farmers: " + simulator.getFarmers().size());
System.out.println("# products: " + simulator.getGlobal().getInputs().size());
......@@ -199,5 +197,6 @@ public class Generator {
for (int i = 0; i < 50; i++) {
simulator.step(TimeUnit.YEAR);
}
simulator.patchesCBOR();
}
}
......@@ -17,6 +17,12 @@ import fr.inrae.agriterix.simulator.dynamics.*;
import org.apache.commons.cli.*;
import org.apache.commons.lang3.mutable.MutableInt;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.simple.SimpleFeature;
public class Simulator {
......@@ -279,6 +285,17 @@ public class Simulator {
arrayBuilder = cborBuilder.addArray();
os = Files.newOutputStream(Paths.get("data/out/" + timestamp + "/patches.cbor"));
// header
arrayBuilder.addArray()
.add("ID_PARCEL")
.add("culture")
.add("surface")
.add("geoarea")
.add("pratique")
.add("production")
.add("id_exploit")
.end();
for (Farmer farmer : this.getFarmers()) {
for (Patch patch : farmer.getFarm().getPatches()) {
arrayBuilder.addArray()
......@@ -304,6 +321,8 @@ public class Simulator {
}
// fixit crashed
public static void main(String[] args) throws Exception {
Options options = new Options();
Option inputFile = new Option("i", "input", true, "Simulation input file");
......
......@@ -16,7 +16,7 @@ public class DynamicsLogger {
private FileHandler fileHandler;
private Logger logger;
private String separator = ", ";
private String separator = ",";
private String path = "data/out";
public DynamicsLogger(String className, String timeStamp, String... headers) {
......
......@@ -3,26 +3,39 @@ package fr.inrae.agriterix.simulator.dynamics;
import fr.inrae.agriterix.simulator.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Here we can log global state of the simulator at each timestep, i.e. Farmers numbers, global products stocks etc.
*/
public class StateDynamics extends Dynamics {
DynamicsLogger ageClassLogger;
@Override
public void process(SimulationContext context, List<GeoArea> geoAreas, List<Farmer> farmers, Flow global,
Parameters parameters, TimeUnit timeUnit) throws IOException {
StringBuilder report = new StringBuilder();
report.append("Total farmers: ");
report.append(farmers.size());
logger.log(context.getTimestep(), report.toString());
int[] ageClasses = new int[82];
// all geoAreas
for (Farmer farmer : farmers) {
ageClasses[farmer.getAge() - 18] += 1;
}
ageClassLogger.log(context.getTimestep(), Arrays.toString(ageClasses).replace("[", "")
.replace("]", ""));
}
@Override
public void initLogger() {
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp, "HEADER");
//TODO multiple loggers that plots age distributions
int[] ageClassesHeader = new int[82];
for (int i = 0; i < ageClassesHeader.length; i++) {
ageClassesHeader[i] = i + 18;
}
ageClassLogger = new DynamicsLogger("AgeClasses", super.timeStamp, "timestamp",
Arrays.toString(ageClassesHeader).replace("[", "")
.replace("]", ""));
}
@Override
......
......@@ -284,7 +284,6 @@ public class SimulatorTest {
return i + (j - i) * random.nextFloat();
}
@After
public void cleanup() throws IOException {
Path current = FileSystems.getDefault().getPath("data/out");
......
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