Using VBA to Call Solver for Optimization in Excel

solver via vba n.w
1 / 26
Embed
Share

Learn how to enhance your Excel optimization process by utilizing VBA to call Solver. Discover the benefits of embedding Solver commands into VBA code and setting up Solver parameters using VBA functions. Reference the Solver library, identify decision variables, objective functions, and constraints, and input Solver parameters efficiently with VBA in Excel.

  • Excel Optimization
  • VBA Solver
  • Solver Parameters
  • Excel VBA
  • Optimization Techniques

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Solver via VBA IE 469 Spring 2019

  2. Why do we need to use VBA to call solver? When you work with more complex codes that includes optimization, it is a good idea to embed solver commands into the VBA code. VBA has functions to run Solver. BUT! You need to have the problem setup on the worksheet just as we did before. Afterwards, you can use the VBA Solver functions to setup the solver i.e., filling in the parameters in the Solver Dialog Box via VBA Slide 2

  3. Solver via VBA Before using Solver commands in VBA, you must reference the Solver library in the VBE. To do this, go to Tools > References and choose Solver from the list. Slide 3

  4. Solver via VBA Previously, when using the Solver from the Tools menu in Excel, we identified the cells which contain the decision variables, objective function, and constraint equations to the Solver using the Changing Cells, Target Cell, and Add Constraint inputs, respectively. We now learn how to identify these parts of the model as Solver input using VBA code. Slide 4

  5. Solver via VBA There are two main Solver functions used to input the Solver parameters in VBA. These are SolverOK and SolverAdd. SolverOK is used to set the objective function (or Target Cell) and decision variables (or Changing Cells). The format of this function and argument titles are: SolverOK SetCell, MaxMinVal, ValueOf, ByChange, Engine Slide 5

  6. Solver via VBA SetCell argument is used to specify the range of the objective function. This cell should contain the formula of the objective function which references the decision variable cells. The MaxMinVal argument specifies objective type 1 (= maximize) 2 (= minimize) 3 (= value). If this is selected, then the ValueOf argument is used to set this value; if the objective function will be maximized or minimized, then this argument is ignored. Some examples would be solving a problem to a break-even point ByChange argument specifies the range which contains the decision variables. This range of cells should not have any values in it Slide 6

  7. Solver via VBA SolverOk Range("D14"), 1, , Range("D10:H10"), 2 Objective Function Simplex LP Changing Cells Maximize Slide 7

  8. Solver via VBA The second main Solver function will be used to input constraints; this is the SolverAdd which should be used to add each individual constraint or each group of similar constraints. The SolverAdd function has three arguments: SolverAdd CellRef, Relation, FormulaText Slide 8

  9. Solver via VBA CellRef argument specifies the range which contains a constraint equation. This equation should reference the decision variable cells. The Relation argument specifies the inequality of the constraint: 1 is <= 2 is = 3 is >= 4 is int (integer values) and 5 is bin (binary, 0/1, values). The FormulaText argumentspecifies the range which contains the RHS value of the constraint. Slide 9

  10. Solver via VBA SolverAdd Range("D12"), 1, Range("F12") LHS RHS <= Slide 10

  11. Solver via VBA There are two more functions which can be used to modify constraints: they are SolverChange and SolverDelete. These functions will allow you to modify or delete a constraints, respectively. They both have the same arguments as the SolverAdd function. If CellRef and Relation do not match an existing constraint, you must use the SolverDelete and SolverAdd functions to change the constraint. SolverChange Range("D12"), 1, Range("F15") Slide 11

  12. Solver via VBA To set the Solver options in VBA, we use the SolverOptions function. This function has many arguments for each of the options we have seen previously in the Solver Options dialog box. SolverOptions( MaxTime, Iterations, Precision, AssumeLinear, StepThru, Estimates, Derivatives, SearchOption, IntTolerance, Scaling, Convergence, AssumeNonNeg, PopulationSize, RandomSeed, MultiStart, RequireBounds, MutationRate, MaxSubproblems, MaxIntegerSols, SolveWithout, MaxTimeNoImp) Slide 12

  13. Solver via VBA There are two arguments which we will use more frequently, which are AssumeLinear and AssumeNonNeg. Both of these arguments take True/False values; True makes the corresponding assumption. For most of our models, we will set both of these arguments to true as follows: SolverOptions AssumeLinear:=True, AssumeNonNeg:=True Slide 13

  14. Solver via VBA After the Solver input has been entered and any options have been set, we are ready to run the Solver. To run the Solver in VBA, we use the function SolverSolve. This function has two arguments and is written as follows: SolverSolve(UserFinish, ShowRef) Slide 14

  15. Solver via VBA The UserFinish argument uses a True/False value to determine whether to return the Solver results with or without showing the Solver Results dialog box. We will usually set this argument value to True; if the value is False then the Solver Results dialog box will appear after the Solver has run the model. The ShowRef argument is used when the StepThru option is set; hence, we will usually ignore this argument. For the above example, we would type the following: SolverSolve True Slide 15

  16. Solver via VBA The SolverSolve function also returns an integer value classifying the result. 0 Solver found a solution. All constraints and optimality conditions are satisfied. 1 Solver has converged to the current solution. All constraints are satisfied. 2 Solver cannot improve the current solution. All constraints are satisfied. 4 The Objective Cell values do not converge. 5 Solver could not find a feasible solution. Slide 16

  17. Solver via VBA Dim result As Integer result = SolverSolve (True) If result = 5 Then MsgBox Your problem was infeasible. Please modify your model. End If Slide 17

  18. Other VBA Commands The fullset of possible commands to run Solver via VBA: SolverAdd Function SolverChange Function SolverDelete Function SolverFinish Function SolverFinishDialog Function SolverGet Function SolverLoad Function SolverOk Function SolverOkDialog Function SolverOptions Function SolverReset Function SolverSave Function SolverSolve Function Slide 18

  19. OpenSolver OpenSolver also has similar commands. Check www.opensolver.org Slide 19

  20. Heuristic Algorithms via VBA IE 469 Spring 2019

  21. Why do we need Heuristic Algorithms after all? We could use Excel Solver or Open Solver to solve optimization problems. So, why is there a need to consider heuristic algorithms? Slide 21

  22. Why do we need Heuristic Algorithms after all? Some problems are very hard to solve. Even though given a solution instance it is very easy to verify if it is feasible and if so, the value of the objective function; there is no efficient way to find the optimal solution. As the problem size grows bigger it gets even harder to solve them optimally. Example: TSP problem It all comes down to computational complexity. Many of these hard probems are said to be NP-Complete. Slide 22

  23. Heuristic Algorithms When you start working, you will have absolutely no excuse to your manager if you fail to solve a problem assigned to you or your team! Don t worry, in real life circumstances you do NOT usually need the OPTIMAL solution after all! A satisfactory solution provided in a reasonable time would be sufficient in many circumstances. Usually, these satisfactory solutions are not too far away from the optimal. In some cases and if you are lucky enough, they may even be the optimal solution you were looking for. (But there is no guarantee!) Slide 23

  24. Heuristic Algorithms This course is NOT a heuristic algorithm design course. You are NOT expected to come up with a heuristic algorithm. BUT, given a heuristic algorithm design sometimes a pseudocode, you should be able to code them in VBA. These will not be complex algorithms I promise Slide 24

  25. Traveling Salesman Problem Given a graph, we should find the minimum distance tour that will Start and end at the same node. All nodes are visited once. Slide 25

  26. Traveling Salesman Problem The algorithm we will use is called the Nearest Neighbor Algorithm. (Constructive Heuristic) Assumption: The graph is fully connected! Algorithm: Start from an arbitrary node a. From a, go to another node, b such that the node b has not been visited before and the distance (a,b) is the shortest among all other unvisited nodes. Stop when all nodes are visited. Now, let s code it! Slide 26

More Related Content