0

I'm fairly new to this but will try and be as clear as possible.

Essentially I have 5 different lists of lists. 4 are imported from txt files and the 5th is a merger of the 4. Each inner list contains a value at index position 3. My objective is to maximize the sum by picking appropriately.

I also have a couple constraints:

  • The sum of the values at index 6 position can't exceed 50000
  • I pick 2 items from set C, 3 from set W, 2 from set D, 1 from set G, and 1 from set U (the combined) and I can't pick the same item for each set. Ie. each pick in W has to be different.

My code is below. I'm having trouble in that the optimizer just spits out my initial list of picks. Looking at the data though, I know for sure there are better solutions. I read that the issue may be related to late binding but I'm not sure if that's right and if it is, not sure how to update to fix error either. Appreciate any help. Thanks!

Read: Scipy.optimize.minimize SLSQP with linear constraints fails

import numpy as np
from scipy.optimize import minimize

C = open('C.txt','r').read().splitlines()
W = open('W.txt','r').read().splitlines()
D = open('D.txt','r').read().splitlines()
G = open('G.txt','r').read().splitlines()

def splitdata(file):
    for index,line in enumerate(file):
        file[index] = line.split('\t')

    return(file)

def objective(x, sign=-1.0):

    x = list(map(int, x))
    pos = 3

    Cvalue = float(C[x[0]][pos]) + float(C[x[1]][pos])

    Wvalue = float(W[x[2]][pos]) + float(W[x[3]][pos]) + float(W[x[4]][pos])

    Dvalue = float(D[x[5]][pos]) + float(D[x[6]][pos])

    Gvalue = float(G[x[7]][pos])

    Uvalue = float(U[x[8]][pos])

    grand_value = sign*(Cvalue + Wvalue + Dvalue + Gvalue + Uvalue)

    #print(grand_value)
    return grand_value

def constraint_cost(x):

    x = list(map(int, x))
    pos = 6

    Ccost = int(C[x[0]][pos]) + int(C[x[1]][pos])

    Wcost = int(W[x[2]][pos]) + int(W[x[3]][pos]) + int(W[x[4]][pos])

    Dcost = int(D[x[5]][pos]) + int(D[x[6]][pos])

    Gcost = int(G[x[7]][pos])

    Ucost = int(U[x[8]][pos])

    grand_cost =  50000 - (Ccost + Wcost + Dcost + Gcost + Ucost)

    #print(grand_cost)
    return grand_cost

def constraint_C(x):
    if x[0] == x[1]:
        return 0
    else: 
        return 1 

def constraint_W(x):
    if x[2] == x[3] or x[2] == x[4] or x[3] == x[4]:
        return 0
    else: 
        return 1 

def constraint_D(x):
    if x[5] == init[6]:
        return 0
    else: 
        return 1

con1 = {'type':'ineq','fun':constraint_cost}
con2 = {'type':'ineq','fun':constraint_C}
con3 = {'type':'ineq','fun':constraint_W}
con4 = {'type':'ineq','fun':constraint_D}

con = [con1, con2, con3, con4]

c0 = [0,1]
w0 = [0,1,2]
d0 = [0,1]
g0 = [0]
u0 = [0]
init = c0+w0+d0+g0+u0

C = splitdata(C)
W = splitdata(W)
D = splitdata(D)
G = splitdata(G)
U = C + W + D + G

sol = minimize(objective, init, method='SLSQP',constraints=con)

print(sol)
Nick ODell
  • 15,465
  • 3
  • 32
  • 66
tyu0912
  • 11
  • 4
  • 1
    SLSQP expects smooth (continuous differentiable) functions. Your functions violate that. – Erwin Kalvelagen Mar 30 '18 at 01:08
  • Hello. Thanks for the reply. I did not know that actually but I went through all the methods provided and none of them seem to give anything reasonable. To clarify my objective a bit, my result [x1, x2, x3,..,x9] should be positive integers corresponding to an list item in my bigger C,W,G,D,or U list. – tyu0912 Mar 30 '18 at 01:26
  • 2
    That does not surprise me. Using a continuous solver for a discrete problem is likely to fail. – Erwin Kalvelagen Mar 30 '18 at 03:32
  • Did some more searching since. Thanks for pointing me on the right path! – tyu0912 Apr 01 '18 at 23:49

0 Answers0