# 出典: mo-notebooks/03/maintenance-planning.ipynb

# %%
# install dependencies and select solver

SOLVER = "cbc"

from amplpy import AMPL, ampl_notebook

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

# %%
import matplotlib.pyplot as plt
import numpy as np

# %%
# problem parameters
T = 90  # planning period from 1..T
M = 3  # length of maintenance period
P = 4  # number of maintenance periods

# daily profits
np.random.seed(0)
c = {k: np.random.uniform() for k in range(1, T + 1)}

# %%
class MP(object):
    def __init__(self, c, M, P):
        self.T = len(c)
        self.M = M
        self.P = P
        self.c = c

        self.modfile = "maintenance_planning.mod"

        self.ampl = AMPL()
        self.ampl.option["solver"] = SOLVER
        self.solved = False

    def load_data(self):
        self.ampl.param["T"] = self.T
        self.ampl.param["P"] = self.P
        self.ampl.param["M"] = self.M
        self.ampl.param["c"] = self.c

    def set_solver(self, solver):
        self.ampl.option["solver"] = solver

    def set_modfile(self, modfile):
        self.modfile = modfile

    def solve(self):
        self.ampl.read(self.modfile)
        self.load_data()
        self.ampl.solve()
        assert self.ampl.solve_result == "solved", self.ampl.solve_result
        self.solved = True

    def plot_schedule(self):
        if not self.solved:
            self.solve()

        T = self.T
        M = self.M
        P = self.P
        PH = list(range(1, T + 1))
        Y = list(range(1, T - M + 2))
        S = list(range(M))

        fig, ax = plt.subplots(3, 1, figsize=(9, 4))

        ax[0].bar(PH, [self.c[t] for t in PH])
        ax[0].set_title("daily profit $c_t$")

        x = self.ampl.var["x"].to_dict()

        ax[1].bar(PH, [x[t] for t in PH], label="normal operation")
        ax[1].set_title("unit operating schedule $x_t$")

        y = self.ampl.var["y"].to_dict()

        ax[2].bar(Y, [y[t] for t in Y])
        ax[2].set_title(str(P) + " maintenance starts $y_t$")
        for a in ax:
            a.set_xlim(0.1, T + 0.9)

        plt.tight_layout()


model = MP(c, M, P)
model.plot_schedule()

# %%
class MPRamp(MP):
    def __init__(self, c, M, P, upos_max, uneg_max):
        super(MPRamp, self).__init__(c, M, P)
        super(MPRamp, self).set_modfile("maintenance_planning_ramp.mod")
        self.upos_max = upos_max
        self.uneg_max = uneg_max

    def load_data(self):
        super(MPRamp, self).load_data()
        self.ampl.param["upos_max"] = self.upos_max
        self.ampl.param["uneg_max"] = self.uneg_max


upos_max = 0.3334
uneg_max = 0.5000

model = MPRamp(c, M, P, upos_max, uneg_max)
model.plot_schedule()

# %%
class MPRampOperational(MPRamp):
    def __init__(self, c, M, P, upos_max, uneg_max, N):
        super(MPRampOperational, self).__init__(c, M, P, upos_max, uneg_max)
        super(MPRampOperational, self).set_modfile(
            "maintenance_planning_ramp_operational.mod"
        )
        self.N = N

    def load_data(self):
        super(MPRampOperational, self).load_data()
        self.ampl.param["N"] = self.N


N = 10  # minimum number of operational days between maintenance periods
model = MPRampOperational(c, M, P, upos_max, uneg_max, N)
model.plot_schedule()
