简体   繁体   中英

Solver constraints ignored

i am facing a new problem that drives me crazy for two days now: I'm trying to run the following:

Sub getNoShortSalePortfolio()

Dim ws As Worksheet
Set ws = ActiveWorkbook.ActiveSheet

Dim assetReturns As ListObject
Set assetReturns = ws.ListObjects("tblAssetReturns")

Dim numColumnsAssets As Long
numColumnsAssets = assetReturns.ListColumns.Count
    
Dim portfolioWeightsVector() As Variant
Dim activeCellAddress As Variant
Dim adjustableCellsAddress As String

activeCellAddress = ws.Range("noShortSale").Address

adjustableCellsAdress = activeCellAddress & ":" & Range(activeCellAddress).Offset(numColumnsAssets - 2, 0).Address

Range(adjustableCellsAdress).ClearContents

Solverreset

SolverOptions Precision:=0.0001, Iterations:=10, AssumeNonNeg:=True
SolverOk SetCell:=Range("noShortSaleMean"), MaxMinVal:=1, ByChange:=Range(adjustableCellsAdress) ', EngineDesc:="GRG Nonlinear"

SolverAdd CellRef:=Range(adjustableCellsAdress), Relation:=1, FormulaText:=1
SolverAdd CellRef:=Range("noShortSaleWeightSum"), Relation:=3, FormulaText:=1
SolverAdd CellRef:=Range("noShortSaleVola"), Relation:=2, FormulaText:=Range("portfolioVolaAssetShare")

SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
End Sub

However, my solver just iterates 2 times and returns a vector of solutions that are all equal (in my case say 10% as i want to constrain the sum of my solutions to be smaller or equal to 100%.

Furthermore, i want as another constraint that a function of my solution vector meets a predetermined value from another cell, however, the solver seems to ignore this constraint totally. What am i doing wrong?

EDIT: Here is a screenshot of the solver在此处输入图像描述

Manually running the solver with在此处输入图像描述

reaches the desired (and expected solution: 在此处输入图像描述 However, it seems the target cell and the other constraint are not defined in my VBA..

I appreciate any help

Thanks a lot

Thomas

 Sub getNoShortSalePortfolio()

Dim ws As Worksheet
Set ws = ActiveWorkbook.ActiveSheet

Dim assetReturns As ListObject
Set assetReturns = ws.ListObjects("tblAssetReturns")

Dim numColumnsAssets As Long
numColumnsAssets = assetReturns.ListColumns.Count
    
Dim portfolioWeightsVector() As Variant
Dim activeCellAddress As Variant
Dim adjustableCellsAddress As String

activeCellAddress = ws.Range("noShortSale").Address

adjustableCellsAdress = activeCellAddress & ":" & Range(activeCellAddress).Offset(numColumnsAssets - 2, 0).Address

Range(adjustableCellsAdress).ClearContents

Solverreset

SolverOptions Precision:=0.000001, Iterations:=20, AssumeNonNeg:=True
SolverOk SetCell:="noShortSaleMean", MaxMinVal:=1, ValueOf:=0, ByChange:=Range(adjustableCellsAdress), Engine:=1, EngineDesc:="GRG Nonlinear"
'SolverOk SetCell:="noShortSaleVola", MaxMinVal:=2, ByChange:=Range(adjustableCellsAdress), EngineDesc:="GRG Nonlinear"

SolverAdd CellRef:=Range(adjustableCellsAdress), Relation:=1, FormulaText:=1
SolverAdd CellRef:="noShortSaleWeightSum", Relation:=2, FormulaText:=1
SolverAdd CellRef:="noShortSaleVola", Relation:=2, FormulaText:="portfolioVolaAssetShare"

SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1 
End Sub

I found one way to solve this issue.. I need to remove these Range() stuff, then it behaves normally (.ie as the manual solution)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM