For a very small problem (actually a series of related small problems) I tried to use Solver from VBA. I encountered two bugs that were not completely trivial to find a workaround for.
Sub runsolver() Dim sumcell As String Dim objcell As String Dim xcells As String Dim fixcell As String Dim fxvalue As Double: fxvalue = 0.5 sumcell = Range("sumcell").Address(True, True, xlA1, True) objcell = Range("objcell").Address(True, True, xlA1, True) xcells = Range("xcells").Address(True, True, xlA1, True) fixcell = Range("xcells").Cells(1, 1).Address(True, True, xlA1, True) Call runsolver2(sumcell, objcell, xcells, fixcell, fxvalue) End Sub Sub runsolver2(sumcell As String, objcell As String, xcells As String, fixcell As String, fixvalue As Variant) Call Solver.SolverReset ' constraint: sum(x) = 1 ' this constraint is ignored ' workaround: FormulaText:=0.99999999999999 Call Solver.SolverAdd(CellRef:=sumcell, Relation:=2, FormulaText:=1) ' constraint: x1=0.5 ' this constraint leads to unexpected internal error or out of memory ' workaround: declare argument as: BYVAL fixvalue as variant Call Solver.SolverAdd(CellRef:=fixcell, Relation:=2, FormulaText:=fixvalue) Call Solver.SolverOk(SetCell:=objcell, MaxMinVal:=2, ValueOf:=0, ByChange:=xcells) Call Solver.SolverOptions(AssumeNonNeg:=True) Dim rc As Integer rc = Solver.SolverSolve(True) Call Solver.SolverFinish(KeepFinal:=1, ReportArray:=Array(1)) End Sub
The first bug is somewhat incomprehensible to me: if I introduce a constraint like x1+x2=1 the whole constraint is completely ignored (without a warning message). When I change the 1 into 0.99999999 the constraint is accepted and will be used in the optimization of the model.
The second bug is an Unexpected Internal Error or Out Of Memory. The workaround is mentioned in the above code.
After applying the workarounds I get the correct results:
The problem occurs in different Excel versions including Office 2013.
------------------------------
Erwin Kalvelagen
Amsterdam Optimization Modeling Group
erwin@amsterdamoptimization.
http://amsterdamoptimization.
------------------------------