searev_sys.dyn = dyn_searev_sto
searev_sys.control_box = admissible_controls
searev_sys.cost = cost_model
searev_sys.perturb_laws = [innov_law]

#searev_sys.print_summary()

### Create the DP solver:
dpsolv = DPSolver(searev_sys)
# discretize the state space
N_E = 31
N_S = 61
N_A = 61
S_min, S_max = -4 * .254, 4 * 0.254
A_min, A_max = -4 * .227, 4 * .227
x_grid = dpsolv.discretize_state(0, E_rated, N_E, S_min, S_max, N_S, A_min,
                                 A_max, N_A)
E_grid, S_grid, A_grid = x_grid
# discretize the perturbation
N_w = 9
dpsolv.discretize_perturb(-3 * innov_std, 3 * innov_std, N_w)
# control discretization step:
dpsolv.control_steps = (.001, )

dpsolv.print_summary()


# An heuristic control law:
def P_sto_law_lin(E_sto, Speed, Accel):
    '''linear storage control law'''
    P_prod = searev_power(Speed)
    P_grid = P_rated * E_sto / E_rated
### Create the system description:
sto_sys = SysDescription((1, 1, 0),
                         name='Deterministic Storage for PV',
                         stationnary=False)
sto_sys.dyn = dyn_sto
sto_sys.control_box = admissible_controls
sto_sys.cost = cost_model

sto_sys.print_summary()

### Create the DP solver:
dpsolv = DPSolver(sto_sys)
# discretize the state space
N_E = 50

E_grid = dpsolv.discretize_state(0, E_rated, N_E)[0]
dpsolv.control_steps = (.001, )

dpsolv.print_summary()

J_fin = np.zeros(N_E)

J, pol = dpsolv.bellman_recursion(T_horiz, J_fin)

pol_sto = pol[..., 0]

