package com.aps.macroplanner.variable;

import com.google.ortools.linearsolver.MPSolver;
import com.aps.macroplanner.data.*;
import com.aps.macroplanner.model.MacroPlannerModel;

/**
 * 库存变量构建器 — 创建 InvQty 变量。
 *
 * <p>InvQty[productId_spId_periodIndex] — 期末库存变量</p>
 * <ul>
 *   <li>索引: Product × StockingPoint × Period</li>
 *   <li>含义: 某个产品在某个库存点、某个周期结束时的库存量</li>
 *   <li>范围: [0, +∞)</li>
 *   <li>注意: 第 0 周期的期初库存由初始库存常量给定，
 *       之后每个周期的期初库存 = 上一周期的期末库存 (由 Balance 约束保证)</li>
 * </ul>
 */
public class InventoryVariableBuilder {

    public static void create(MacroPlannerModel model, TestDataBuilder data) {
        double inf = MPSolver.infinity();
        for (Product prod : data.getProducts()) {
            for (StockingPoint sp : data.getStockingPointsForProduct(prod.getId())) {

                for (Period p : data.getPeriods()) {
                    String key = prod.getId() + "_" + sp.getId() + "_" + p.getIndex();
                    model.getInvQtyVars().put(key,
                            model.getSolver().makeNumVar(0.0, inf, "InvQty_" + key));
                }
            }
        }
    }
}