# いかかかい
from pyscipopt import Model
model = Model("いかかかい")
i = [model.addVar(vtype="BINARY", name=f"i{i}") for i in range(0, 10)]
k = [model.addVar(vtype="BINARY", name=f"k{i}") for i in range(0, 10)]
model.addCons(sum(i) == 1)
model.addCons(sum(k) == 1)
iscore = model.addVar(vtype="INTEGER", name="iscore")
model.addCons(iscore == sum(n * i[n] for n in range(1,10)))
kscore = model.addVar(vtype="INTEGER", name="kscore")
model.addCons(kscore == sum(n * k[n] for n in range(1,10)))
model.addCons(i[0] == 0)
model.addCons(k[0] == 0)
# メインの数式: 10i + 2ka = 10ka + i → 9i = 8ka
model.addCons(9 * iscore == 8 * kscore)
#model.addCons(i[8] + k[9] <= 1)
# 目的:
model.setObjective(0)
model.optimize()
# 結果
if model.getStatus() == "optimal":
print("✅ 解が見つかりました:")
for var in model.getVars():
if model.getVal(var) != 0 :
print(f"{var.name} = {model.getVal(var)}")
else:
print("❌ 解が見つかりませんでした")
model.writeProblem("ikakakai.lp")
つづく