I'm trying to Solve a little probleme just to otpimize some units production in a game, where Alpha is a variety coefficient (it sets how the variable can differ from each other) :
import numpy as np
import scipy.optimize as opti
alpha = 0.05
C = np.array([-1,-1,-1,-1,-15,-3,-3,-4,0,0,0,0,0,0])
B = np.array([1600,0,0,0,0,0,0,0,0,0,0,0,0,0])
MatriceC = np.array([\
np.array([14-((1-alpha)*8),7-((1-alpha)*8),7-((1-alpha)*25),18-((1-
alpha)*12),30-((1-alpha)*30),40-((1-alpha)*40),18-((1-alpha)*1),76-((1-
alpha)*16),-1,0,0,0,0,0]),\
np.array([14-((1+alpha)*8),7-((1+alpha)*8),7-((1+alpha)*25),18-
((1+alpha)*12),30-((1+alpha)*30),40-((1+alpha)*40),18-((1+alpha)*1),76-
((1+alpha)*16),0,-1,0,0,0,0])*(-1),\
np.array([14-((1-alpha)*30),7-((1-alpha)*2),7-((1-alpha)*13),18-((1-
alpha)*7),30-((1-alpha)*30),40-((1-alpha)*40),18-((1-alpha)*24),76-((1-
alpha)*56),0,0,-1,0,0,0]),\
np.array([14-((1+alpha)*30),7-((1+alpha)*2),7-((1+alpha)*13),18-
((1+alpha)*7),30-((1+alpha)*30),40-((1+alpha)*40),18-((1+alpha)*24),76-
((1+alpha)*56),0,0,0,-1,0,0])*(-1),\
np.array([8-((1-alpha)*30),8-((1-alpha)*2),25-((1-alpha)*13),12-((1-
alpha)*7),30-((1-alpha)*30),40-((1-alpha)*40),1-((1-alpha)*24),16-((1-
alpha)*56),0,0,0,0,-1,0]),\
np.array([8-((1+alpha)*30),8-((1+alpha)*2),25-((1+alpha)*13),12-
((1+alpha)*7),30-((1+alpha)*30),40-((1+alpha)*40),1-((1+alpha)*24),16-
((1+alpha)*56),0,0,0,0,0,-1])*(-1)])
#print(help(opti.linprog))
print(np.shape(MatriceC))
print(np.shape(B))
opti.linprog(C,A_eq=MatriceC,b_eq=B) #This causes the error...
And I get as an output :
(6, 14)
(14,)
ValueError: Invalid input for linprog with method = 'simplex'. The number
of rows in A_eq must be equal to the number of values in b_eq
Considering the shape of the matrix I get. I don't understand what I'm doing wrong.
PS :
I have tried adding
MatriceC = MatriceC.T
Just before the linprog call and it stills outpout the same error. It did change the (6, 14) shape into (14, 6) (well it's logical)
Transponse your MatriceC
with MatriceC.T
before passing it to linprog
linprog
according to their doc:
Minimize: c^T * x
Subject to: A_ub * x <= b_ub A_eq * x == b_eq
In order to satisfy the above equation, the matrices' dimension should conform to each other. Read about Matrix Multiplication .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.