将 Silver 应用到各个行

Applying Solver to various rowsa

我想知道如何获取代码以对多行(300 行)执行求解器操作。有一个目标变量,几个约束,以及两个应该优化的变量。使用下面的命令,我得到一个错误,这个问题对于 Solver 来说太大了,这只能是语法错误。这是我的语法:

Dim i As Long
For i = 7 To 310

SolverOk SetCell:="$AA$" & i, MaxMinVal:=2, ValueOf:=0, ByChange:="$AE$:$AF$" & i, _
    Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$AA$" & i, Relation:=1, FormulaText:="-0.000001"
SolverOk SetCell:="$AA$" & i, MaxMinVal:=2, ValueOf:=0, ByChange:="$AE$:$AF$" & i, _
    Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$AB$" & i, Relation:=3, FormulaText:="0.000001"
SolverOk SetCell:="$AA$" & i, MaxMinVal:=2, ValueOf:=0, ByChange:="$AE$:$AF$" & i, _
    Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$AC$" & i, Relation:=1, FormulaText:="-0.000001"
SolverOk SetCell:="$AA$" & i, MaxMinVal:=2, ValueOf:=0, ByChange:="$AE$:$AF$" & i, _
    Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$AD$" & i, Relation:=3, FormulaText:="0.000001"
SolverOk SetCell:="$AA$" & i, MaxMinVal:=2, ValueOf:=0, ByChange:="$AE$:$AF$" & i, _
    Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$AE$" & i, Relation:=3, FormulaText:="0"
SolverOk SetCell:="$AA$" & i, MaxMinVal:=2, ValueOf:=0, ByChange:="$AE$:$AF$" & i, _
    Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$AF$" & i, Relation:=3, FormulaText:="0"
SolverOk SetCell:="$AA$" & i, MaxMinVal:=2, ValueOf:=0, ByChange:="$AE$:$AF$" & i, _
    Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$AE$" & i, Relation:=1, FormulaText:="1"
SolverOk SetCell:="$AA$" & i, MaxMinVal:=2, ValueOf:=0, ByChange:="$AE$:$AF$" & i, _
    Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="$AF$" & i, Relation:=1, FormulaText:="1.5"
SolverOk SetCell:="$AA$" & i, MaxMinVal:=2, ValueOf:=0, ByChange:="$AE$:$AF$" & i, _
    Engine:=1, EngineDesc:="GRG Nonlinear"
SolverOptions MaxTime:=0, Iterations:=1000000, Precision:=0.000001, Convergence _
    :=0.0001, StepThru:=True, Scaling:=True, AssumeNonNeg:=True, Derivatives:=1
SolverOptions PopulationSize:=100, RandomSeed:=0, MutationRate:=0.075, Multistart _
    :=False, RequireBounds:=False, MaxSubproblems:=0, MaxIntegerSols:=0, _
    IntTolerance:=1, SolveWithout:=False, MaxTimeNoImp:=30
   SolverSolve
Next i

End Sub

也许这就是你所需要的,汉娜:

Sub hanna()
  Dim i As Long

  For i = 7 To 310
    SolverReset

    With Rows(i)
      SolverOk SetCell:=.Range("AA1").Address, _
               MaxMinVal:=2, _
               ByChange:=.Range("AE1:AF1").Address, _
               Engine:=1
      SolverAdd CellRef:=.Range("AA1").Address, Relation:=1, FormulaText:="-0.000001"
      SolverAdd CellRef:=.Range("AB1").Address, Relation:=3, FormulaText:="0.000001"
      SolverAdd CellRef:=.Range("AC1").Address, Relation:=1, FormulaText:="-0.000001"
      SolverAdd CellRef:=.Range("AD1").Address, Relation:=3, FormulaText:="0.000001"
      SolverAdd CellRef:=.Range("AE1").Address, Relation:=1, FormulaText:="1"
      SolverAdd CellRef:=.Range("AF1").Address, Relation:=1, FormulaText:="1.5"
      SolverSolve UserFinish:=True
    End With
  Next i
End Sub