-
Notifications
You must be signed in to change notification settings - Fork 1
/
simulate.py
176 lines (141 loc) · 5.03 KB
/
simulate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import datetime
import matplotlib.pyplot as plt
from cafe.farm import (
Config,
Event,
Farm,
guate_harvest_function,
predict_yield_for_farm,
)
def simulateCoOp(plotList, numYears, pruneYear=None, growthPattern=None, strategy=None):
"""
Uses a list of plots, `plotList`, to simulate a cooperative over `numFarms` number of years.
Returns a list of two lists: `simulation`
list one, `harvestYear`, represents the year range in the simulation.
list two, `annualHarvest`, represents the amount of coffee (in lbs) harvested for that year
"""
# numPlots = len(plotList)
annualHarvest = []
harvestYear = []
start_year = min([plot.start.year for plot in plotList])
# start_year = 2020
for current_year in range(start_year, start_year + numYears + 1):
configs = (
Config("e14", name="e14", output_per_crop=125, unit="cuerdas"),
Config("borbon", name="borbon", output_per_crop=200, unit="cuerdas"),
Config("catuai", name="catuai", output_per_crop=125, unit="cuerdas"),
Config("catura", name="catura", output_per_crop=125, unit="cuerdas"),
)
species_list = [config.species for config in configs]
scopes = {
species: {"type": "species", "def": species} for species in species_list
}
harvest_functions = {
"e14": guate_harvest_function(lifespan=15, mature=5),
"catura": guate_harvest_function(lifespan=16, mature=4),
"catuai": guate_harvest_function(lifespan=17, mature=4),
"borbon": guate_harvest_function(lifespan=30, mature=5),
}
events = [
Event(
name=f"{species} harvest",
impact=harvest_functions[species],
scope=scopes[species],
)
for species in species_list
]
start_year = datetime.datetime(2020, 1, 1)
end_year = datetime.datetime(2021, 1, 1)
events.append(
Event(
"catastrophic overfertilization",
impact=0.001,
scope={"type": "species", "def": "borbon"},
start=start_year,
end=end_year,
)
)
farm = Farm(plotList)
thisYearsHarvest = predict_yield_for_farm(
farm=farm,
configs=configs,
events=events,
time=datetime.datetime(current_year, 1, 1),
)
harvestYear.append(current_year)
# annualHarvest.append(thisYearsHarvest[0]) # inspect single plot
annualHarvest.append(sum(thisYearsHarvest))
simulation = [harvestYear, annualHarvest]
return simulation
def main(args):
import os
farmData = args.farm
# trees = args.trees
# strategy = args.strategy
years = args.years
output = args.output
if not os.path.exists(farmData):
raise ValueError(
(
f"File: {farmData} does not exist.\n"
"If you are running default commands and this is your first time"
"running the simulation, assure you have run:\n"
"`python3 src/cafe/fakeData.py --farms 100"
"--year 2020 --output data/fakeData.csv`\n"
"in the core directory before calling"
"simulateCoOp.py from the command line."
)
)
print("Importing Data")
farm_example = Farm.from_csv(farmData)
farmList = farm_example.plots
print("Simulating Cooperative")
simData = simulateCoOp(farmList, years)
print("Plotting")
pltYears, pltHarvests = simData
# get parameters for axes
mnYear, mxYear = min(pltYears), max(pltYears)
mxHarvest = max(pltHarvests)
plt.rcParams["figure.figsize"] = (20, 10)
fsize = 20 # font size
plt.axes(xlim=(mnYear, mxYear), ylim=(0, (mxHarvest + (mxHarvest * 0.10))))
plt.plot(pltYears, pltHarvests, linewidth=4)
plt.style.use("ggplot")
plt.title("Yield Forecast", fontsize=(fsize * 1.25))
plt.xlabel("Year", fontsize=fsize)
plt.xticks(pltYears, rotation=45)
plt.ylabel("Total pounds of green coffee produced", fontsize=fsize)
plt.savefig(output, dpi=100)
# plt.show()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Parse growth data for simulation.")
parser.add_argument(
"-f",
"--farm",
default="data/fakeData.csv",
type=str,
help="""
Path to data containing plot details.
e.g., cuerdas, tree types, etc.\n
""",
)
parser.add_argument(
"-y",
"--years",
default=75,
type=int,
help="""
Number of years that should be iterated
through in the simulation (default=30).\n
""",
)
parser.add_argument(
"-o",
"--output",
default="testNewFarm.png",
type=str,
help="Desired name of plot output file (default=testNewFarm.png).",
)
args = parser.parse_args()
main(args)