Solver Tutorial - Step by Step - Product Mix Example - in Visual BasicVB Product Mix ExampleFollow these steps to define and solve the Product Mix problem in a Visual Basic program (the steps in another Windows programming language would be very similar): Creating a Visual Basic ProgramAssuming that you have Visual Basic and a Solver DLL product installed, and you've opened a new or existing project, the next step is to define a VB function where the formulas for the objective function and the constraints are calculated. In the VB code module below, we define a function FuncEval() with the arguments required by the Solver DLL products. (Click on the code module for a full-size image.) In the above code, we've written the formulas to correspond directly to our earlier outline of the problem (click on Writing the Formulas to see this again). But we could also use Visual Basic arrays and FOR loops to compute these values. (In fact, we can let the Solver DLL compute the objective and constraint values for us, using just the coefficients such as 75, 50 and 35 for the objective function. Read the page Using the LP Coefficient Matrix Directly in our Advanced Tutorial for more details on this approach.) Using the Solver DLL FunctionsNext, we must tell the Solver DLL about elements of the model that aren't included in the FuncEval() function above. For example, the left hand sides of the constraints are computed in FuncEval(), but the constant right hand sides (450 Chassis, 250 Picture tubes, etc.), as well as lower bounds of 0 on the variables, must be specified separately. To do this, we call the Solver DLL loadnlp() function and pass appropriate arguments, as shown in the code module below. (Click on the code module for a full-size image.) In this code segment, obj, rhs, sense, matval, x, lb and ub are previously defined arrays -- all of type Double, except for sense which is of type Byte. Other symbols such as NullL and INFBOUND are predefined. The call to loadnlp() defines a problem and passes these arrays as arguments The obj and matval arguments are work arrays for the Solver DLL, and need not be initialized when used with loadnlp(). Finding and Using the SolutionOnce the problem is defined, we can simply call the optimize() function to find the optimal solution. We then call the solution() function to retrieve the solution status, the objective function, and the decision variable values at the optimal solution, as shown in the code module below. (Click on the code module for a full-size image.) The solution status is an integer code reporting the status of the optimization. For example, 1 (symbolic name PSTAT_OPTIMAL) means that an optimal solution was found, 2 (symbolic name PSTAT_INFEASIBLE) means that there is no feasible solution (values for the variables that will satisfy all of the constraints), and so on. All of the possible status codes are defined in the Solver DLL Programmer's Guide. If you've gotten to this point, congratulations! You've successfully set up and solved a simple Solver problem using Visual Basic. If you like, you can see how to set up and solve the same Product Mix problem in Microsoft Excel. If you haven't yet read the other parts of the tutorial, you may want to return to the Tutorial Start and read the overviews "What are Solvers Good For?", "How Do I Define a Model?" and "What Makes a Model Hard to Solve?" This was an example of a linear programming problem. Other types of optimization problems may involve quadratic programming, mixed-integer programming, constraint programming, smooth nonlinear optimization, global optimization, and nonsmooth optimization. To learn more, click on Optimization Problem Types. For a more advanced explanation of linearity and sparsity in optimization problems, and use of the Solver DLL loadlp() function, continue with our Advanced Tutorial. Next: Creating an Excel Worksheet >
|



