<p>我根据您的示例描述构建了一个简单的模型</p>
<pre class="lang-python prettyprint-override"><code>from gekko import GEKKO
import numpy as np
m = GEKKO() # Initialize gekko
m.options.SOLVER=1 # APOPT is an MINLP solver
# optional solver settings with APOPT
m.solver_options = ['minlp_maximum_iterations 500', \
# minlp iterations with integer solution
'minlp_max_iter_with_int_sol 10', \
# treat minlp as nlp
'minlp_as_nlp 0', \
# nlp sub-problem max iterations
'nlp_maximum_iterations 50', \
# 1 = depth first, 2 = breadth first
'minlp_branch_method 1', \
# maximum deviation from whole number
'minlp_integer_tol 0.05', \
# covergence tolerance
'minlp_gap_tol 0.01']
x1 = m.Array(m.Var, 5, **{'value':0,'lb':0,'ub':1, 'integer': True}) # battery options
print(f'x1_initial: {x1}')
x2 = m.Array(m.Var, 5, **{'value':0,'lb':0,'ub':1, 'integer': True}) # bulb options
print(f'x2_initial: {x2}')
bat_cost = np.array([ 10, 2, 3, 4, 5]) # battery costs
bat_weigh = np.array([ 1, 25, 20, 19, 20]) # battery weighs
bulb_cost = np.array([ 2, 5, 33, 24, 5]) # bulb costs
bulb_weigh = np.array([ 6, 10, 2, 10, 20]) # bulb weighs
m.Equation( sum(bat_weigh * x1) + sum(bulb_weigh * x2) <= 25) # limit total weigh
m.Equation(m.sum(x1) == 1) # restrict choice to a single battery
m.Equation(m.sum(x2) == 1) # restrict choice to a single bulb
m.Obj( sum(bat_cost * x1) + sum(bulb_cost * x2) ) # Objective
m.solve(disp=False) # Solve
print('Results:')
print(f'x1: {x1}')
print(f'x2: {x2}')
print(f'battery cost: {sum(np.array([i[0] for i in x1]) * bat_cost)}')
print(f'battery weigh: {sum(np.array([i[0] for i in x1]) * bat_weigh)}')
print(f'bulb cost: {sum(np.array([i[0] for i in x2]) * bulb_cost)}')
print(f'bulb weigh: {sum(np.array([i[0] for i in x2]) * bulb_weigh)}')
print('Objective value: ' + str(m.options.objfcnval))
</code></pre>
<p>结果如下:</p>
<pre><code>x1_initial: [0 0 0 0 0]
x2_initial: [0 0 0 0 0]
Results:
x1: [[0.0] [0.0] [0.0] [1.0] [0.0]]
x2: [[1.0] [0.0] [0.0] [0.0] [0.0]]
battery cost: 4.0
battery weigh: 19.0
bulb cost: 2.0
bulb weigh: 6.0
Objective value: 6.0
</code></pre>
<p>这是一个非常简单的示例,演示如何表示电池和灯泡信息。它可以变得更复杂,但我需要更多的细节,理解为什么会有多项式方程,它们代表什么</p>
<p>重申一下,你所犯的错误与线路有关:</p>
<pre class="lang-python prettyprint-override"><code>m.Equation(check(x1,x2)<=5)
</code></pre>