Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PLUM
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Land Use Lab
PLUM
Commits
10ef6d14
Commit
10ef6d14
authored
2 years ago
by
Bart Arendarczyk
Browse files
Options
Downloads
Patches
Plain Diff
Fixed calculation issue with trade barriers.
parent
22095f91
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/ac/ed/lurg/country/CountryManager.java
+4
-0
4 additions, 0 deletions
src/ac/ed/lurg/country/CountryManager.java
src/ac/ed/lurg/country/TradeManager.java
+34
-30
34 additions, 30 deletions
src/ac/ed/lurg/country/TradeManager.java
with
38 additions
and
30 deletions
src/ac/ed/lurg/country/CountryManager.java
+
4
−
0
View file @
10ef6d14
...
@@ -82,4 +82,8 @@ public class CountryManager {
...
@@ -82,4 +82,8 @@ public class CountryManager {
return
mapFromCompositeCountry
.
keySet
();
return
mapFromCompositeCountry
.
keySet
();
}
}
public
boolean
isCountry
(
String
isoCode
)
{
return
codeMap
.
containsKey
(
isoCode
);
}
}
}
This diff is collapsed.
Click to expand it.
src/ac/ed/lurg/country/TradeManager.java
+
34
−
30
View file @
10ef6d14
...
@@ -2,6 +2,7 @@ package ac.ed.lurg.country;
...
@@ -2,6 +2,7 @@ package ac.ed.lurg.country;
import
java.io.BufferedReader
;
import
java.io.BufferedReader
;
import
java.io.FileReader
;
import
java.io.FileReader
;
import
java.util.Collection
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Map.Entry
;
import
java.util.Map.Entry
;
import
java.util.HashMap
;
import
java.util.HashMap
;
...
@@ -18,31 +19,44 @@ public class TradeManager {
...
@@ -18,31 +19,44 @@ public class TradeManager {
private
static
final
int
COUNTRY_COL
=
0
;
private
static
final
int
COUNTRY_COL
=
0
;
private
static
final
int
ITEM_COL
=
1
;
private
static
final
int
ITEM_COL
=
1
;
private
static
final
int
TRADE_BARRIER_COL
=
2
;
private
static
final
int
TRADE_BARRIER_COL
=
2
;
private
Map
<
Composit
eCountry
,
CropToDoubleMap
>
tradeMap
=
new
HashMap
<
CompositeCountry
,
CropToDoubleMap
>();
private
Map
<
Singl
eCountry
,
CropToDoubleMap
>
singleCountryBarriers
=
new
HashMap
<
>();
public
TradeManager
()
{
public
TradeManager
()
{
if
(
ModelConfig
.
ACTIVE_TRADE_BARRIERS
)
if
(
ModelConfig
.
ACTIVE_TRADE_BARRIERS
)
read
();
read
();
}
}
// TODO weird summing
public
CropToDoubleMap
getTradeBarriers
(
CompositeCountry
cc
)
{
public
CropToDoubleMap
getTradeBarriers
(
CompositeCountry
cc
)
{
if
(
tradeMap
.
get
(
cc
)
==
null
)
{
CropToDoubleMap
tradeBarriers
=
new
CropToDoubleMap
();
CropToDoubleMap
tradeBarriers
=
new
CropToDoubleMap
();
// If not using data, use default value for all
if
(!
ModelConfig
.
ACTIVE_TRADE_BARRIERS
)
{
for
(
CropType
c
:
CropType
.
getImportedTypes
())
{
for
(
CropType
c
:
CropType
.
getImportedTypes
())
{
tradeBarriers
.
put
(
c
,
ModelConfig
.
TRADE_BARRIER_FACTOR_DEFAULT
);
tradeBarriers
.
put
(
c
,
ModelConfig
.
TRADE_BARRIER_FACTOR_DEFAULT
);
}
}
return
tradeBarriers
;
tradeMap
.
put
(
cc
,
tradeBarriers
);
}
}
else
{
tradeMap
.
get
(
cc
).
put
(
CropType
.
ENERGY_CROPS
,
ModelConfig
.
TRADE_BARRIER_ENERGY_CROPS
);
// Use observed trade barriers
Collection
<
SingleCountry
>
singleCountries
=
CountryManager
.
getInstance
().
getAllForCompositeCountry
(
cc
);
int
n
=
0
;
for
(
SingleCountry
sc
:
singleCountries
)
{
CropToDoubleMap
countryData
=
singleCountryBarriers
.
get
(
sc
);
for
(
CropType
crop
:
CropType
.
getImportedTypes
())
{
if
(
countryData
.
containsKey
(
crop
))
{
tradeBarriers
.
incrementValue
(
crop
,
countryData
.
get
(
crop
));
}
else
{
tradeBarriers
.
incrementValue
(
crop
,
ModelConfig
.
TRADE_BARRIER_FACTOR_DEFAULT
);
}
}
n
++;
}
}
tradeBarriers
.
divideBy
(
n
);
// Take average. TODO weigh by population or demand
return
tradeMap
.
get
(
cc
);
tradeBarriers
.
put
(
CropType
.
ENERGY_CROPS
,
ModelConfig
.
TRADE_BARRIER_ENERGY_CROPS
);
return
tradeBarriers
;
}
}
...
@@ -66,26 +80,16 @@ public class TradeManager {
...
@@ -66,26 +80,16 @@ public class TradeManager {
countryCode
=
tokens
[
COUNTRY_COL
];
countryCode
=
tokens
[
COUNTRY_COL
];
itemName
=
tokens
[
ITEM_COL
];
itemName
=
tokens
[
ITEM_COL
];
barrierValue
=
Double
.
parseDouble
(
tokens
[
TRADE_BARRIER_COL
]);
barrierValue
=
Double
.
parseDouble
(
tokens
[
TRADE_BARRIER_COL
]);
if
(!
CountryManager
.
getInstance
().
isCountry
(
countryCode
))
{
continue
;
}
SingleCountry
country
=
CountryManager
.
getInstance
().
getForCode
(
countryCode
);
SingleCountry
country
=
CountryManager
.
getInstance
().
getForCode
(
countryCode
);
CropType
crop
=
CropType
.
getForFaoName
(
itemName
);
CropType
crop
=
CropType
.
getForFaoName
(
itemName
);
CropToDoubleMap
countryData
=
singleCountryBarriers
.
computeIfAbsent
(
country
,
k
->
new
CropToDoubleMap
());
CompositeCountry
cc
=
CountryManager
.
getInstance
().
getForSingleCountry
(
country
);
countryData
.
put
(
crop
,
barrierValue
);
if
(
cc
==
null
)
{
continue
;
}
else
{
CropToDoubleMap
countryData
=
tradeMap
.
get
(
cc
);
if
(
countryData
==
null
)
{
countryData
=
new
CropToDoubleMap
();
tradeMap
.
put
(
cc
,
countryData
);
}
countryData
.
incrementValue
(
crop
,
barrierValue
);
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
LogWriter
.
printlnError
(
"Failed in processing trade barriers line "
+
line
);
LogWriter
.
printlnError
(
"Failed in processing trade barriers line "
+
line
);
LogWriter
.
print
(
e
);
LogWriter
.
print
(
e
);
...
@@ -97,7 +101,7 @@ public class TradeManager {
...
@@ -97,7 +101,7 @@ public class TradeManager {
LogWriter
.
printlnError
(
"Failed in reading trade barriers data file"
);
LogWriter
.
printlnError
(
"Failed in reading trade barriers data file"
);
LogWriter
.
print
(
e
);
LogWriter
.
print
(
e
);
}
}
LogWriter
.
println
(
"Processed "
+
filename
+
", create "
+
tradeMap
.
size
()
+
" barriers entries"
);
LogWriter
.
println
(
"Processed "
+
filename
+
", create "
+
singleCountryBarriers
.
size
()
+
" barriers entries"
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment