forked from bodzio528/FS22_CropRotation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCropRotationPlannerTest.lua
227 lines (188 loc) · 7.31 KB
/
CropRotationPlannerTest.lua
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
local verbose = false
local crops = {}
local matrix = {}
-- setup crop rotation data
local function test_setup()
crops = {
[1] = { returnPeriod = 2 }, -- BARLEY
[2] = { returnPeriod = 3 }, -- SOYBEAN
[3] = { returnPeriod = 3 }, -- CANOLA
[4] = { returnPeriod = 3 }, -- POTATO
[5] = { returnPeriod = 1 }, -- MAIZE
[6] = { returnPeriod = 1 }, -- GRASS
[7] = { returnPeriod = 2 }, -- WHEAT
[8] = { returnPeriod = 2 }, -- OAT
[9] = { returnPeriod = 2 } -- WEIRDOCROP (RP2, GOOD)
}
matrix = {
[1] = {[2] = 2, [3] = 2, [4] = 2},
[2] = {},
[3] = {[1] = 2, [2] = 2, [4] = 0, [8] = 2},
[4] = {[2] = 2, [3] = 0, [4] = 0},
[5] = {[2] = 2, [3] = 0},
[6] = {[6] = 2},
[7] = {[2] = 2, [3] = 2, [7] = 0, [8] = 1},
[8] = {[2] = 2, [3] = 2},
[9] = {[9] = 2}
}
end
-- mock crop rotation
local function getRotationForecropValue(past, current)
if past == 0 then
return 2
end
return matrix[current][past] or 1
end
local function getRotationForecropMultiplier(prevIndex, lastIndex, currentIndex)
local prevValue = getRotationForecropValue(prevIndex, currentIndex)
local lastValue = getRotationForecropValue(lastIndex, currentIndex)
local prevFactor = -0.025 * prevValue ^ 2 + 0.125 * prevValue -- <0.0 ; 0.15>
local lastFactor = -0.05 * lastValue ^ 2 + 0.25 * lastValue -- <0.0 ; 0.30>
return prevFactor + lastFactor + 0.7 -- <0.7 ; 1.15>
end
local function getRotationReturnPeriodMultiplier(prevIndex, lastIndex, currentIndex)
local returnPeriod = crops[currentIndex].returnPeriod
if returnPeriod == 2 then
-- monoculture
if prevIndex == lastIndex and lastIndex == currentIndex then
return 0.9
-- same as last
elseif lastIndex == currentIndex then
return 0.95
end
elseif returnPeriod == 3 then
-- monoculture
if prevIndex == lastIndex and lastIndex == currentIndex then
return 0.85
-- same as last
elseif lastIndex == currentIndex then
return 0.9
-- 1 year gap
elseif prevIndex == currentIndex and lastIndex ~= currentIndex then
return 0.95
end
end
return 1.0
end
local function getRotationYieldMultiplier(prevIndex, lastIndex, currentIndex)
local returnPeriod = getRotationReturnPeriodMultiplier(prevIndex, lastIndex, currentIndex)
local forecrops = getRotationForecropMultiplier(prevIndex, lastIndex, currentIndex)
return returnPeriod * forecrops
end
-- test framework
function match(expected, actual)
if #expected == #actual and table.concat(expected) == table.concat(actual) then
return true
else
print(string.format("expected table(%i) = {%s} is not equal to actual table(%i) = {%s}",
#expected, table.concat(expected, ", "),
#actual, table.concat(actual, ", ")))
return false
end
end
-- input: list of crop indices: {1, 2, 3} NOTE: indices must be consecutive natural numbers
-- output: list of multipliers: {1.15, 1.1, 1.0}
function planner(input)
if #input < 1 then return {} end
result = {}
for pos, current in pairs(input) do
if current ~= 0 then
lastPos = 1 + math.mod(pos - 1 - 1 + #input, #input)
prevPos = 1 + math.mod(pos - 2 - 1 + #input, #input)
last = input[lastPos]
prev = input[prevPos]
local mult = getRotationYieldMultiplier(prev, last, current)
table.insert(result, mult)
if verbose then
print(string.format("pos: %i => mult: %f curr: %i last: %i prev: %i", pos, mult, current, last, prev))
end
else -- degenerated
table.insert(result, 0)
end
end
return result
end
-- the tests
function test_plannerDegenerated_empty()
assert(match({}, planner({})))
end
local function test_plannerDegenerated_zeroes()
assert(match({0}, planner({0})))
assert(match({1.05, 0, 1.1}, planner({5, 0, 5})))
end
function test_plannerMonoculture1x()
assert(match({0.85}, planner({2}))) -- canola(RP3) monoculture
assert(match({0.90}, planner({1}))) -- barley(RP2) monoculture
assert(match({1.00}, planner({5}))) -- maize(RP1) monoculture
assert(match({0.595}, planner({4}))) -- potato(RP3, BAD) -- worst case
assert(match({0.63}, planner({7}))) -- wheat(RP2, BAD)
assert(match({1.035}, planner({9}))) -- weirdocrop(RP2, GOOD)
assert(match({1.15}, planner({6}))) -- grass(RP1, GOOD) -- best case
end
function test_plannerMonoculture2x()
assert(match({0.85, 0.85}, planner({2, 2}))) -- canola(RP3) monoculture
assert(match({0.90, 0.90}, planner({1, 1}))) -- barley(RP2) monoculture
assert(match({1.0, 1.0}, planner({5, 5}))) -- maize(RP1) monoculture
assert(match({0.595, 0.595}, planner({4, 4}))) -- potato(RP3, BAD) -- worst case
assert(match({0.63, 0.63}, planner({7, 7}))) -- wheat(RP2, BAD)
assert(match({1.035, 1.035}, planner({9, 9}))) -- weirdocrop(RP2, GOOD)
assert(match({1.15, 1.15}, planner({6, 6}))) -- grass(RP1, GOOD) -- best case
end
function test_plannerMonoculture3x()
assert(match({0.85, 0.85, 0.85}, planner({2, 2, 2}))) -- canola(RP3) monoculture
assert(match({0.90, 0.90, 0.90}, planner({1, 1, 1}))) -- barley(RP2) monoculture
assert(match({1.0, 1.0, 1.0}, planner({5, 5, 5}))) -- maize(RP1) monoculture
assert(match({0.595, 0.595, 0.595}, planner({4, 4, 4}))) -- potato(RP3, BAD) -- worst case
assert(match({0.63, 0.63, 0.63}, planner({7, 7, 7}))) -- wheat(RP2, BAD)
assert(match({1.035, 1.035, 1.035}, planner({9, 9, 9}))) -- weirdocrop(RP2, GOOD)
assert(match({1.15, 1.15, 1.15}, planner({6, 6, 6}))) -- grass(RP1, GOOD) -- best case
end
function test_plannerCereals2x()
assert(match({1.0, 1.0}, planner({1, 8}))) -- BARLEY/OAT => 1.0
end
function test_plannerCereals3x()
assert(match({1.0, 1.0, 1.0}, planner({1, 7, 8}))) -- BARLEY/WHEAT/OAT => 1.0
end
function test_plannerPerfect3x()
crops = { -- all crops have return period of 3 years
[1] = { returnPeriod = 3 },
[2] = { returnPeriod = 3 },
[3] = { returnPeriod = 3 }
}
matrix = { -- matrix of perfect forecrops
[1] = {[2] = 2, [3] = 2},
[2] = {[1] = 2, [3] = 2},
[3] = {[1] = 2, [2] = 2}
}
assert(match({1.15, 1.15, 1.15}, planner({1, 2, 3})))
end
function test_plannerFallow6x()
old_ = getRotationForecropValue
getRotationForecropValue = function (past, current)
if past == 0 then return 2 end
return matrix[current][past] or 1
end
assert(match({0, 0, 1.15, 0, 0, 1.15}, planner({0, 0, 1, 0, 0, 1})))
getRotationForecropValue = old_
end
local function run_unittests()
local test_functions = {
test_plannerDegenerated_empty,
test_plannerDegenerated_zeroes,
test_plannerMonoculture1x,
test_plannerMonoculture2x,
test_plannerMonoculture3x,
test_plannerCereals2x,
test_plannerCereals3x,
test_plannerPerfect3x,
test_plannerFallow6x
}
for i, test_func in pairs(test_functions) do
test_setup()
test_func()
end
end
run_unittests()
local t = {1,2,nil,4}
-- t[3] = nil
for i,v in pairs(t) do print(string.format("T[%i] = %d", i, v)) end