Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Agriterix
simulator
Commits
9e4fd674
Commit
9e4fd674
authored
Jul 22, 2021
by
Loris Croce
Browse files
flow dynamics scenario loading
parent
a2cd465f
Pipeline
#37996
passed with stages
in 3 minutes and 53 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
build.gradle
View file @
9e4fd674
...
...
@@ -35,7 +35,8 @@ dependencies {
implementation
'net.andreinc:mockneat:0.4.7'
// https://mvnrepository.com/artifact/org.yaml/snakeyaml
implementation
group:
'org.yaml'
,
name:
'snakeyaml'
,
version:
'1.28'
// https://mvnrepository.com/artifact/com.opencsv/opencsv
implementation
group:
'com.opencsv'
,
name:
'opencsv'
,
version:
'5.5.1'
}
test
{
...
...
src/main/java/fr/inrae/agriterix/simulator/dynamics/FlowDynamics.java
View file @
9e4fd674
package
fr.inrae.agriterix.simulator.dynamics
;
import
com.opencsv.CSVReader
;
import
com.opencsv.exceptions.CsvException
;
import
fr.inrae.agriterix.simulator.*
;
import
java.util.ArrayList
;
import
java.io.FileNotFoundException
;
import
java.io.FileReader
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
public
class
FlowDynamics
extends
Dynamics
{
private
List
<
String
[]>
scenario
;
private
int
counter
;
public
FlowDynamics
()
{
}
@Override
public
void
initLogger
()
{
logger
=
new
DynamicsLogger
(
this
.
getClass
().
getSimpleName
(),
super
.
timeStamp
);
}
@Override
public
void
initParameters
()
{
try
(
CSVReader
reader
=
new
CSVReader
(
new
FileReader
(
configPath
+
"evolutions.csv"
)))
{
scenario
=
reader
.
readAll
();
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
CsvException
e
)
{
e
.
printStackTrace
();
}
counter
=
0
;
}
@Override
public
void
process
(
SimulationContext
context
,
List
<
GeoArea
>
geoAreas
,
List
<
Farmer
>
farmers
,
Flow
global
,
Parameters
parameters
,
TimeUnit
timeUnit
)
{
/**
* Here comes the modification of flows attributes, useful scenarios.
* just an example for now
*/
for
(
GeoArea
geoArea
:
geoAreas
)
{
for
(
Flow
flow
:
geoArea
.
getFlows
())
{
// here comes loading TODO (by file ?)
// just an example
List
<
Product
>
inputsMod
=
new
ArrayList
<>();
inputsMod
.
add
(
new
Product
(
"Lait"
,
Unit
.
L
));
List
<
Product
>
requiredMod
=
new
ArrayList
<>();
requiredMod
.
add
(
new
Product
(
"Lait"
,
Unit
.
L
));
List
<
Product
>
producedMod
=
new
ArrayList
<>();
producedMod
.
add
(
new
Product
(
"Fromage"
,
Unit
.
Kg
));
editInputs
(
context
.
getTimestep
(),
flow
,
inputsMod
,
1.5f
);
editRequirements
(
context
.
getTimestep
(),
flow
,
requiredMod
,
2
f
);
editProduced
(
context
.
getTimestep
(),
flow
,
producedMod
,
.
5
f
);
Map
<
String
,
Float
>
inputsMod
=
new
HashMap
<>();
Map
<
String
,
Float
>
requiredMod
=
new
HashMap
<>();
Map
<
String
,
Float
>
producedMod
=
new
HashMap
<>();
for
(
int
i
=
counter
;
i
<
scenario
.
size
();
i
++)
{
// checking timestep
if
(
Integer
.
parseInt
(
scenario
.
get
(
counter
)[
0
])
==
context
.
getTimestep
())
{
for
(
GeoArea
geoArea:
geoAreas
)
{
// checking geoArea
if
(
scenario
.
get
(
counter
)[
4
]
==
geoArea
.
getLabel
())
{
// checking flowtype
if
(
scenario
.
get
(
counter
)[
3
]
==
"Inputs"
)
{
inputsMod
.
put
(
scenario
.
get
(
counter
)[
1
],
Float
.
parseFloat
(
scenario
.
get
(
counter
)[
2
]));
}
else
if
(
scenario
.
get
(
counter
)[
3
]
==
"Required"
)
{
requiredMod
.
put
(
scenario
.
get
(
counter
)[
1
],
Float
.
parseFloat
(
scenario
.
get
(
counter
)[
2
]));
}
else
if
(
scenario
.
get
(
counter
)[
3
]
==
"Produced"
)
{
producedMod
.
put
(
scenario
.
get
(
counter
)[
1
],
Float
.
parseFloat
(
scenario
.
get
(
counter
)[
2
]));
}
}
for
(
Flow
flow
:
geoArea
.
getFlows
())
{
editInputs
(
counter
,
flow
,
inputsMod
);
editRequirements
(
counter
,
flow
,
requiredMod
);
editProduced
(
counter
,
flow
,
producedMod
);
}
}
counter
=
i
;
break
;
}
}
}
//TODO test methods below
/**
* Changes the maximum value of inputs.
* Changes the maximum value of inputs
* @param timestep current timestep
* @param flow selected flow
* @param products selected products
* @param factor factor, f < 1 : decrease, f > 1 : increase
* @param mods product label → factor map
*/
public
void
editInputs
(
int
timestep
,
Flow
flow
,
List
<
Product
>
products
,
Float
factor
)
{
public
void
editInputs
(
int
timestep
,
Flow
flow
,
Map
<
String
,
Float
>
mods
)
{
for
(
Flow
.
Input
input
:
flow
.
getInputs
())
{
for
(
Product
product
:
products
)
{
if
(
input
.
getProduct
().
equals
(
product
))
{
input
.
setMaximum
(
input
.
getMaximum
()
*
factor
);
logger
.
logEvolutions
(
timestep
,
flow
,
"input"
,
product
,
factor
);
for
(
Map
.
Entry
<
String
,
Float
>
entry
:
mods
.
entrySet
()
)
{
if
(
input
.
getProduct
().
getLabel
().
equals
(
entry
.
getKey
()
))
{
input
.
setMaximum
(
input
.
getMaximum
()
*
entry
.
getValue
()
);
logger
.
logEvolutions
(
timestep
,
flow
,
"input"
,
input
.
getProduct
(),
entry
.
getValue
()
);
}
}
}
}
/**
* Changes the value of requirements
.
* @param timestep
* Changes the value of requirements
* @param timestep
current timestep
* @param flow selected flow
* @param products selected products
* @param factor factor, f < 1 : decrease, f > 1 : increase
* @param mods product label → factor map
*/
public
void
editRequirements
(
int
timestep
,
Flow
flow
,
List
<
Product
>
products
,
Float
factor
)
{
public
void
editRequirements
(
int
timestep
,
Flow
flow
,
Map
<
String
,
Float
>
mods
)
{
for
(
Product
required
:
flow
.
getRatio
().
getRequired
().
keySet
())
{
for
(
Product
product
:
products
)
{
if
(
required
.
equals
(
product
))
{
for
(
Map
.
Entry
<
String
,
Float
>
entry
:
mods
.
entrySet
()
)
{
if
(
required
.
getLabel
().
equals
(
entry
.
getKey
()
))
{
flow
.
getRatio
().
getRequired
().
put
(
required
,
flow
.
getRatio
().
getRequired
()
.
get
(
required
)
*
factor
);
logger
.
logEvolutions
(
timestep
,
flow
,
"requirement"
,
product
,
factor
);
.
get
(
required
)
*
entry
.
getValue
());
logger
.
logEvolutions
(
timestep
,
flow
,
"requirement"
,
required
,
entry
.
getValue
());
}
}
}
}
/**
* Changes the value of produced
.
* @param timestep
* Changes the value of produced
* @param timestep
current timestep
* @param flow selected flow
* @param products selected products
* @param factor factor, f < 1 : decrease, f > 1 : increase
* @param mods product label → factor map
*/
public
void
editProduced
(
int
timestep
,
Flow
flow
,
List
<
Product
>
products
,
Float
factor
)
{
public
void
editProduced
(
int
timestep
,
Flow
flow
,
Map
<
String
,
Float
>
mods
)
{
for
(
Product
produced
:
flow
.
getRatio
().
getProduced
().
keySet
())
{
for
(
Product
product
:
products
)
{
if
(
produced
.
equals
(
product
))
{
for
(
Map
.
Entry
<
String
,
Float
>
entry
:
mods
.
entrySet
()
)
{
if
(
produced
.
getLabel
().
equals
(
entry
.
getKey
()
))
{
flow
.
getRatio
().
getProduced
().
put
(
produced
,
flow
.
getRatio
().
getProduced
()
.
get
(
produced
)
*
factor
);
logger
.
logEvolutions
(
timestep
,
flow
,
"produced"
,
produc
t
,
factor
);
.
get
(
produced
)
*
entry
.
getValue
()
);
logger
.
logEvolutions
(
timestep
,
flow
,
"produced"
,
produc
ed
,
entry
.
getValue
()
);
}
}
}
}
@Override
public
void
initLogger
()
{
logger
=
new
DynamicsLogger
(
this
.
getClass
().
getSimpleName
(),
super
.
timeStamp
);
}
@Override
public
void
initParameters
()
{
}
}
src/main/java/fr/inrae/agriterix/simulator/dynamics/MarketDynamics.java
View file @
9e4fd674
...
...
@@ -31,6 +31,7 @@ public class MarketDynamics extends Dynamics {
@Override
public
void
initParameters
()
{
//TODO load import_export.csv
}
...
...
src/test/java/fr/inrae/agriterix/simulator/DynamicsTest.java
View file @
9e4fd674
package
fr.inrae.agriterix.simulator
;
import
com.opencsv.CSVReader
;
import
com.opencsv.exceptions.CsvException
;
import
fr.inrae.agriterix.simulator.dynamics.MarketDynamics
;
import
fr.inrae.agriterix.simulator.dynamics.PopulationDynamics
;
import
org.junit.*
;
import
static
org
.
junit
.
Assert
.*;
import
java.io.FileNotFoundException
;
import
java.io.FileReader
;
import
java.io.IOException
;
import
java.util.*
;
import
java.util.concurrent.ConcurrentHashMap
;
...
...
@@ -55,6 +60,10 @@ public class DynamicsTest {
m
.
allocate
(
productions
.
entrySet
().
iterator
().
next
(),
inputs2
.
get
(
0
),
flow2
,
new
Stocks
(),
1
f
);
assertEquals
(
Optional
.
of
(
0
.
f
),
Optional
.
of
(
productions
.
get
(
carottes
)));
assertEquals
(
Optional
.
of
(
192
.
f
),
Optional
.
of
(
flow2
.
getQuantities
().
get
(
carottes
)));
}
@Test
public
void
playground
()
{
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment