Restrict scipy.optimize.minimize to integer values

前端 未结 4 897
孤城傲影
孤城傲影 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:56

    One thing that might help your problem you could have a constraint as:

    max([x-int(x)])=0
    

    This is not going to completely solve your problem, the algorithm will still try and cheat and you will get values with some level of error ~±5e-10 that it will still try and optimize towards just by the error in scipy's algorithm but it's better than nothing.

    cons = ({'type':'eq', 'fun': con},
            {'type':'eq','fun': lambda x : max([x[i]-int(x[i]) for i in range(len(x))])})
    

    having tested this process on some optimizations I know the solution to, this process is more sensitive to the initial values than the unconstrained search, it gets fairly accurate answers however the solution may actually not find the true value, you are basically requiring the large jump of the optimization process (what it uses to make sure it's not optimizing to a local minimum) to search the sample space as the smaller increments are usually not strong enough to move to the next number over.

提交回复
热议问题