Restrict scipy.optimize.minimize to integer values

前端 未结 4 895
孤城傲影
孤城傲影 2020-12-10 02:00

I\'m using scipy.optimize.minimize to optimize a real-world problem for which the answers can only be integers. My current code looks like this:



        
4条回答
  •  -上瘾入骨i
    2020-12-10 02:55

    pulp solution

    After some research, I don't think your objective function is linear. I recreated the problem in the Python pulp library but pulp doesn't like that we're dividing by a float and 'LpAffineExpression'. This answer suggests that linear programming "doesn't understand divisions" but that comment is in context of adding constraints, not the objective function. That comment pointed me to "Mixed Integer Linear Fractional Programming (MILFP)" and on Wikipedia.

    Here's how you could do it in pulp if it actually worked (maybe someone can figure out why):

    import pulp
    
    data = [(481.79, 5), (412.04, 4), (365.54, 3)] #, (375.88, 3), (379.75, 3), (632.92, 5), (127.89, 1), (835.71, 6), (200.21, 1)]
    x = pulp.LpVariable.dicts('x', range(len(data)), lowBound=0, upBound=7, cat=pulp.LpInteger)
    
    numerator = dict((i,tup[0]) for i,tup in enumerate(data))
    denom_int = dict((i,tup[1]) for i,tup in enumerate(data))
    
    problem = pulp.LpProblem('Mixed Integer Linear Programming', sense=pulp.LpMinimize)
    
    # objective function (doesn't work)
    # TypeError: unsupported operand type(s) for /: 'float' and 'LpAffineExpression'
    problem += sum([numerator[i] / (denom_int[i] + x[i]) for i in range(len(data))])
    
    problem.solve()
    
    for v in problem.variables():
      print(v.name, "=", v.varValue)
    

    brute solution with scipy.optimize

    You can use brute and ranges of slices for each x in your function. If you have 3 xs in your function, you'll also have 3 slices in your ranges tuple. The key to all of this is to add the step size of 1 to the slice(start, stop,step) so slice(#, #, 1).

    from scipy.optimize import brute
    import itertools
    
    def f(x):
      return (481.79/(5+x[0]))+(412.04/(4+x[1]))+(365.54/(3+x[2]))
    
    ranges = (slice(0, 9, 1),) * 3
    result = brute(f, ranges, disp=True, finish=None)
    print(result)
    

    itertools solution

    Or you can use itertools to generate all combinations:

    combinations = list(itertools.product(*[[0,1,2,3,4,5,6,7,8]]*3))
    
    values = []
    for combination in combinations:
      values.append((combination, f(combination)))
    
    best = [c for c,v in values if v == min([v for c,v in values])]
    print(best)
    

    Note: this is a scaled-down version of your original function for example purposes.

提交回复
热议问题