Excel Solver,初始变量单元格值会影响求解器

时间:2017-02-03 12:48:34

标签: excel vba excel-vba solver boundary

我有以下问题: 我需要通过改变另一个单元格值来最小化单元格,这需要是一个整数。 我的Excel解算器有时可以正常工作,有时则无法解决问题。我注意到它取决于初始条件,因此根据初始变量单元格值,求解器可能会找到一个不是最优的解。

有什么想法吗?

附上你找到运行解算器的VBA代码。

 Sub optimizer1()

 optimizer1 Macro



    SolverReset                                                                             'resetting solver
    SolverOk SetCell:="$AD$4", MaxMinVal:=2, ValueOf:=0, ByChange:="$Q$4", Engine:= _
        1, EngineDesc:="GRG Nonlinear"                                                      'setting up target and input cell
    SolverAdd CellRef:="$Q$4", Relation:=4, FormulaText:="integer"                          'The value of variable cell must be integer
    'SolverAdd CellRef:="$U$4", Relation:=1, FormulaText:="$L$4"                            'setting up the range (these are the boundary conditions)
    'SolverAdd CellRef:="$U$4", Relation:=3, FormulaText:="$K$4"
    'SolverAdd CellRef:="$AA$4", Relation:=1, FormulaText:="$L$4"
    'SolverAdd CellRef:="$AA$4", Relation:=3, FormulaText:="$K$4" '(some Boundary conditions have been disabled, please neglect them for the purpose of the question)
    SolverOk SetCell:="$AD$4", MaxMinVal:=2, ValueOf:=0, ByChange:="$Q$4", Engine:= _
        1, EngineDesc:="GRG Nonlinear"
    SolverAdd CellRef:="$Q$4", Relation:=3, FormulaText:="$O$4"
    SolverOk SetCell:="$AD$4", MaxMinVal:=2, ValueOf:=0, ByChange:="$Q$4", Engine:= _
        1, EngineDesc:="GRG Nonlinear"                                                      ' Another boundary condition
    SolverOk SetCell:="$AD$4", MaxMinVal:=2, ValueOf:=0, ByChange:="$Q$4", Engine:= _
        1, EngineDesc:="GRG Nonlinear"
    SolverSolve UserFinish:=True
End Sub

谢谢!

0 个答案:

没有答案