Commit dd9f7717 authored by brugeron's avatar brugeron

Create a linearization module

Implementation of the module in the utils.py
Test on the storage design file
parent 4bae24b3
......@@ -30,6 +30,7 @@ from pulp import LpBinary
from .optimisation.elements import Quantity
from .optimisation.elements import Constraint, \
DynamicConstraint
import numpy as np
__docformat__ = "restructuredtext en"
......@@ -192,3 +193,177 @@ def def_abs_value(quantity, q_min, q_max):
setattr(parent, 'set_{}_abs'.format(q_name), set_abs)
return abs_value
def linearize_square(quantity, nd):
time = quantity.parent.time
q_min = quantity.lb
q_max = quantity.ub
square_max = q_max * q_max
q_name = getattr(quantity, 'name')
q_len = getattr(quantity, 'vlen')
parent = getattr(quantity, 'parent')
square_name = 'square_' + q_name
parent.square_quantity = Quantity(name=square_name, value=None,
lb=0., ub=1., vlen=time.LEN, parent=parent)
list_w = []
list_u = []
for i in range(nd + 1):
w_name = q_name + '_w_' + str(i)
w_temp = Quantity(name=w_name,
lb=0, ub=1, vlen=time.LEN, parent=parent)
list_w.append(parent.name + '_' + w_name)
setattr(parent, w_name, w_temp)
u_name = q_name + '_u_' + str(i)
u_temp = Quantity(name=u_name, vtype=LpBinary,
vlen=time.LEN, parent=parent)
list_u.append(parent.name + '_' + u_name)
setattr(parent, u_name, u_temp)
cst_name = 'def_cst_uw_' + q_name + '_' + str(i)
if i == 0:
setattr(parent, cst_name, DynamicConstraint(name=cst_name,
exp_t='{0}_{1}_u_{2}[t] >= {0}_{1}_w_{2}[t]'.format(parent.name,
q_name,
str(i)),
t_range='for t in time.I',
parent=parent))
elif i == nd:
setattr(parent, cst_name, DynamicConstraint(name=cst_name,
exp_t='{0}_{1}_u_{3}[t] >= {0}_{1}_w_{2}[t]'.format(parent.name,
q_name,
str(i),
str(i - 1)),
t_range='for t in time.I',
parent=parent))
else:
setattr(parent, cst_name, DynamicConstraint(name=cst_name,
exp_t='{0}_{1}_u_{2}[t] + {0}_{1}_u_{3}[t] >= {0}_{1}_w_{2}[t]'.format(
parent.name, q_name, str(i), str(i - 1)),
t_range='for t in time.I',
parent=parent))
# parent.list_cst.append(cst_temp)
Q = list(np.linspace(q_min, q_max, nd))
parent.Q = Quantity(name=q_name + '_Q', value=Q, opt=False, vlen=len(Q), parent=parent)
i_nd_array = np.arange(start=0, stop=nd, step=1)
i_nd = list(i_nd_array)
parent.i_nd = Quantity(name=q_name + '_i_nd', value=i_nd, opt=False, vlen=len(i_nd), parent=parent)
'eval({0}[0])[t] + eval({0}[1])[t] + eval({0}[2])[t] + eval({0}[3])[t] + eval({0}[4])[t] + eval({0}[5])[t] == 1'
exp1 = 'lpSum(eval({0}[i])[t] for i in range({1})) == 1'.format(list_u, nd-1)
exp2 = 'lpSum(eval({0})[i][t] for i in range({1})) == 1'.format(list_w[nd], nd+1)
exp3 = 'lpSum(eval({2})[i][t]*{0}_Q[i] for i in range({3})) == {0}_{1}_square_quantity[t]'.format(parent.name, q_name,
list_w, nd)
exp4 = 'lpSum(eval({2})[i][t]*{5}[i] for i in range({3}))== {0}_{4}[t]'.format(parent.name, q_name, list_w, nd,
square_name, Q)
''
def_square_1 = DynamicConstraint(name='def_square_1_' + q_name,
exp_t=exp1,
t_range='for t in time.I',
parent=parent)
def_square_2 = DynamicConstraint(name='def_square_2_' + q_name,
exp_t=exp2,
t_range='for t in time.I',
parent=parent)
def_square_3 = DynamicConstraint(name='def_square_3_' + q_name,
exp_t=exp3,
t_range='for t in time.I',
parent=parent)
def_square_4 = DynamicConstraint(name='def_square_4_' + q_name,
exp_t=exp4,
t_range='for t in time.I',
parent=parent)
setattr(parent, 'def_square_1_' + q_name, def_square_1)
# setattr(parent, 'def_square_2_' + q_name, def_square_2)
# setattr(parent, 'def_square_3_' + q_name, def_square_3)
# setattr(parent, 'def_square_4_' + q_name, def_square_4)
return parent.square_quantity
def linearize_product(x, y, nd):
time = x.parent.time
x_parent = x.parent
y_parent = y.parent
x_name = x.name
y_name = y.name
x_min = x.lb
x_max = x.ub
y_min = y.lb
y_max = y.ub
x_parent.x_normalize = Quantity(name='x_normalize',
lb=0., ub=1., vlen=time.LEN, parent=x_parent)
def_x_normalize = DynamicConstraint(name='def_x_normalize',
exp_t='{0}_x_normalize[t] == ({0}_{1}[t] - {2})/({3}-{2})'.format(x_parent.name,
x_name, x_min,
x_max))
setattr(x_parent, 'def_x_normalize', def_x_normalize)
y_parent.y_normalize = Quantity(name='y_normalize',
lb=0., ub=1., vlen=time.LEN, parent=y_parent)
def_y_normalize = DynamicConstraint(name='def_y_normalize',
exp_t='{0}_y_normalize[t] == ({0}_{1}[t] - {2})/({3}-{2})'.format(
y_parent.name, y_name, y_min, y_max))
setattr(y_parent, 'def_y_normalize', def_y_normalize)
x_parent.z_normalize = Quantity(name='z_normalize',
lb=0, ub=1, vlen=time.LEN, parent=x_parent)
x_parent.z_product = Quantity(name='z_product',
lb=-10e+5, ub=10e+5, vlen=time.LEN, parent=x_parent)
x_parent.a = Quantity(name='a',
lb=0, ub=1, vlen=time.LEN, parent=x_parent)
def_a = DynamicConstraint(name='def_a',
exp_t='{0}_a[t] == 0.5 * ({0}_x_normalize[t] + {1}_y_normalize[t])'.format(x_parent.name,
y_parent.name)
)
setattr(x_parent, 'def_a', def_a)
y_parent.b = Quantity(name='b',
lb=-0.5, ub=0.5, vlen=time.LEN, parent=y_parent)
def_b = DynamicConstraint(name='def_b',
exp_t='{1}_b[t] == 0.5 * ({0}_x_normalize[t] - {1}_y_normalize[t])'.format(x_parent.name,
y_parent.name)
)
setattr(y_parent, 'def_b', def_b)
x_parent.square_a = linearize_square(x_parent.a, nd)
y_parent.square_b = linearize_square(y_parent.b, nd)
exp_norm = '{0}_z_normalize[t] == {0}_square_a[t] - {1}_square_b[t]'.format(x_parent.name,
y_parent.name)
def_product_normalize = DynamicConstraint(name='def_product_normalize',
exp_t=exp_norm,
t_range='for t in time.I',
parent=x_parent)
setattr(x_parent, 'def_product_normalize', def_product_normalize)
exp_prod = '{0}_z_product =={0}_z_normalize[t]*({6}-{4}) * ({7}-{5}) -{4}*{5}+ {0}_{2}[t]*{5} + {1}_{3}[t]*{4}'.format(
x_parent.name, y.parent.name, x_name, y_name, x_min, y_min, x_max, y_max)
def_product = DynamicConstraint(name='def_product',
exp_t=exp_prod,
t_range='for t in time.I',
parent=x_parent)
setattr(x_parent, 'def_product', def_product)
return x_parent.z_product
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment