/
crop.go
202 lines (180 loc) · 6.78 KB
/
crop.go
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package crops
import (
"errors"
"github.com/USACE/go-consequences/consequences"
"github.com/USACE/go-consequences/geography"
"github.com/USACE/go-consequences/hazards"
)
//Crop describes a crop that can be used to compute agricultural consequences
type Crop struct {
id byte
name string
substituteName string
SubstituteCrop Substitute
x float64
y float64
totalMarketValue float64 //Marketable value, yeild *pricePerUnit
productionFunction productionFunction
lossFunction DamageFunction
cropSchedule CropSchedule
}
type Substitute struct {
id byte
name string
totalMarketValue float64 //Marketable value, yeild *pricePerUnit
productionFunction productionFunction
lossFunction DamageFunction
cropSchedule CropSchedule
}
func (s Substitute) toCrop() Crop {
c := BuildCrop(s.id, s.name)
c.WithProductionFunction(s.productionFunction)
c.WithLossFunction(s.lossFunction)
c.WithCropSchedule(s.cropSchedule)
c.totalMarketValue = s.totalMarketValue
return c
}
func (c Crop) toSubstitute() Substitute {
s := Substitute{id: c.id, name: c.name, totalMarketValue: c.totalMarketValue, productionFunction: c.productionFunction, lossFunction: c.lossFunction, cropSchedule: c.cropSchedule}
return s
}
//BuildCrop builds a crop since the properties of crop are not exported
func BuildCrop(cropid byte, cropname string) Crop {
return Crop{id: cropid, name: cropname}
}
//WithLocation allows the construction of a location on a crop
func (c *Crop) WithLocation(xloc float64, yloc float64) Crop {
c.x = xloc
c.y = yloc
return *c
}
//WithOutput allows the setting of the yeild per acre and price per unit of output and resulting value per output
func (c *Crop) WithOutput(cropYeild float64, price float64) Crop {
c.totalMarketValue = cropYeild * price
return *c
}
//WithProductionFunction allows the setting of the production function
func (c *Crop) WithProductionFunction(pf productionFunction) Crop {
c.productionFunction = pf
return *c
}
//WithLossFunction allows the setting of the loss function
func (c *Crop) WithLossFunction(lf DamageFunction) Crop {
c.lossFunction = lf
return *c
}
//WithCropSchedule allows the setting of the cropschedule
func (c *Crop) WithCropSchedule(cs CropSchedule) Crop {
c.cropSchedule = cs
return *c
}
//GetCropID fulfils the crops.CropType interface
func (c Crop) GetCropID() byte {
return c.id
}
//GetCropName fulfils the crops.CropType interface
func (c Crop) GetCropName() string {
return c.name
}
//GetX fulfils the consequences.Locatable interface
func (c Crop) GetX() float64 {
return c.x
}
//GetY fulfils the consequences.Locatable interface
func (c Crop) GetY() float64 {
return c.y
}
func (c Crop) Location() geography.Location {
return geography.Location{X: c.x, Y: c.y}
}
//GetTotalMarketValue returns crop.totalMarketValue
func (c Crop) GetTotalMarketValue() float64 {
return c.totalMarketValue
}
//Compute implements concequence.Receptor on crop
func (c Crop) Compute(event hazards.HazardEvent) (consequences.Result, error) {
//Check event to determine if it is an arrival time and duration event
header := []string{"Crop", "x", "y", "Damage Outcome", "Damage", "Duration", "Arrival Time"}
results := []interface{}{c.name, c.Location().X, c.Location().Y, Unassigned.String(), 0.0, 0.0, ""}
var ret = consequences.Result{Headers: header, Result: results}
var err error = nil
da, ok := event.(hazards.ArrivalandDurationEvent)
if ok {
//determine cropdamageoutcome
outcome := c.cropSchedule.ComputeCropDamageCase(da)
results[5] = da.Duration()
results[6] = da.ArrivalTime().Format("Mon Jan 2 15:04:05")
//switch case on damageoutcome
//compute damages
damage := 0.0
switch outcome {
case Unassigned:
//huh?
damage = 0.0
err = errors.New("Damage Outcome was Unassigned")
case Impacted:
damage = c.computeImpactedCase(da)
case NotImpactedDuringSeason:
damage = 0.0
err = errors.New("Damage Outcome was Not Impacted During Season")
case PlantingDelayed:
damage = c.computeDelayedCase(da)
case NotPlanted:
damage, outcome = c.computeNotPlantedCase(da)
case SubstituteCrop:
// case for sbustitute crop not yet implemented
//get the substitute, and compute damages on it... hope for no infinate loop.
damage = c.computeSubstitueCase(da)
default:
damage = 0.0
err = errors.New("Damage Outcome resulted in Default case")
}
results[3] = outcome.String()
results[4] = damage
}
return ret, err
}
func (c Crop) computeImpactedCase(e hazards.ArrivalandDurationEvent) float64 {
// Determine crop damage percent based on damage dur curve and event dur
dmgfactor := c.lossFunction.ComputeDamagePercent(e) / 100
exposedProductionValue := c.productionFunction.GetExposedValue(e)
totalProductionCost := c.productionFunction.productionCostLessHarvest
percentProductionValue := exposedProductionValue / totalProductionCost
totalMarketValue := c.GetTotalMarketValue()
totalMarketValueLessHarvestCost := totalMarketValue - c.productionFunction.harvestCost
loss := dmgfactor * percentProductionValue * totalMarketValueLessHarvestCost
//fmt.Println("loss = ", loss)
return loss
}
func (c Crop) computeDelayedCase(e hazards.ArrivalandDurationEvent) float64 {
// delayed loss is equivalent to total marketable value less harvest cost, times the percent loss due to late planting
// Not using interpolated % loss for late plant
plantingWindow := (c.cropSchedule.LastPlantingDate.Sub(c.cropSchedule.StartPlantingDate).Hours() / 24)
//fmt.Println(plantingWindow)
actualPlant := (e.ArrivalTime().AddDate(0, 0, int(e.Duration())))
//fmt.Println(actualPlant)
daysLate := (actualPlant.Sub(c.cropSchedule.StartPlantingDate.AddDate(actualPlant.Year(), 0, 0))).Hours() / 24
//fmt.Println(daysLate)
factor := (daysLate / plantingWindow) * c.productionFunction.lossFromLatePlanting / 100
//fmt.Println("factor is : ", factor)
return c.GetTotalMarketValue() * factor
}
func (c Crop) computeNotPlantedCase(e hazards.ArrivalandDurationEvent) (float64, CropDamageCase) {
// Assume Loss is only fixed costs for entire year
if c.substituteName == "" {
return c.productionFunction.GetCumulativeMonthlyFixedCostsOnly()[11], NotPlanted
} else {
return c.computeSubstitueCase(e), SubstituteCrop
}
}
func (c Crop) computeSubstitueCase(e hazards.ArrivalandDurationEvent) float64 {
// TODO
//fmt.Println("compute substitute, TODO implement me.")
ocv := c.totalMarketValue - c.productionFunction.harvestCost
sc := c.SubstituteCrop.toCrop()
scv := sc.totalMarketValue - sc.productionFunction.harvestCost
if scv <= ocv {
return ocv - scv
}
return 0.0
}