def simulation():

    # MPC Evaluation Period
    eval_period = 5

    # Model Initiation
    model = MimoCstr(nsim=50, k0=8.2e10)

    # MPC Initiation
    mpc_init = ModelPredictiveControl(10, model.Nx, model.Nu, 0.1, 0.1, 0.1,
                                      model.xs, model.us)

    # MPC Construction
    mpc_control = mpc_init.get_mpc_controller(model.cstr_ode,
                                              eval_period,
                                              model.x0,
                                              random=False,
                                              verbosity=0)

    # Output Disturbance
    output_disturb = np.zeros(model.Nx)
    x_corrected = np.zeros([model.Nsim + 1, model.Nx])
    """
    Simulation portion
    """

    for t in range(model.Nsim):
        """
        Disturbance
        """

        # if t == 10:
        #     model.disturbance()
        """
        MPC evaluation
        """

        if t % eval_period == 0 and t != 0:
            # Solve the MPC optimization problem
            mpc_init.solve_mpc(model.x, model.u, model.xsp, mpc_control, t)
            if t != 0:
                output_disturb = model.xs - model.x[t, :]
        elif t < 5:
            model.u[t, :] = [300, 0.1]
        else:
            model.u[t, :] = model.u[t - 1, :]

        # Calculate the next stages
        model.x[t + 1, :] = model.next_state(model.x[t, :], model.u[t, :])
        x_corrected[t + 1, :] = model.x[t + 1, :] + output_disturb

    print(model.cost_function(x_corrected))

    return model, mpc_init, x_corrected
def simulation():

    # Plant Model
    model_plant = MimoCstr(nsim=50)

    # Build Controller Model
    model_control = MimoCstr(nsim=model_plant.Nsim,
                             nx=model_plant.Nx * 2,
                             xs=np.array([0.878, 324.5, 0.659, 0, 0, 0]),
                             x0=np.array([1, 310, 0.659, 0, 0, 0]),
                             control=True)

    # MPC Object Initiation
    control = ModelPredictiveControl(model_control.Nsim,
                                     10,
                                     model_control.Nx,
                                     model_control.Nu,
                                     0.1,
                                     0.1,
                                     0.1,
                                     model_control.xs,
                                     model_control.us,
                                     dist=True)

    # MPC Construction
    mpc_control = control.get_mpc_controller(model_control.cstr_ode,
                                             control.eval_time,
                                             model_control.x0,
                                             random_guess=False)
    """
    Simulation portion
    """

    for t in range(model_plant.Nsim):

        # Solve the MPC optimization problem, obtain current input and predicted state
        model_control.u[t, :], model_control.x[t + 1, :] = control.solve_mpc(
            model_plant.x, model_plant.xsp, mpc_control, t, control.p)

        # Calculate the next states for the plant
        model_plant.x[t + 1, :] = model_plant.next_state(
            model_plant.x[t, :], model_control.u[t, :])

        # Update the P parameters for offset-free control
        control.p = model_plant.x[t + 1, :] - model_control.x[t + 1, 0:3]

    print(model_plant.cost_function())

    return model_plant, model_control, control