feat: kæden planlægger hø og ensilage — græs med produkt, mangel/afvisning pr. vare (v1.62.29)

This commit is contained in:
2026-10-05 14:01:11 +02:00
parent 59022e99d9
commit 9971cdde8d
12 changed files with 144 additions and 61 deletions
+25 -9
View File
@@ -14,26 +14,40 @@ function ADCropPlan.plannedFruit(field)
return field.fruit
end
function ADCropPlan.shortages(demand, stock, fields, yields)
-- Markens produkt for slåafgrøder: "auto" (standard) er ensilage.
function ADCropPlan.fieldProduct(field)
local product = field.product
if product == nil or product == "" or product == "auto" then return "silage" end
return product
end
-- demand/stock/yields er pr. afgrøde, eller pr. markvare når products[nøgle] = {fruit, product} (hø = græs med
-- produkt hø): så tæller kun marker med samme afgrøde OG produkt som udbud, og manglen bærer afgrøde + produkt.
function ADCropPlan.shortages(demand, stock, fields, yields, products)
local list = {}
for fruit, perMonth in pairs(demand or {}) do
local perHa = (yields or {})[fruit] or 0
for key, perMonth in pairs(demand or {}) do
local perHa = (yields or {})[key] or 0
local spec = (products or {})[key]
local fruit, product = spec ~= nil and spec.fruit or key, spec ~= nil and spec.product or nil
if perHa > 0 and (perMonth or 0) > 0 then
local need = perMonth * ADCropPlan.HORIZON_MONTHS
local supply = (stock or {})[fruit] or 0
local supply = (stock or {})[key] or 0
for _, field in ipairs(fields or {}) do
if ADCropPlan.plannedFruit(field) == fruit then supply = supply + (field.areaHa or 0) * perHa end
if ADCropPlan.plannedFruit(field) == fruit and (product == nil or ADCropPlan.fieldProduct(field) == product) then
supply = supply + (field.areaHa or 0) * perHa
end
end
local missing = need - supply
local missingHa = missing / perHa
if missing > need * ADCropPlan.MIN_SHARE and missingHa > ADCropPlan.MIN_HA then
table.insert(list, {fruit = fruit, needLiters = need, supplyLiters = supply, missingLiters = missing, missingHa = missingHa})
table.insert(list, {fruit = fruit, product = product, key = key, needLiters = need, supplyLiters = supply,
missingLiters = missing, missingHa = missingHa})
end
end
end
table.sort(list, function(a, b)
if a.missingHa ~= b.missingHa then return a.missingHa > b.missingHa end
return a.fruit < b.fruit
return a.key < b.key
end)
return list
end
@@ -61,7 +75,8 @@ function ADCropPlan.choose(shortages, fields, usedFruits, yields, prices, dismis
local remaining = (harvesting or {})[shortage.fruit] and 0 or shortage.missingHa
local candidates = {}
for _, field in ipairs(fields or {}) do
if not taken[field.id] and isCandidate(field, used, (dismissed or {})[field.id], shortage.fruit) then
-- afvisninger gemmes pr. nøgle (afgrøden, eller varen for hø/ensilage), så hø og ensilage ikke blandes
if not taken[field.id] and isCandidate(field, used, (dismissed or {})[field.id], shortage.key or shortage.fruit) then
local planned = ADCropPlan.plannedFruit(field) or ""
local value = valueOf(planned)
table.insert(candidates, {field = field, value = value})
@@ -80,7 +95,8 @@ function ADCropPlan.choose(shortages, fields, usedFruits, yields, prices, dismis
local auto = field.chainFree == true and field.targetSource ~= "user"
if auto or suggested < ADCropPlan.MAX_SUGGEST_PER_FRUIT then
table.insert(actions, {fieldId = field.id, from = ADCropPlan.plannedFruit(field) or "", to = shortage.fruit,
hectares = field.areaHa or 0, reason = shortage.fruit, mode = auto and "auto" or "suggest"})
product = shortage.product, hectares = field.areaHa or 0, reason = shortage.key or shortage.fruit,
mode = auto and "auto" or "suggest"})
taken[field.id] = true
remaining = remaining - (field.areaHa or 0)
if not auto then suggested = suggested + 1 end