-
Notifications
You must be signed in to change notification settings - Fork 1
Using Without Registration
The ActiveX DLL can be called from your VBA projects without registration. You can find the 32- and 64-bit versions of the DLL's in the https://github.com/GCuser99/SolverWrapper/tree/main/Build folder of this repository, or generate your own builds from the .twinproj file.
THIS IS NOT THE RECOMMENDED SETUP, but is shown here just to show the possibilities. If you go the non-registration route, you will not have access to Intelisense nor the Object Browser from VBA.
These are declares needed to run twinBASIC DLL without registration:
Option Explicit
Public Declare PtrSafe Function New\_SolvProblem Lib "\[Path to DLL]\\SolverWrapper\_win64.dll" () As Object
'Public Declare PtrSafe Function New\_SolvProblem Lib "\[Path to DLL]\\SolverWrapper\_win32.dll" () As Object
Public Enum SlvGoalType
  slvMaximize = 1
  slvMinimize = 2
  slvTargetValue = 3
  \[\_First] = 1
  \[\_Last] = 3
End Enum
Public Enum SlvShowTrial
  slvContinue = 0
  slvStop = 1
  \[\_First] = 0
  \[\_Last] = 1
End Enum
Public Enum SlvEstimates
  slvTangent = 1
  slvQuadratic = 2
  \[\_First] = 1
  \[\_Last] = 2
End Enum
Public Enum SlvDerivatives
  slvForward = 1
  slvCentral = 2
  \[\_First] = 1
  \[\_Last] = 2
End Enum
Public Enum SlvSearchOption
  slvNewton = 1
  slvConjugate = 2
  \[\_First] = 1
  \[\_Last] = 2
End Enum
Public Enum SlvRelation
  slvLessThanEqual = 1
  slvEqual = 2
  slvGreaterThanEqual = 3
  slvInt = 4
  slvBin = 5
  slvAllDif = 6
  \[\_First] = 1
  \[\_Last] = 6
End Enum
Public Enum SlvSolveMethod
  slvGRG\_Nonlinear = 1
  slvSimplex\_LP = 2
  slvEvolutionary = 3
  \[\_First] = 1
  \[\_Last] = 3
End Enum
Public Enum SlvCallbackReason
  slvShowIterations = 1
  slvMaxTimeLimit = 2
  slvMaxIterationsLimit = 3
  slvMaxSubproblemsLimit = 4
  slvMaxSolutionsLimit = 5
  \[\_First] = 1
  \[\_Last] = 5
End Enum
Private Enum SolverMode
  SolveMode = 0
  CloseMode = 1
  CancelRestoreMode = 2
  \[\_First] = 0
  \[\_Last] = 2
End Enum
Public Enum SlvMsgCode
  slvFoundSolution = 0
  slvConvergedOnSolution = 1
  slvCannotImproveSolution = 2
  slvMaxIterReached = 3
  slvObjectiveNotConvergent = 4
  slvCouldNotFindSolution = 5
  slvStoppedByUser = 6
  slvProblemNotLinear = 7
  slvProblemTooLarge = 8
  slvErrorInObjectiveOrConstraint = 9
  slvMaxTimeReached = 10
  slvNotEnoughMemory = 11
  slvNoDocumentation = 12
  slvErrorInModel = 13
  slvFoundIntegerSolution = 14
  slvMaxSolutionsReached = 15
  slvMaxSubProblemsReached = 16
  slvConvergedToGlobalSolution = 17
  slvAllVariablesMustBeBounded = 18
  slvBoundsConflictWithBinOrAllDif = 19
  slvBoundsAllowNoSolution = 20
  \[\_First] = 0
  \[\_Last] = 20
End Enum
The example below automates solving the problem in SOLVSAMP.XLS on the "Portfolio of Securities" worksheet.
SOLVSAMP.XLS is distributed with MS Office Excel and can be found in:
Application.LibraryPath \& "\\..\\SAMPLES\\SOLVSAMP.XLS"
which on many systems can be found here:
C:\\Program Files\\Microsoft Office\\root\\Office16\\SAMPLES\\SOLVSAMP.XLS
Copy the code below into the sample workbook and then save SOLVSAMP.XLS to SOLVSAMP.XLSM.
Option Explicit
'This is a non-linear problem - use slvGRG\_Nonlinear
Sub Solve\_Portfolio\_of\_Securities()
  Dim oProblem As Object
  Dim ws As Worksheet
 
  Set oProblem = New\_SolvProblem
 
  Set ws = ThisWorkbook.Worksheets("Portfolio of Securities")
 
  'initialize the problem by passing a reference to the worksheet of interest
  oProblem.Initialize ws
 
  'define the objective cell to be optimized
  oProblem.Objective.Define "E18", slvMaximize
 
  'define and initialize the decision cell(s)
  oProblem.DecisionVars.Add "E10:E14"
  oProblem.DecisionVars.Initialize 0.2, 0.2, 0.2, 0.2, 0.2
 
  'add some constraints
  oProblem.Constraints.AddBounded "E10:E14", 0#, 1#
  oProblem.Constraints.Add "E16", slvEqual, 1#
  oProblem.Constraints.Add "G18", slvLessThanEqual, 0.071
 
  'set the solver engine to use
  oProblem.Solver.Method = slvGRG\_Nonlinear
 
  'set some solver options
  oProblem.Solver.Options.AssumeNonNeg = True
  oProblem.Solver.Options.RandomSeed = 7
 
  oProblem.Solver.SaveAllTrialSolutions = True
  'solve the optimization problem
  oProblem.SolveIt
 
  'save all trial solutions that passed the constraints to the worksheet
  If oProblem.Solver.SaveAllTrialSolutions Then
  ws.Range("o2:az10000").ClearContents
  oProblem.SaveSolutionsToRange ws.Range("o2"), keepOnlyValid:=True
  End If
End Sub
Getting Started
How-to Topics
- Solver Primer
- A Walkthrough Example
- Using the Enhanced Callback
- Using SolverWrapper Events
- ActiveX DLL FAQ
- Using Without Registration
Object Model Overview