/
weaning.go
406 lines (309 loc) · 13.3 KB
/
weaning.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// weaning
/*
Copyright 2021 Bruce Golden and Matt Spangler
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package ecoIndex
import (
"fmt"
"math"
"github.com/blgolden/iGenDecModel/iGenDec/animal"
"github.com/blgolden/iGenDecModel/iGenDec/logger"
"gonum.org/v1/gonum/mat"
)
//var grossRevenueByYearWeanedCalves []float64 // Returns
var variableCostsByYearWeanedCalves []float64
var variableCostsByYearCows map[int]float64
type weaningGrossRevenueByYear_t struct {
nSteers float64
SteerRevenue float64
DiscountedSteerRevenue float64
wtSteers float64
nHeifers float64
HeiferRevenue float64
DiscountedHeiferRevenue float64
wtHeifers float64
nDead int
}
type weaningGrossCostsByYear_t struct {
SteerCosts float64
DiscountedSteerCosts float64
HeiferCosts float64
DiscountedHeiferCosts float64
}
var weaningGrossRevenueByYear map[int]weaningGrossRevenueByYear_t
var weaningGrossCostsByYear map[int]weaningGrossCostsByYear_t
type cullCowRevenueByYear_t struct {
nCowsOpen float64
nCowsOld float64
CowRevenue float64
DiscountedCowRevenue float64
}
var cullCowGrosRevenueByYear map[int]cullCowRevenueByYear_t
// calculate the cull cow sales revenue
// Calculate animals total weaning sale revenue
func weaningSaleRevenue(calf animal.Animal) (salePrice float64) {
weight, ok := animal.WeaningWtPhenotype(calf)
if !ok {
fmt.Println("Calf ", calf.Id, "can't get weaning weight. YearBorn:", calf.YearBorn)
}
pricePerPound := getPricePerPound(weight, calf.Sex, "WW")
salePrice = weight * pricePerPound
return salePrice
}
// Cost to raise a weanling calf by year
func calculateWeaningCostsByYear() {
weaningGrossCostsByYear = make(map[int]weaningGrossCostsByYear_t)
for _, calf := range animal.Records {
/*if calf.YearBorn == 1 {
calf.Dead = 0
}*/
yearWeaned := int(float64(calf.BirthDate)+205.) / 365
if calf.YearBorn >= 1 && calf.Dead == 0 {
if calf.Sex == animal.Steer {
for _, a := range calf.AumToWeaning {
//c := weaningGrossCostsByYear[a.Year]
c := weaningGrossCostsByYear[yearWeaned]
c.SteerCosts += a.Aum * AumCost[a.MonthOfYear-1]
weaningGrossCostsByYear[yearWeaned] = c
}
} else if calf.Sex == animal.Heifer {
for _, a := range calf.AumToWeaning {
c := weaningGrossCostsByYear[yearWeaned]
c.HeiferCosts += a.Aum * AumCost[a.MonthOfYear-1]
weaningGrossCostsByYear[yearWeaned] = c
}
}
}
}
return
}
// Revenue from sale of weaning calves
func calculateWeaningRevenueByYear() {
weaningGrossRevenueByYear = make(map[int]weaningGrossRevenueByYear_t)
for _, calf := range animal.Records {
yearWeaned := int(calf.BirthDate+205) / 365
if calf.YearBorn >= 1 && calf.Dead == 0 {
if calf.Sex == animal.Steer {
w := weaningGrossRevenueByYear[yearWeaned]
w.nSteers++
w.SteerRevenue += weaningSaleRevenue(calf)
p, _ := animal.Phenotype(calf, "WW")
w.wtSteers += p
weaningGrossRevenueByYear[yearWeaned] = w
} else if calf.Sex == animal.Heifer { // This works because sex has been set to Cow if she became a replacement
w := weaningGrossRevenueByYear[yearWeaned]
w.nHeifers++
w.HeiferRevenue += weaningSaleRevenue(calf)
p, _ := animal.Phenotype(calf, "WW")
w.wtHeifers += p
weaningGrossRevenueByYear[yearWeaned] = w
}
} else {
w := weaningGrossRevenueByYear[yearWeaned]
w.nDead++
weaningGrossRevenueByYear[yearWeaned] = w
}
}
return
}
// Determine the discounted value of the revenue
func calculateDiscountedWeaningRevenueByYear(nYears int) {
for y := animal.Burnin + 1; y <= nYears; y++ {
period := float64(y - StartYearOfNetReturns)
w := weaningGrossRevenueByYear[y]
df := math.Pow(1.+DiscountRate, period)
w.DiscountedSteerRevenue = w.SteerRevenue / df
w.DiscountedHeiferRevenue = w.HeiferRevenue / df
weaningGrossRevenueByYear[y] = w
}
}
// Determine the discounted value of the costs
func calculateDiscountedWeaningCostsByYear(nYears int) {
for y := animal.Burnin + 1; y <= nYears; y++ {
period := float64(y - StartYearOfNetReturns)
df := math.Pow(1.+DiscountRate, period)
c := weaningGrossCostsByYear[y]
c.DiscountedSteerCosts = c.SteerCosts / df
c.DiscountedHeiferCosts = c.HeiferCosts / df
weaningGrossCostsByYear[y] = c
}
}
// This is used to caalculate the weaning costs per year for
// all indexes except weaning, because weaning does it below in the
// net returns calculation.
func WeaningVariableCostsPerMating(nYears int) float64 {
var TotalDiscountedCosts float64
var TotalDiscountedCostsPerMating float64
if *logger.OutputMode == "verbose" {
fmt.Println("\nDiscounted Costs of Weanling Calf:")
fmt.Println(" Costs_______________________ ")
fmt.Println("Year $ Actual $ Discounted $ cum/Exposure N Cows Exposed")
}
for y := StartYearOfNetReturns; y <= nYears; y++ {
TotalDiscountedCosts += weaningGrossCostsByYear[y].DiscountedSteerCosts + weaningGrossCostsByYear[y].DiscountedHeiferCosts
TotalDiscountedCostsPerMating += (weaningGrossCostsByYear[y].DiscountedSteerCosts + weaningGrossCostsByYear[y].DiscountedHeiferCosts) /
float64(animal.CowsExposedPerYear[y])
if *logger.OutputMode == "verbose" {
fmt.Printf("%5d %10.2f %10.2f %10.2f %7d\n", y,
weaningGrossCostsByYear[y].SteerCosts+weaningGrossCostsByYear[y].HeiferCosts,
weaningGrossCostsByYear[y].DiscountedSteerCosts+weaningGrossCostsByYear[y].DiscountedHeiferCosts,
TotalDiscountedCostsPerMating,
animal.CowsExposedPerYear[y])
}
}
return TotalDiscountedCostsPerMating / float64(nYears-StartYearOfNetReturns+1)
}
// The total discounted net returns to fixed costs for weaning
// and optionally write a table to stdout
// Returns average cost/mating
func WeaningNetReturnsToFixedCosts(nYears int) float64 {
var TotalDiscountedRevenue float64
var TotalDiscountedNetRevenuePerMating float64
if *logger.OutputMode == "verbose" {
fmt.Println("\nReturns and Costs from Weanling Calf Sales:")
fmt.Println(" Returns_____________________ Costs_______________________ ")
fmt.Println("Year $ Actual $ Discounted $ Actual $ Discounted $ Net/Exposure N Cows Exposed")
}
for y := StartYearOfNetReturns; y <= nYears; y++ {
TotalDiscountedRevenue += weaningGrossRevenueByYear[y].DiscountedSteerRevenue + weaningGrossRevenueByYear[y].DiscountedHeiferRevenue
netPerExposure := (weaningGrossRevenueByYear[y].DiscountedSteerRevenue + weaningGrossRevenueByYear[y].DiscountedHeiferRevenue -
weaningGrossCostsByYear[y].DiscountedSteerCosts - weaningGrossCostsByYear[y].DiscountedHeiferCosts) /
float64(animal.CowsExposedPerYear[y])
TotalDiscountedNetRevenuePerMating += netPerExposure
if *logger.OutputMode == "verbose" {
fmt.Printf("%5d %10.2f %10.2f %10.2f %10.2f %10.2f %7d\n", y,
weaningGrossRevenueByYear[y].SteerRevenue+weaningGrossRevenueByYear[y].HeiferRevenue,
weaningGrossRevenueByYear[y].DiscountedSteerRevenue+weaningGrossRevenueByYear[y].DiscountedHeiferRevenue,
weaningGrossCostsByYear[y].SteerCosts+weaningGrossCostsByYear[y].HeiferCosts,
weaningGrossCostsByYear[y].DiscountedSteerCosts+weaningGrossCostsByYear[y].DiscountedHeiferCosts,
netPerExposure,
animal.CowsExposedPerYear[y])
}
}
return TotalDiscountedNetRevenuePerMating / float64(nYears-StartYearOfNetReturns+1)
}
// Value calves at weaning
func weaningSale(nYears int) float64 {
if *logger.OutputMode == "verbose" {
fmt.Println("Processing weaning sale net returns...")
}
calculateWeaningRevenueByYear()
calculateDiscountedWeaningRevenueByYear(nYears)
calculateWeaningCostsByYear()
calculateDiscountedWeaningCostsByYear(nYears)
if *logger.OutputMode == "verbose" {
fmt.Println("\nRevenue from weaning calf sales:")
fmt.Println("Sale Year n Steers Steer $ Wt Steers n Heifers Heifer $ Wt Heifers n Dead")
for year := 1; year <= nYears; year++ {
fmt.Printf(" %5d %5d %12.2f %12.1f %5d %12.2f %12.1f %7d\n", year, int(weaningGrossRevenueByYear[year].nSteers),
weaningGrossRevenueByYear[year].SteerRevenue, weaningGrossRevenueByYear[year].wtSteers,
int(weaningGrossRevenueByYear[year].nHeifers), weaningGrossRevenueByYear[year].HeiferRevenue, weaningGrossRevenueByYear[year].wtHeifers,
weaningGrossRevenueByYear[year].nDead)
}
}
return WeaningNetReturnsToFixedCosts(nYears) // This returns total accumulated net returns/mating
}
// Determine the net revenue from cull cow sale
func cullSale(nYears int) float64 {
cullCowGrosRevenueByYear = make(map[int]cullCowRevenueByYear_t)
if *logger.OutputMode == "verbose" {
fmt.Println("\nRevenue from cull cow sales:")
fmt.Println("Year Tot Weight $ Revenue $ DiscountedRev N CullsOpen N CullsOld")
}
var TotalDiscountedNetRevenuePerMating float64
for y := StartYearOfNetReturns; y <= nYears; y++ {
/*
var tsmm TraitSexMinWtMaxWt_t
tsmm.MaxWt = 9999.0
tsmm.MinWt = 0.0
tsmm.Sex = animal.Cow
tsmm.Trait = "MW"
*/
pricePerPound := getPricePerPound(1000.0, animal.Cow, "MW") // 1000 just to get the number. Don't need actual
c := cullCowGrosRevenueByYear[y]
c.CowRevenue = animal.WtCullCows[y].CumWt * pricePerPound
c.nCowsOpen = animal.WtCullCows[y].NheadOpen
c.nCowsOld = animal.WtCullCows[y].NheadOld
period := float64(y - StartYearOfNetReturns)
c.DiscountedCowRevenue = c.CowRevenue / math.Pow(1.+DiscountRate, period)
cullCowGrosRevenueByYear[y] = c
TotalDiscountedNetRevenuePerMating += c.DiscountedCowRevenue / float64(animal.CowsExposedPerYear[y])
if *logger.OutputMode == "verbose" {
fmt.Printf("%5d %10.0f %10.2f %10.2f %10.0f %10.0f\n", y, animal.WtCullCows[y].CumWt, c.CowRevenue, c.DiscountedCowRevenue, c.nCowsOpen, c.nCowsOld)
}
}
return TotalDiscountedNetRevenuePerMating / float64(nYears-StartYearOfNetReturns+1)
}
// Cost per year for cows
func CowCosts(nYears int) float64 {
variableCostsByYearCows = make(map[int]float64)
for _, c := range animal.Records {
if c.Sex == animal.Cow && c.YearBorn > 0 {
for _, a := range c.CowAum {
if a.Year >= StartYearOfNetReturns {
variableCostsByYearCows[a.Year] += a.Aum * AumCost[a.MonthOfYear-1]
}
}
}
}
if *logger.OutputMode == "verbose" {
fmt.Println("\nCow costs:")
fmt.Println("Year $ Cost $ Discounted $ Net/Exp")
}
var cumDc float64
for y := StartYearOfNetReturns; y <= nYears; y++ {
period := float64(y - StartYearOfNetReturns)
df := math.Pow(1.+DiscountRate, period)
dr := variableCostsByYearCows[y] / df
netPerExposure := dr / float64(animal.CowsExposedPerYear[y])
cumDc += netPerExposure
if *logger.OutputMode == "verbose" {
fmt.Printf("%5d %10.2f %10.2f %10.2f\n", y, variableCostsByYearCows[y], dr, netPerExposure)
}
}
return cumDc / float64(nYears-StartYearOfNetReturns+1)
}
// Process an index with sale at weaning
func EvaluateWeaningIndex(nYears int, burninMarker int, param map[string]interface{}, gvCholesky mat.Cholesky, rvCholesky mat.Cholesky) float64 {
var base animal.Component_t
base.Component = "D"
base.TraitName = "base"
IndexNetReturns := weaningSale(nYears) // Discounted and per mating
IndexNetReturns += cullSale(nYears) // Discounted and per mating
IndexNetReturns -= CowCosts(nYears)
if *logger.OutputMode == "verbose" {
fmt.Printf("\nPlanning Horizon (in years): %12d\n", nYears-StartYearOfNetReturns+1)
//fmt.Printf("%d Year Discounted Net Returns to land, management and labor per exp accum: %12.2f\n", nYears-StartYearOfNetReturns+1, NetReturns)
fmt.Printf("%d year Discounted Net Returns to land, management and labor per exposure: %12.2f\n", nYears-StartYearOfNetReturns+1,
IndexNetReturns)
//} else {
//}
fmt.Println("NOTE: all net values are returns to land, management and labor")
}
return IndexNetReturns
}
// Average weaning costs per mating accross years of simulation
// Not used by weaning index but used by bg, fat cattle, and grade and yield
func calculateWeaningCostsPerMating(nYears int) float64 {
var totalDiscountedWeaningCostPerMating float64
for y := StartYearOfNetReturns; y <= nYears; y++ {
totalDiscountedWeaningCostPerMating += (weaningGrossCostsByYear[y].DiscountedSteerCosts + weaningGrossCostsByYear[y].DiscountedHeiferCosts) /
float64(animal.CowsExposedPerYear[y])
}
return totalDiscountedWeaningCostPerMating / float64(nYears-StartYearOfNetReturns+1)
}