"""安全在庫配置問題（SOS2 を用いた凸結合定式化）.

注意: SOS2 に対応したソルバー（Gurobi, CPLEX, COPT, Xpress）が必要。
      HiGHS や SCIP では "MIP conversion of general SOS2: not implemented" になる。
"""
import math
from amplpy import AMPL

n, K = 4, 10
h = {i: 1.0*i for i in range(1, n+1)}
T = {i: 2 for i in range(1, n+1)}
b = {(i, k): math.sqrt(k) for i in range(1, n+1) for k in range(K)}

ampl = AMPL()
ampl.read("ssa.mod")
ampl.param["n"] = n
ampl.param["K"] = K
ampl.param["h"] = h
ampl.param["T"] = T
ampl.param["b"] = b
ampl.option["solver"] = "highs"
try:
    ampl.solve(verbose=False)
    print("solve_result:", ampl.solve_result)
    if ampl.solve_result == "solved":
        print("Opt. Value=", ampl.get_objective("Cost").value())
except Exception as exc:
    print("SOS2 非対応のソルバーです:", str(exc).split("\n")[0])