### Plot the policy
plt.figure('policy')
plt.subplot(111,
            title='$P_{grid}(t,E)$ policy',
            xlabel='time',
op_cost(np.array([-2,-1,0,1,2]),1,0)

invsys.cost = op_cost

#invsys.print_summary()

print('Invertory Control with (h={:.1f}, p={:.1f}, c={:.1f})'.format(h,p,c))

### DP Solver ##################################################################
from stodynprog import DPSolver
dpsolv = DPSolver(invsys)

# discretize the state space
xmin, xmax = (-3,6)
N_x = xmax-xmin+1 # number of states
dpsolv.discretize_state(xmin, xmax, N_x)

# discretize the perturbation
N_w = len(demand_values)
dpsolv.discretize_perturb(demand_values[0], demand_values[-1], N_w)
# control discretization step:
dpsolv.control_steps=(1,) #

#dpsolv.print_summary()

### Value iteration
J_0 = np.zeros(N_x)
# first iteration
J,u = dpsolv.value_iteration(J_0)
print(u[...,0])
# A few more iterations
op_cost(np.array([-2, -1, 0, 1, 2]), 1, 0)

invsys.cost = op_cost

#invsys.print_summary()

print('Invertory Control with (h={:.1f}, p={:.1f}, c={:.1f})'.format(h, p, c))

### DP Solver ##################################################################
from stodynprog import DPSolver
dpsolv = DPSolver(invsys)

# discretize the state space
xmin, xmax = (-3, 6)
N_x = xmax - xmin + 1  # number of states
dpsolv.discretize_state(xmin, xmax, N_x)

# discretize the perturbation
N_w = len(demand_values)
dpsolv.discretize_perturb(demand_values[0], demand_values[-1], N_w)
# control discretization step:
dpsolv.control_steps = (1, )  #

#dpsolv.print_summary()

### Value iteration
J_0 = np.zeros(N_x)
# first iteration
J, u = dpsolv.value_iteration(J_0)
print(u[..., 0])
# A few more iterations
Example #5
0
def optimize(crtReachMovementTask):
    minimum_movement_length = crtReachMovementTask.minimum_movement_length
    maximum_movement_length = crtReachMovementTask.maximum_movement_length
    c = crtReachMovementTask.c
    target_position = crtReachMovementTask.target_position
    number_of_iterations = crtReachMovementTask.number_of_iterations

    #functions defined here inside the optimizer
    def dyn_inv(x, u, w):
        'dynamical equation of the inventory stock `x`. Returns x(k+1).'
        return (x + u + w, )

    def op_cost(x, u, w):
        'energy cost'

        cost = abs(target_position - x) + c * u

        #cost = abs(20-x)+c*u
        # print(cost)
        return cost

    def admissible_movements(x):
        'interval of allowed orders movements(x_k)'
        U1 = (minimum_movement_length, maximum_movement_length)
        return (U1, )  # tuple, to support several controls

    # Attach it to the system description.

    # Attach the dynamical equation to the system description:
    movement_sys.dyn = dyn_inv

    mov_values = [-6, -4, -2, 0, 2, 4, 6]
    mov_proba = [0.1, 0.1, 0.15, 0.3, 0.15, 0.1, 0.1]
    demand_law = stats.rv_discrete(values=(mov_values, mov_proba))
    demand_law = demand_law.freeze()

    demand_law.pmf([0, 3])  # Probality Mass Function
    demand_law.rvs(10)  # Random Variables generation

    movement_sys.perturb_laws = [demand_law
                                 ]  # a list, to support several perturbations
    movement_sys.control_box = admissible_movements
    movement_sys.cost = op_cost

    print('Movement Control with (c={:.1f})'.format(c))

    dpsolv = DPSolver(movement_sys)

    # discretize the state space
    xmin, xmax = (0, 100)
    N_x = xmax - xmin + 1  # number of states
    dpsolv.discretize_state(xmin, xmax, N_x)

    # discretize the perturbation
    N_w = len(mov_values)
    dpsolv.discretize_perturb(mov_values[0], mov_values[-1], N_w)
    # control discretization step:
    dpsolv.control_steps = (1, )  #

    #dpsolv.print_summary()

    ### Value iteration
    J_0 = np.zeros(N_x)
    # first iteration
    J, u = dpsolv.value_iteration(J_0)
    print(u[..., 0])
    for i in xrange(number_of_iterations):
        J, u = dpsolv.value_iteration(J)

    # print(u[...,0])

    return [movement_sys, dpsolv, u]
    return penal

### Create the system description:
sto_sys = SysDescription((1,1,0), name='Deterministic Storage', stationnary=False)
sto_sys.dyn = dyn_sto
sto_sys.control_box = admissible_controls
sto_sys.cost = cost_model

sto_sys.print_summary()

### Create the DP solver:
dpsolv = DPSolver(sto_sys)
# discretize the state space
N_E = 100

E_grid = dpsolv.discretize_state(0, p['E_rated'], N_E)[0]
dpsolv.control_steps=(.001,)

dpsolv.print_summary()

if __name__ == '__main__':
    ### Solve the problem:
    # Zero final cost
    J_fin = np.zeros(N_E)
    # Solve the problem:
    J, pol = dpsolv.bellman_recursion(T_horiz, J_fin)

    # RMS cost from the cost function:
    J_l2_empty = np.sqrt(J[0,0]/T_horiz)
    J_l2_mid = np.sqrt(J[0,N_E//2]/T_horiz)
    J_l2_full = np.sqrt(J[0,-1]/T_horiz)
    return penal

### Create the system description:
sto_sys = SysDescription((1,1,0), name='Deterministic Storage for PV', stationnary=False)
sto_sys.dyn = dyn_sto
sto_sys.control_box = admissible_controls
sto_sys.cost = cost_model

sto_sys.print_summary()

### Create the DP solver:
dpsolv = DPSolver(sto_sys)
# discretize the state space
N_E = 50

E_grid = dpsolv.discretize_state(0, E_rated, N_E)[0]
dpsolv.control_steps=(.001,)

dpsolv.print_summary()

J_fin = np.zeros(N_E)

J, pol = dpsolv.bellman_recursion(T_horiz, J_fin)

pol_sto = pol[..., 0]

### Plot the policy
plt.figure('policy')
plt.subplot(111, title='$P_{grid}(t,E)$ policy', xlabel='time', ylabel='$E_{sto}$')
plt.imshow(P_prod_data - pol_sto.T, extent=(0, (T_horiz-1)/24, 0, 2))
Example #8
0
### Create the system description:
sto_sys = SysDescription((1, 1, 0),
                         name='Deterministic Storage',
                         stationnary=False)
sto_sys.dyn = dyn_sto
sto_sys.control_box = admissible_controls
sto_sys.cost = cost_model

sto_sys.print_summary()

### Create the DP solver:
dpsolv = DPSolver(sto_sys)
# discretize the state space
N_E = 100

E_grid = dpsolv.discretize_state(0, p['E_rated'], N_E)[0]
dpsolv.control_steps = (.001, )

dpsolv.print_summary()

if __name__ == '__main__':
    ### Solve the problem:
    # Zero final cost
    J_fin = np.zeros(N_E)
    # Solve the problem:
    J, pol = dpsolv.bellman_recursion(T_horiz, J_fin)

    # RMS cost from the cost function:
    J_l2_empty = np.sqrt(J[0, 0] / T_horiz)
    J_l2_mid = np.sqrt(J[0, N_E // 2] / T_horiz)
    J_l2_full = np.sqrt(J[0, -1] / T_horiz)
searev_sys.dyn = dyn_searev_sto
searev_sys.control_box = admissible_controls
searev_sys.cost = cost_model
searev_sys.perturb_laws = [innov_law]

# searev_sys.print_summary()

### Create the DP solver:
dpsolv = DPSolver(searev_sys)
# discretize the state space
N_E = 31
N_S = 61
N_A = 61
S_min, S_max = -4 * 0.254, 4 * 0.254
A_min, A_max = -4 * 0.227, 4 * 0.227
x_grid = dpsolv.discretize_state(0, E_rated, N_E, S_min, S_max, N_S, A_min, A_max, N_A)
E_grid, S_grid, A_grid = x_grid
# discretize the perturbation
N_w = 9
dpsolv.discretize_perturb(-3 * innov_std, 3 * innov_std, N_w)
# control discretization step:
dpsolv.control_steps = (0.001,)

dpsolv.print_summary()


# An heuristic control law:
def P_sto_law_lin(E_sto, Speed, Accel):
    """linear storage control law"""
    P_prod = searev_power(Speed)
    P_grid = P_rated * E_sto / E_rated