Restrict scipy.optimize.minimize to integer values

前端 未结 4 932
孤城傲影
孤城傲影 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条回答
  •  孤街浪徒
    2020-12-10 02:57

    Here is a way to solve the Mixed Integer Nonlinear Programming problem with Python Gekko (a package that I maintain):

    from gekko import GEKKO
    
    m = GEKKO(remote=False)
    x = m.Array(m.Var,9,lb=0,ub=7,integer=True)
    
    def f(x):
        return (481.79/(5+x[0]))+(412.04/(4+x[1]))\
               +(365.54/(3+x[2]))+(375.88/(3+x[3]))\
               +(379.75/(3+x[4]))+(632.92/(5+x[5]))\
               +(127.89/(1+x[6]))+(835.71/(6+x[7]))\
               +(200.21/(1+x[8]))
    
    m.Minimize(f(x))
    m.Equation(sum(x)==7)
    m.options.SOLVER=1
    m.solve()
    print(x)
    

    This gives the solution:

     ---------------------------------------------------
     Solver         :  APOPT (v1.0)
     Solution time  :  0.0529 sec
     Objective      :  859.5269999999999
     Successful solution
     ---------------------------------------------------
    
    
    [[0.0] [0.0] [1.0] [1.0] [1.0] [0.0] [1.0] [0.0] [3.0]]
    

提交回复
热议问题