Skip to content
Snippets Groups Projects
Commit 10ef6d14 authored by Bart Arendarczyk's avatar Bart Arendarczyk
Browse files

Fixed calculation issue with trade barriers.

parent 22095f91
No related branches found
No related tags found
No related merge requests found
...@@ -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);
}
} }
...@@ -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<CompositeCountry, CropToDoubleMap> tradeMap = new HashMap<CompositeCountry, CropToDoubleMap>(); private Map<SingleCountry, 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");
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment