# 出典: mo-notebooks/02/bim-fractional.ipynb

# %%
# 依存関係をインストールし、ソルバーを選択します

SOLVER = "highs"

from amplpy import AMPL, ampl_notebook

ampl = ampl_notebook(
    modules=["highs"],  # modules to install
    license_uuid="default",  # license to use
)  # instantiate AMPL object and register magics

# %%
def BIM_with_revenues_minus_costs():
    m = AMPL()
    m.read("BIM_with_revenues_minus_costs.mod")

    return m


BIM_linear = BIM_with_revenues_minus_costs()
BIM_linear.solve(solver=SOLVER)
assert BIM_linear.solve_result == "solved", BIM_linear.solve_result

print(
    "x=({:.1f},{:.1f}) value={:.3f} revenue={:.2f} cost={:.2f}".format(
        BIM_linear.var["x1"].value(),
        BIM_linear.var["x2"].value(),
        BIM_linear.obj["profit"].value(),
        BIM_linear.var["revenue"].value(),
        BIM_linear.var["variable_cost"].value()
        + BIM_linear.param["fixed_cost"].value(),
    )
)

# %%
def BIM_with_revenues_over_costs():
    m = AMPL()
    m.read("BIM_with_revenues_over_costs.mod")

    return m


BIM_fractional = BIM_with_revenues_over_costs()
BIM_fractional.solve(solver=SOLVER)
assert BIM_fractional.solve_result == "solved", BIM_fractional.solve_result

t = BIM_fractional.var["t"].value()
y1 = BIM_fractional.var["y1"].value()
y2 = BIM_fractional.var["y2"].value()
profit = BIM_fractional.obj["profit"].value()
variable_cost = BIM_fractional.var["variable_cost"].value()
fixed_cost = BIM_fractional.param["fixed_cost"].value()
revenue = BIM_fractional.var["revenue"].value()

print(
    "x=({:.1f},{:.1f}) value={:.3f} revenue={:.2f} cost={:.2f}".format(
        y1 / t,
        y2 / t,
        profit / (variable_cost + fixed_cost * t),
        revenue / t,
        variable_cost / t + fixed_cost,
    )
)
