Python Forum
Constraint function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Constraint function (/thread-39056.html)



Constraint function - tman - Dec-27-2022

I need to code this function out; it’s subject to the following constraints.


e[k+1] = t*e[k+1] + eta_c*p_c[k] -p_d[k]/eta_c. (1)

p_c[k]*p_d[k] = 0
e[0] = 0
0<= p_c[k] <= P
0<= p_d[k] <= P
0<= e[k+1] <= E

Equation one may be written in compact form as:
e(p_c, p_d) = t*1^H*x0 + eta_c*A*p_c – A*p_d *eta_d

t=0.02, eta_c =0.8, eta_d =0.9, P =10, E = 20, H=24, k = 1:24, x0 = e[1]
A = lower diagonal matrix of size H by H.
p_c and p_d are any sequence of number of length H. They can be column vectors


RE: Constraint function - Larz60+ - Dec-27-2022

Please show us what you're most recent (or most correct) attempt so far.
This article may help: https://pythonforundergradengineers.com/sympy-expressions-and-equations.html


RE: Constraint function - tman - Dec-27-2022

Thanks a lot. Here are my attempts. The constraints are not obeyed in the results
N = 1; 
T =24;
P = 10
E = 20 
dt = 1 # 
eta_c = 0.8 
eta_d = 0.9 
e0 = 0.95* E 
t = 0.02

p_c = np.linspace(0, P, T, endpoint=True)  
p_d = np.linspace(0, P, T, endpoint=False)   
A =   np.tri(T)     # lower trinagular matrix
II = np.ones(T)
e = cp.Variable((T,N))

e = t*II*x0 + eta_c*A@p_c - A@p_d/eta_d 
e[0] = e0
# use a for loop to define the unit constriant over each time period
const = []
for k in range(T-1):  # go through each period
#         const.append(x[0] == e0)
        #e[k+1] = t*x[k] + eta_c*p_c[k] - p_d[k]/eta_d 

        p_c[k]*p_d[k] == 0               
        p_c[k] >= 0           
        p_c[k] <= P           
        p_d[k] >= 0            
        p_d[k] <= P            
        e[k+1] >= 0
        e[k+1] <= E



RE: Constraint function - tman - Dec-27-2022

N = 1;
T =24;
P = 10
E = 20
dt = 1 #
eta_c = 0.8
eta_d = 0.9
e0 = 0.95* E
t = 0.02

p_c = np.linspace(0, P, T, endpoint=True)
p_d = np.linspace(0, P, T, endpoint=False)
A = np.tri(T) # lower trinagular matrix
II = np.ones(T)
e = cp.Variable((T,N))

e = t*II*x0 + eta_c*A@p_c - A@p_d/eta_d
e[0] = e0
# use a for loop to define the unit constriant over each time period
const = []
for k in range(T-1): # go through each period
# const.append(x[0] == e0)
#e[k+1] = t*x[k] + eta_c*p_c[k] - p_d[k]/eta_d

p_c[k]*p_d[k] == 0
p_c[k] >= 0
p_c[k] <= P
p_d[k] >= 0
p_d[k] <= P
e[k+1] >= 0
e[k+1] <= E
Error:
No error, but the code isn't doing its job, the value of e does not satisfy the conditions e = 0.9058 -1.4707 -2.4892 3.1803 -1.4966 -3.8782 -0.2953 2.1146 3.8051 0.4944 7.4861 -9.7195 -15.8602 -8.3353 9.6608 -31.9699 1.9543 -8.6885 -39.9836 10.8077 11.4135 10.6851 -8.5794 -1.9008



RE: Constraint function - ndc85430 - Dec-27-2022

That code is poorly formatted (no indentation) and I don't really know what you're trying to do, but you have a load of expressions on lines 25-31 that you do nothing with. They're evaluated and then just thrown away. The for loop beginning on line 21 then doesn't actually do anything.