title: scipy.optimize minimize not changing value. think it's due to late binding but unsure how to change...
body: <p>I'm fairly new to this but will try and be as clear as possible.</p>
  
  <p>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. </p>
  
  <p>I also have a couple constraints:</p>
  
  <ul>
  <li>The sum of the values at index 6 position can't exceed 50000</li>
  <li>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. </li>
  </ul>
  
  <p>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!</p>
  
  <p>Read: <a href="https://stackoverflow.com/questions/37791680/scipy-optimize-minimize-slsqp-with-linear-constraints-fails">Scipy.optimize.minimize SLSQP with linear constraints fails</a></p>
  
  <pre><code>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)
  </code></pre>