0

I'm trying to move an optimization job that I usually do in Excel to a Python script. Basically I have an array of numbers (deals) that in some combination add to the variable (target). The way I find this combination in Excel is to have another array that is binary, which I sum the product of both of the arrays to get the dummy cell. My objective function is then to subtract dummy from the target. Finally I use solver to try get the objective function to zero by changing the binary array. Bellow is the example of the code I have tried so far, and some snippets of the Excel I use running solver.

Really appreciate any help in this, have watched countless videos on Scipy, and I'm pretty stuck.

import numpy as np
from scipy.optimize import minimize, LinearConstraint

deals = np.array([11359992.5, 45892294.4, 10963487.39, 54817436.94, 43853949.55,
                 39352270.93, 51792041.32, 51809259.82, 25913243.16,
                 51671721.32,50836797.35])

target = 102508518.67

x = np.arange(len(deals))
constraint = LinearConstraint(x,lb=0,ub=1)
dummy = np.dot(deals,x)
objective_function = dif - dummy
res = minimize(fun = objective_function, x0=0, constraints=constraint)

Snippet of the Excel before running Solver Snippet of the Excel before running Solver

Snippet of the Excel after running Solver Snippet of the Excel after running Solver

blazej
  • 927
  • 4
  • 11
  • 21
  • Welcome to SO! So in short you just want to find a subset of deals such that the sum of the subset equals the target? Also, note that scipy.optimize doesn't provide solvers for mixed-integer programming. For this purpose, you can use packages like PuLP or Pyomo. – joni Jul 12 '21 at 16:43
  • Thanks!..correct, in short its just finding that subset that equals the target. Thanks for the insight, I checked out some of the Pyomo documentation but didnt think it was necessary. Will refresh to see if I find a way to the solution. – Sebastian T Jul 12 '21 at 18:44

0 Answers0