Solver doesn't seem to handle constraints

BassssK

New Member
Joined
Apr 27, 2012
Messages
1
Hello Mr. Excel

I've been working on some solver model that has grown to 40 decision variables and about 60 constraints. It is a model that determines how to split some power demand over several engines, resulting in the lowest fuel consumption.
One problem is that this creates many local minima, that I've tried to overcome by setting several starting values. All these starting values are in the feasible area; they obey the constraints. Then when I run the solver, for some reason some of the values are set outside the constraints. This gives an error in the sheet, so the solution fails.

The code is rather large, but I think it helps if I post it anyway:

Code:
Sub Solver1()
'
' Macro1 Macro

'Starting point determines the best solution

solverreset

    'add all constraints
    ' relation 1 = "<="
    ' relation 2 = "="
    ' relation 3 = ">="
    ' relation 4 = "integer"
    ' relation 5 = "binary"
    
    'engines can't be negative
    SolverAdd CellRef:="$M$18:$M$20", Relation:=3, FormulaText:="0"
    SolverAdd CellRef:="$M$22:$M$24", Relation:=3, FormulaText:="0"
    SolverAdd CellRef:="$M$26:$M$28", Relation:=3, FormulaText:="0"
    SolverAdd CellRef:="$M$30:$M$32", Relation:=3, FormulaText:="0"
    SolverAdd CellRef:="$M$34:$M$36", Relation:=3, FormulaText:="0"
    SolverAdd CellRef:="$M$38:$M$40", Relation:=3, FormulaText:="0"
    SolverAdd CellRef:="$M$42:$M$44", Relation:=3, FormulaText:="0"
    SolverAdd CellRef:="$M$46:$M$48", Relation:=3, FormulaText:="0"
    SolverAdd CellRef:="$M$50:$M$52", Relation:=3, FormulaText:="0"
    SolverAdd CellRef:="$M$54:$M$56", Relation:=3, FormulaText:="0"
        
        
        
    'engines can't deliver more than Pbrake, including engine margin

        'ME 1
        SolverAdd CellRef:="$M$18", Relation:=1, FormulaText:="Con1_ME_Max_Load1*Con1_ME_Pb1"
        SolverAdd CellRef:="$M$22", Relation:=1, FormulaText:="Con1_ME_Max_Load1*Con1_ME_Pb1"
        SolverAdd CellRef:="$M$26", Relation:=1, FormulaText:="Con1_ME_Max_Load1*Con1_ME_Pb1"
        SolverAdd CellRef:="$M$30", Relation:=1, FormulaText:="Con1_ME_Max_Load1*Con1_ME_Pb1"
        SolverAdd CellRef:="$M$34", Relation:=1, FormulaText:="Con1_ME_Max_Load1*Con1_ME_Pb1"
        SolverAdd CellRef:="$M$38", Relation:=1, FormulaText:="Con1_ME_Max_Load1*Con1_ME_Pb1"
        SolverAdd CellRef:="$M$42", Relation:=1, FormulaText:="Con1_ME_Max_Load1*Con1_ME_Pb1"
        SolverAdd CellRef:="$M$46", Relation:=1, FormulaText:="Con1_ME_Max_Load1*Con1_ME_Pb1"
        SolverAdd CellRef:="$M$50", Relation:=1, FormulaText:="Con1_ME_Max_Load1*Con1_ME_Pb1"
        SolverAdd CellRef:="$M$54", Relation:=1, FormulaText:="Con1_ME_Max_Load1*Con1_ME_Pb1"
        
        'ME 2
        SolverAdd CellRef:="$M$19", Relation:=1, FormulaText:="Con1_ME_Max_Load2*Con1_ME_Pb2"
        SolverAdd CellRef:="$M$23", Relation:=1, FormulaText:="Con1_ME_Max_Load2*Con1_ME_Pb2"
        SolverAdd CellRef:="$M$27", Relation:=1, FormulaText:="Con1_ME_Max_Load2*Con1_ME_Pb2"
        SolverAdd CellRef:="$M$31", Relation:=1, FormulaText:="Con1_ME_Max_Load2*Con1_ME_Pb2"
        SolverAdd CellRef:="$M$35", Relation:=1, FormulaText:="Con1_ME_Max_Load2*Con1_ME_Pb2"
        SolverAdd CellRef:="$M$39", Relation:=1, FormulaText:="Con1_ME_Max_Load2*Con1_ME_Pb2"
        SolverAdd CellRef:="$M$43", Relation:=1, FormulaText:="Con1_ME_Max_Load2*Con1_ME_Pb2"
        SolverAdd CellRef:="$M$47", Relation:=1, FormulaText:="Con1_ME_Max_Load2*Con1_ME_Pb2"
        SolverAdd CellRef:="$M$51", Relation:=1, FormulaText:="Con1_ME_Max_Load2*Con1_ME_Pb2"
        SolverAdd CellRef:="$M$55", Relation:=1, FormulaText:="Con1_ME_Max_Load2*Con1_ME_Pb2"
    
        'diesel generator sets
        SolverAdd CellRef:="$M$20", Relation:=1, FormulaText:="Con1_DG_Max_Load*Con1_DG_Pbtot"
        SolverAdd CellRef:="$M$24", Relation:=1, FormulaText:="Con1_DG_Max_Load*Con1_DG_Pbtot"
        SolverAdd CellRef:="$M$28", Relation:=1, FormulaText:="Con1_DG_Max_Load*Con1_DG_Pbtot"
        SolverAdd CellRef:="$M$32", Relation:=1, FormulaText:="Con1_DG_Max_Load*Con1_DG_Pbtot"
        SolverAdd CellRef:="$M$36", Relation:=1, FormulaText:="Con1_DG_Max_Load*Con1_DG_Pbtot"
        SolverAdd CellRef:="$M$40", Relation:=1, FormulaText:="Con1_DG_Max_Load*Con1_DG_Pbtot"
        SolverAdd CellRef:="$M$44", Relation:=1, FormulaText:="Con1_DG_Max_Load*Con1_DG_Pbtot"
        SolverAdd CellRef:="$M$48", Relation:=1, FormulaText:="Con1_DG_Max_Load*Con1_DG_Pbtot"
        SolverAdd CellRef:="$M$52", Relation:=1, FormulaText:="Con1_DG_Max_Load*Con1_DG_Pbtot"
        SolverAdd CellRef:="$M$56", Relation:=1, FormulaText:="Con1_DG_Max_Load*Con1_DG_Pbtot"
        
    'engine cylinder numbers between specified in catalog (not yet)
    'making cyls part of changing cells doesn't work
    'instead, test multiple engine configurations parallel (more solvers at once)
       
    'Interaction between PTO, PTI and engines
    'kW EM = P el - kW DG
    SolverAdd CellRef:="$M$21", Relation:=2, FormulaText:="P_el1-$M20"
    SolverAdd CellRef:="$M$25", Relation:=2, FormulaText:="P_el2-$M24"
    SolverAdd CellRef:="$M$29", Relation:=2, FormulaText:="P_el3-$M28"
    SolverAdd CellRef:="$M$33", Relation:=2, FormulaText:="P_el4-$M32"
    SolverAdd CellRef:="$M$37", Relation:=2, FormulaText:="P_el5-$M36"
    SolverAdd CellRef:="$M$41", Relation:=2, FormulaText:="P_el6-$M40"
    SolverAdd CellRef:="$M$45", Relation:=2, FormulaText:="P_el7-$M44"
    SolverAdd CellRef:="$M$49", Relation:=2, FormulaText:="P_el8-$M48"
    SolverAdd CellRef:="$M$53", Relation:=2, FormulaText:="P_el9-$M52"
    SolverAdd CellRef:="$M$57", Relation:=2, FormulaText:="P_el10-$M56"
    'kW EM = kW ME - P prop
    SolverAdd CellRef:="$M$21", Relation:=2, FormulaText:="$M$18+$M$19-P_prop1"
    SolverAdd CellRef:="$M$25", Relation:=2, FormulaText:="$M$22+$M$23-P_prop2"
    SolverAdd CellRef:="$M$29", Relation:=2, FormulaText:="$M$26+$M$27-P_prop3"
    SolverAdd CellRef:="$M$33", Relation:=2, FormulaText:="$M$30+$M$31-P_prop4"
    SolverAdd CellRef:="$M$37", Relation:=2, FormulaText:="$M$34+$M$35-P_prop5"
    SolverAdd CellRef:="$M$41", Relation:=2, FormulaText:="$M$38+$M$39-P_prop6"
    SolverAdd CellRef:="$M$45", Relation:=2, FormulaText:="$M$42+$M$43-P_prop7"
    SolverAdd CellRef:="$M$49", Relation:=2, FormulaText:="$M$46+$M$47-P_prop8"
    SolverAdd CellRef:="$M$53", Relation:=2, FormulaText:="$M$50+$M$51-P_prop9"
    SolverAdd CellRef:="$M$57", Relation:=2, FormulaText:="$M$54+$M$55-P_prop10"
    
    
    SolverOptions MaxTime:=100, Iterations:=100, Precision:=0.001, AssumeLinear _
        :=False, StepThru:=False, Estimates:=1, Derivatives:=1, SearchOption:=1, _
        IntTolerance:=0.01, Scaling:=False, Convergence:=0.0001, AssumeNonNeg:=False
    SolverOk SetCell:="$M62", MaxMinVal:=2, ValueOf:="0", ByChange:="$M$18:$M$57"
    SolverSolve True

End sub

Some info to clarify:
- The variables are in in column $M
- Con1_ME_Max_Load1*Con1_ME_Pb1 means the maximum loading of the engines. That is 90% brake power. These values are in the sheet and are just integers. A watch in the VBA editor shows correct values for these.
- The Pprop and Pel variables are the demand. The decision variables should meet these demand, through the given constraints.

- The error I get seems to be when the constraint
Code:
SolverAdd CellRef:="$M$42", Relation:=1, FormulaText:="Con1_ME_Max_Load1*Con1_ME_Pb1"
is close to the Pprop power demand.

Maybe it conflicts with this constraint or something
Code:
SolverAdd CellRef:="$M$45", Relation:=2, FormulaText:="$M$42+$M$43-P_prop7"
but then why does it happen only if the constraint is close to the demand? It works fine on all other lines.

Sorry for the rather long post, but I've spent about all my friday afternoon on this and still couldn't get it to work without errors.
If I need to clarify some more, please say so.


Any help would be greatly appreciated.

Bas
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top