-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
702 lines (660 loc) · 23.6 KB
/
index.js
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
module.exports = (debugBounds) => { return `*-------------------------------
* Fruchtfolge
*
* A spatial crop rotation model
* serving as a base for the
* Fruchtfolge web application
* (c) Christoph Pahmeyer, 2019
*-------------------------------
*
* --- initiate global parameters for Greening evaluation
*
scalar p_totLand;
scalar p_totArabLand;
scalar p_totGreenLand;
scalar p_restLand;
scalar p_shareGreenLand;
scalar p_grassLandExempt;
p_totLand = sum(curPlots, p_plotData(curPlots,"size"));
p_totArabLand = sum(curPlots $ (not plots_permPast(curPlots)), p_plotData(curPlots,"size"));
p_totGreenLand = p_totLand - p_totArabLand;
p_restLand = p_totLand - p_totGreenLand;
p_shareGreenLand = p_totGreenLand / p_totLand;
p_grassLandExempt $((p_shareGreenLand > 0.75) $(p_restLand < 30)) = 1;
*
* --- initiate a cross set of all allowed combinations, might speed up generation time
*
$offOrder
set p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert);
p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
$ p_grossMarginData(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert,'grossMarginHa')
= YES;
alias (cropGroup,cropGroup1);
alias (curCrops,curCrops1);
scalar M / 99999 /;
*
* --- declare objective variable and equation
*
Variables
v_obje
v_totGM
;
Positive Variables
v_devShares(curCrops)
v_devEfa5
v_devEfa75
v_devEfa95
$iftheni.constraints defined constraints
v_devUserShares(constraints,curCrops,curCrops)
$endif.constraints
v_devOneCrop(curPlots)
$iftheni.labour defined p_availLabour
v_devLabour(months)
$endif.labour
;
Binary Variables
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
;
Equations
e_obje
e_totGM
;
*
* --- include model
*
Equations
e_maxShares(curCrops)
e_oneCropPlot(curPlots)
$iftheni.constraints defined constraints
e_minimumShares(constraints,curCrops,curCrops1)
e_maximumShares(constraints,curCrops,curCrops1)
$endif.constraints
;
*
* --- each crop cannot exceed the maximum allowed share specified by the users
* crop rotational settings
*
e_maxShares(curCrops) $ p_cropData(curCrops,"maxShare")..
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
$ (not plots_permPast(curPlots)),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,"size")
)
=L=
(p_totArabLand * p_cropData(curCrops,"maxShare") / 100)
+ v_devShares(curCrops)
;
*
* --- ensure that only one crop is grown on a plot
*
e_oneCropPlot(curPlots)..
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert))
+ v_devOneCrop(curPlots)
=E= 1
;
*
* --- prohibit growing a crop on a plot when there is no gross margin present
*
v_binCropPlot.up(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert) $ ((not
p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert))
$ (not plots_permPast(curPlots))) = 0;
*
* --- Enter user specified constraints into the model,
*
$iftheni.constraints defined constraints
e_minimumShares(constraints,curCrops,curCrops1)
$ (p_constraint(constraints,curCrops,curCrops1)
$ (not (constraints_lt(constraints,'lt'))))..
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,'size'))
+ v_devUserShares(constraints,curCrops,curCrops1)
=G= p_constraint(constraints,curCrops,curCrops1)
;
e_maximumShares(constraints,curCrops,curCrops1)
$ (p_constraint(constraints,curCrops,curCrops1)
$ (constraints_lt(constraints,'lt')))..
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,'size'))
+ sum(p_c_m_s_n_z_a(curPlots,curCrops1,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert),
v_binCropPlot(curPlots,curCrops1,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,'size'))
=L=
p_constraint(constraints,curCrops,curCrops1)
+ v_devUserShares(constraints,curCrops,curCrops1)
;
$endif.constraints
set manType / manure,solid /;
parameter p_excr(manType,man_attr);
p_excr("manure",man_attr) = p_manure(man_attr);
p_excr("solid",man_attr) = p_solid(man_attr);
positive variables
v_manExports(manType,months)
v_170Slack
v_170PlotSlack(curPlots)
v_20RedSlack
;
Equations
e_man_balance
e_170_avg
$$ifi "%duev2020%"=="true" e_170_plots(curPlots)
$$ifi "%duev2020%"=="true" e_20_red_plots
;
Parameter p_manValue(manType,manAmounts,solidAmounts) /
'manure'.'0'.set.solidAmounts 0
'manure'.'10'.set.solidAmounts 10
'manure'.'15'.set.solidAmounts 15
'manure'.'20'.set.solidAmounts 20
'manure'.'25'.set.solidAmounts 25
'manure'.'30'.set.solidAmounts 30
'manure'.'40'.set.solidAmounts 40
'manure'.'50'.set.solidAmounts 50
'manure'.'60'.set.solidAmounts 60
'solid'.set.manAmounts.'0' 0
'solid'.set.manAmounts.'5' 5
'solid'.set.manAmounts.'10' 10
'solid'.set.manAmounts.'12' 15
'solid'.set.manAmounts.'15' 20
'solid'.set.manAmounts.'20' 25
/;
e_man_balance(manType)..
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,"size")
* p_manValue(manType,manAmounts,solidAmounts))
+ sum(months, v_manExports(manType,months))
=E= p_excr(manType,"amount")
;
$iftheni.duev2020 "%duev2020%"=="true"
parameter p_notEndangeredLand;
p_notEndangeredLand = sum((curPlots)
$ (not plots_duevEndangered(curPlots)), p_plotData(curPlots,"size"));
e_170_avg $ p_notEndangeredLand..
sum((p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert),manType)
$ (not plots_duevEndangered(curPlots)),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,"size")
* p_manValue(manType,manAmounts,solidAmounts)
* p_manure("n")
* 80 / 100
) /p_notEndangeredLand =L= 170 + v_170Slack
;
* In "red areas", the 170kg N maximum rule is active for every single field,
* instead of the average of all fields
e_170_plots(curPlots) $ (plots_duevEndangered(curPlots) )..
sum((p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert),manType),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_manValue(manType,manAmounts,solidAmounts)
* p_manure("n")
* 80 / 100
) =L= 170 + v_170PlotSlack(curPlots)
;
* In addition to this, nitrogen fertilizer needs to be reduced by a minimum average of
* 20% on all fields in a "red area "of the farm
e_20_red_plots $ sum(curPlots $ plots_duevEndangered(curPlots), 1)..
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
$ plots_duevEndangered(curPlots),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* (ord(nReduction) - 1) * 10
* p_plotData(curPlots,"size")
)
/ sum(curPlots $ plots_duevEndangered(curPlots), p_plotData(curPlots,"size"))
=G= 20 - v_20RedSlack
;
$else.duev2020
e_170_avg..
sum((p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert),manType),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,"size")
* p_manValue(manType,manAmounts,solidAmounts)
* p_manure("n")
* 80 / 100
)/p_totLand =L= 170 + v_170Slack
$endif.duev2020
*
* --- Storage model
*
Positive Variables
v_curStorage(manType,months)
v_manureSpring(manType,months)
v_manureAutumn(manType)
v_manSlack(manType,months)
;
Equations
e_storageBal(manType,months)
e_manureSpring(manType)
e_manureAutumn
e_solidAutumn
e_maxStorageCap(manType,months)
;
Parameter p_monthlyManure(manType);
* We assume a steady flow of manure into the storages
p_monthlyManure("manure") = p_manure("amount") / 12;
p_monthlyManure("solid") = p_solid("amount") / 12;
* We also assume, that the maximum storage capacity is equal to the monthly
* flow multiplied with the 6 month minimum storage capacity requried by the Fert. Ordinance
Parameter p_maxStoreCap(manType);
p_maxStoreCap("manure") = manStorage;
p_maxStoreCap("solid") = solidStorage;
Parameter p_springManMonths(manType,months) /
manure.feb 0.667
manure.mrz 0.167
manure.apr 0.167
solid.feb 0.333
solid.mrz 0.333
solid.apr 0.333
/;
Parameter p_priceFertExport(manType,months);
p_priceFertExport("manure",months) $ (ord(months) < 6) = manPriceSpring;
p_priceFertExport("manure",months) $ (ord(months) > 5) = manPriceAutumn;
p_priceFertExport("solid",months) $ (ord(months) < 6) = solidPriceSpring;
p_priceFertExport("solid",months) $ (ord(months) > 5) = solidPriceAutumn;
*
* --- We model 3 timepoints:
* 1. The amount of manure in storage at 31.01
* 2. The amount of manure in storage after spring
* 3. The amount of manure in storage after 01.10
*
e_manureSpring(manType)..
sum(months, v_manureSpring(manType,months)) =E=
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,"size")
* p_manValue(manType,manAmounts,solidAmounts)
)
;
* Manure spreading can only be done in these months
set manMonths / feb, mrz, apr, mai /;
v_manureSpring.up(manType,months) = 0;
v_manureSpring.up(manType,months)
$ sum(manMonths $ (sameas(manMonths,months)),1) = +inf;
e_manureAutumn..
v_manureAutumn("manure") =E=
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
$ (ord(manAmounts) > 1),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,"size")
* p_grossMarginData(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert,'autumnFertm3')
)
;
e_solidAutumn..
v_manureAutumn("solid") =E=
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
$ (ord(manAmounts) eq 1),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,"size")
* p_grossMarginData(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert,'autumnFertm3')
)
;
e_storageBal(manType,months)..
v_curStorage(manType,months) =E=
* The storage level of the previous month
v_curStorage(manType,months - 1)
* The monthly in-flow
+ p_monthlyManure(manType) $ (not sameas(months,"jan"))
* We assume that the storage is at capacity at the 31.01
+ p_maxStoreCap(manType) $ sameas(months,"jan")
* In these months manure is assumed to be spreaded
- v_manureSpring(manType,months)
* Exports can be done in April (cheaper) or September
- v_manExports(manType,months) $ (sameas(months,"apr") or sameas(months,"sep"))
* Autumn manure spreading is only assumed to be done in September
- v_manureAutumn(manType) $ sameas(months,"sep")
* Manure slack for infes treatment
+ v_manSlack(manType,months)
;
e_maxStorageCap(manType,months)..
v_curStorage(manType,months) =L=
* at the 01.10, the storage needs to have at least a capacity of 4 months left
p_maxStoreCap(manType)
- (3 * p_monthlyManure(manType)) $ sameas(months,"okt")
;
Equations
e_efa
e_75diversification(cropGroup)
e_95diversification(cropGroup,cropGroup1)
;
* Only activate ecological focus area equation if arable land is greater than 15ha
e_efa $ ((p_totArabLand >= 15) $(not p_grassLandExempt))..
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,"size")
* p_grossMarginData(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert,'efaFactor')
)
+ v_devEfa5
=G=
p_totArabLand * 0.05
;
* Only activate 75% diversifaction rule if arable land is greater than 10ha
e_75diversification(cropGroup) $ ((p_totArabLand >= 10) $(not p_grassLandExempt))..
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
$ ((not plots_permPast(curPlots))
$ crops_cropGroup(curCrops,cropGroup)),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,"size")
)
=L=
p_totArabLand * 0.75
+ v_devEfa75
;
* Only activate 95% diversifaction rule if arable land is greater than 30ha
e_95diversification(cropGroup,cropGroup1)
$ ((p_totArabLand >= 30)
$ (not p_grassLandExempt)
$ (not sameas(cropGroup,cropGroup1)))..
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
$ crops_cropGroup(curCrops,cropGroup),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,"size")
)
+
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
$ crops_cropGroup(curCrops,cropGroup1),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_plotData(curPlots,"size")
)
=L=
p_totArabLand * 0.95
+ v_devEfa95
;
*
* --- Only account for labour constraints when parameter is defined
*
$iftheni.labour defined p_availLabour
Equations
e_maxLabour(months)
* e_maxFieldWorkDays(months)
;
e_maxLabour(months)..
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_grossMarginData(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert,months)
)
=L=
p_availLabour(months)
+ v_devLabour(months)
;
*
* --- we assume a maximum of 14h of work per day
*
$ontext
e_maxFieldWorkDays(months)..
sum((curPlots,curCrops,halfMonths) $ months_halfMonths(months,halfMonths),
v_binCropPlot(curCrops,curPlots)
* p_tractorReq(crops,halfMonths)
=L= p_availFieldWorkDays(months) * p_availTractHours(month)
;
$offtext
$endif.labour
*
* --- calculate overall gross margin for the planning year
*
e_totGM..
v_totGM =E=
sum(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert),
v_binCropPlot(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert)
* p_grossMarginData(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert,'grossMarginHa')
* p_plotData(curPlots,'size')
)
- sum((manType,months), v_manExports(manType,months) * p_priceFertExport(manType,months));
e_obje..
v_obje =E=
v_totGM
- sum(curCrops, v_devShares(curCrops) * M)
- (v_devEfa5 * M)
- (v_devEfa75 * M)
- (v_devEfa95 * M)
- sum(curPlots, v_devOneCrop(curPlots) * M * 10)
- (sum((manType,months), v_manSlack(manType,months) * M))
- (v_170Slack * M)
- ((sum((manType,curPlots), v_170PlotSlack(curPlots))) * M)
- (v_20RedSlack * M)
$iftheni.constraints defined constraints
- sum((constraints,curCrops,curCrops1),
v_devUserShares(constraints,curCrops,curCrops1) * M)
$endif.constraints
$iftheni.labour defined p_availLabour
- sum(months, v_devLabour(months) * 1000)
$endif.labour
;
*
* --- define upper bounds for slack variables
*
v_devShares.up(curCrops) = p_totArabLand;
v_devEfa5.up = p_totArabLand * 0.05;
v_devEfa75.up = p_totArabLand * 0.25;
v_devEfa95.up = p_totArabLand;
v_devOneCrop.up(curPlots) = 1;
$iftheni.constraints defined constraints
v_devUserShares.up(constraints,curCrops,curCrops1) = p_totArabLand;
$endif.constraints
$iftheni.labour defined p_availLabour
v_devLabour.up(months) = 15000;
$endif.labour
if (card(curPlots)<30,
option optCR=0.0;
elseif card(curPlots)<50,
option optCR=0.02;
else
option optCR=0.04;
);
model Fruchtfolge /
e_obje
e_totGM
e_maxShares
e_oneCropPlot
* e_man_balance
e_170_avg
$$ifi "%duev2020%"=="true" e_170_plots
$$ifi "%duev2020%"=="true" e_20_red_plots
e_storageBal
e_manureSpring
e_manureAutumn
e_solidAutumn
e_maxStorageCap
$iftheni.constraints defined constraints
e_minimumShares
e_maximumShares
$endif.constraints
e_efa
e_75diversification
e_95diversification
$iftheni.labour defined p_availLabour
e_maxLabour
$endif.labour
/;
${debugBounds}
solve Fruchtfolge using MIP maximizing v_obje;
set fullMonths /
'Januar'
'Februar'
'März'
'April'
'Mai'
'Juni'
'Juli'
'August'
'September'
'Oktober'
'November'
'Dezember'
/;
set bool / true, false /;
set toBool(*,bool) /
catchCrop.true YES
noCatchCrop.false YES
autumnFert.true YES
noAutumnFert.false YES
/;
scalar
warningsCount
curWarning
;
warningsCount = sum(curCrops $ v_devShares.l(curCrops), 1)
+ 1 $ v_devEfa5.l
+ 1 $ v_devEfa75.l
+ 1 $ v_devEfa95.l
+ sum(curPlots $ v_devOneCrop.l(curPlots), 1)
+ sum((manType,months) $ v_manSlack.l(manType,months), 1)
+ 1 $ v_170Slack.l
+ sum((curPlots) $ v_170PlotSlack.l(curPlots), 1)
+ 1 $ v_20RedSlack.l
$$iftheni.constraints defined constraints
+ sum((constraints,curCrops,curCrops1) $ v_devUserShares.l(constraints,curCrops,curCrops1), 1)
$$endif.constraints
$$iftheni.labour defined p_availLabour
+ sum(months $ v_devLabour.l(months), 1)
$$endif.labour
;
curWarning = 0;
display warningsCount,curWarning;
File results / "%random%" /;
results.lw = 0;
put results;
put "{"
put '"model_status":', Fruchtfolge.modelstat, "," /;
put '"solver_status":', Fruchtfolge.solvestat, "," /;
if ( ((Fruchtfolge.modelstat ne 1) and (Fruchtfolge.modelstat ne 8)),
put '"error_message": "Infeasible model."' /;
ELSE
put '"objective":', v_totGM.l, "," /;
* add warnings if slack variables have non 0 levels
put '"warnings": [' /;
if ((sum(curCrops, v_devShares.l(curCrops)) > 0),
loop(curCrops $ v_devShares.l(curCrops),
put$(v_devShares.l(curCrops) > 0) '"Maximaler Fruchtfolgeanteil von cropId::', curCrops.tl, ':: konnte nicht eingehalten werden."'/;
curWarning = curWarning + 1;
put$(curWarning < warningsCount) "," /;
)
);
if ((v_devEfa5.l > 0),
put '"Konnte 5% ÖVF nicht einhalten. Prüfen, ob Sommerungen vorhanden sind, bzw. ob ZF Anbau erlaubt wurde."' /;
curWarning = curWarning + 1;
put$(curWarning < warningsCount) "," /;
);
if ((v_devEfa75.l > 0),
put '"Konnte 75% Greening-Regel nicht einhalten"' /;
curWarning = curWarning + 1;
put$(curWarning < warningsCount) "," /;
);
if ((v_devEfa95.l > 0),
put '"Konnte 95% Greening-Regel nicht einhalten"' /;
curWarning = curWarning + 1;
put$(curWarning < warningsCount) "," /;
);
if ((sum(curPlots, v_devOneCrop.l(curPlots)) > 0),
loop(curPlots $ v_devOneCrop.l(curPlots),
put$(v_devOneCrop.l(curPlots) > 0) '"Keine mögliche Nachfrucht für plotId::', curPlots.tl, ':: mit den aktuellen Anbaupause/Nachfruchtwirkungen."'/;
curWarning = curWarning + 1;
put$(curWarning < warningsCount) "," /;
)
);
if ((sum((manType,months), v_manSlack.l(manType,months)) > 0),
loop(manType,
loop (months $ v_manSlack.l(manType,months),
put$(v_manSlack.l(manType,months) > 0) '"Nicht ausreichend org. Dünger (', v_manSlack.l(manType,months), 'm3) im Monat ', months.tl, '"' /;
curWarning $ (v_manSlack.l(manType,months) > 0) = curWarning + 1;
put$(curWarning < warningsCount) "," /;
);
)
);
if ((v_170Slack.l > 0),
put '"Konnte 170kg org. N-Dünger-Regel nicht einhalten"' /;
curWarning = curWarning + 1;
put$(curWarning < warningsCount) "," /;
);
if ((v_20RedSlack.l > 0),
put '"Konnte 20% N-Reduktion im roten Gebiet nicht einhalten"' /;
curWarning = curWarning + 1;
put$(curWarning < warningsCount) "," /;
);
if ((sum(curPlots, v_170PlotSlack.l(curPlots)) > 0),
loop(curPlots $ v_170PlotSlack.l(curPlots),
put$(v_170PlotSlack.l(curPlots) > 0) '"Konnte 170kg org. N-Dünger-Regel für plotId::', curPlots.tl, ':: nicht einhalten (', v_170PlotSlack.l(curPlots) ,')."'/;
curWarning = curWarning + 1;
put$(curWarning < warningsCount) "," /;
)
);
$$iftheni.constraints defined constraints
if ((sum((constraints,curCrops,curCrops1), v_devUserShares.l(constraints,curCrops,curCrops1)) > 0),
loop((constraints,curCrops,curCrops1) $ v_devUserShares.l(constraints,curCrops,curCrops1),
put$(v_devUserShares.l(constraints,curCrops,curCrops1) > 0) '"Konnte Restriktion für constraintId::', constraints.tl, ':: nicht einhalten."'/;
curWarning = curWarning + 1;
put$(curWarning < warningsCount) "," /;
)
);
$$endif.constraints
$$iftheni.labour defined p_availLabour
if ((sum(months, v_devLabour.l(months)) > 0),
loop((months,fullMonths) $ ( v_devLabour.l(months) $ (months.pos eq fullMonths.pos)),
put$(v_devLabour.l(months) > 0) '"Konnte maximale Arbeitszeit für ', fullMonths.tl, ' nicht einhalten."'/;
curWarning = curWarning + 1;
put$(curWarning < warningsCount) "," /;
)
);
$$endif.labour
put '],' /;
* write recommendations from optimisation to JSON file
put '"recommendation":', "{"/;
loop(curPlots,
loop(p_c_m_s_n_z_a(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert),
put$(v_binCropPlot.l(curPlots,curCrops,manAmounts,solidAmounts,nReduction,catchCrop,autumnFert) > 0)
'"', curPlots.tl, '": {',
'"crop": "', curCrops.tl, '",',
'"manAmount": ', manAmounts.tl, ',',
'"solidAmount": ', solidAmounts.tl, ',',
'"nReduction": ', nReduction.tl, ',',
'"catchCrop":', catchCrop.tl, ',',
'"autumnFert":', autumnFert.tl,
'}' /
);
* when there is no crop recommendation, fill with first crop and zero fertilisation
if((v_devOneCrop.l(curPlots) > 0),
put '"', curPlots.tl, '": {' /;
loop(curCrops $ (curCrops.pos eq 2),
put '"crop": "', curCrops.tl, '",' /;
)
put '"manAmount": 0,',
'"solidAmount": 0,',
'"nReduction": 0,',
'"catchCrop": false,',
'"autumnFert": false',
'}' /;
);
put$(curPlots.pos < card(curPlots)) "," /
);
put "}," /;
put '"storage": {' /;
put '"manure": [' /;
loop (months,
put v_curStorage.l("manure",months) /;
put$(months.pos < card(months)) "," /;
);
put '],' /;
put '"solid": [' /;
loop (months,
put v_curStorage.l("solid",months) /;
put$(months.pos < card(months)) "," /;
);
put ']' /;
put "}," /;
put '"exports": {' /;
put '"manure": [' /;
loop (months,
put v_manExports.l("manure",months) /;
put$(months.pos < card(months)) "," /;
);
put '],' /;
put '"solid": [' /;
loop (months,
put v_manExports.l("solid",months) /;
put$(months.pos < card(months)) "," /;
);
put ']' /;
put "}" /;
);
put "}" /;
putclose;
`}