Gekko Returning Incorrect Successful Solution
Solution 1:
There is no problem to use np.matmul
or any other function that allows objects instead of only numeric values. Objects are needed because b
is an array of Gekko type values that are needed to compute the derivatives with automatic differentiation. You can also use the new @
operator that simplifies the expressions. Your original problem statement was incomplete with many missing definitions. I added a few sample values so that the script can run without definition errors. Here are guidelines to help reproduce the error.
N = 2
n = 3
qb_index_range = [0,2]
rb_index_range = [0,2]
wr_index_range = [0,2]
info_df = pd.DataFrame({'cost':np.ones(n)})
budget = 100
sigma_post = np.random.rand(n,n)
Here is an example of using np.matmul()
that can also be the dot product np.dot()
.
sigma_1 = np.matmul(np.matmul(b,sigma_post), b.T)
This can also be written with the matrix multiplication operator.
sigma_1 = b@sigma_post@b.T
Here is the complete script.
from gekko import GEKKO
import numpy as np
import pandas as pd
m = GEKKO(remote=False)
m.options.max_iter=1000
N = 2
n = 3
b = m.Array(m.Var,(N,n), lb=0, ub=1, integer=True)
qb_index_range = [0,2]
rb_index_range = [0,2]
wr_index_range = [0,2]
info_df = pd.DataFrame({'cost':np.ones(n)})
budget = 100
sigma_post = np.eye(n)
for i inrange(N):
for j inrange(n):
if j in [qb_index_range[0], rb_index_range[0], wr_index_range[0]]:
b[i][j].value = 1else:
b[i][j].value = 0# CONSTRAINT: Each Lineup must be less than budget
z = [None]*N
for i inrange(N):
z[i] = m.Intermediate(sum(b[i, :]*list(info_df['cost'])))
m.Equations([z[i] <= budget for i inrange(N)])
# CONSTRAINT: Each Lineup has one QB
z_1 = [None]*N
for i inrange(N):
z_1[i] = m.Intermediate(sum(b[i, qb_index_range[0]: qb_index_range[1]+1]))
m.Equations([z_1[i] == 1for i inrange(N)])
# CONSTRAINT: Each Lineup has one RB
z_2 = np.array([None]*N)
for i inrange(N):
z_2[i] = m.Intermediate(sum(b[i, rb_index_range[0]: rb_index_range[1]+1]))
m.Equations([z_2[i] == 1for i inrange(N)])
# CONSTRAINT: Each Lineup has one WR
z_3 = np.array([None]*N)
for i inrange(N):
z_3[i] = m.Intermediate(sum(b[i, wr_index_range[0]: wr_index_range[1]+1]))
m.Equations([z_3[i] == 1for i inrange(N)])
#OBJECTIVE: maximize with two lineups#sigma_1 = np.matmul(np.matmul(b,sigma_post), b.T)
sigma_1 = b@sigma_post@b.T
m.Maximize(sigma_1[0][0] + sigma_1[1][1]- 2*sigma_1[1][0])
m.options.SOLVER = 1
m.solve(debug=0,disp=False)
print(b)
This produces a successful solution. The correct solution cannot be verified because the original problem statement is not complete.
[[[1.0] [0.0] [0.0]][[1.0] [0.0] [0.0]]]
Post a Comment for "Gekko Returning Incorrect Successful Solution"