Local Solver Quick Start Guide (series 2): the bucket with the largest capacity

The first model of LocalSolver-the bucket with the largest capacity

We will start by optimizing the shape of a bucket. This is a typical non-linear problem. You can use LocalSolver to directly model and obtain an optimized solution in a few seconds.

For each language, we will explain how to write the model, compile the code (if necessary) and start it.

Use the Localsolver simulator ( LSP ) to solve the first model

In this section, we will show you how to model and solve your first problem: the optimization of the barrel. With a limited material surface ( S=π ), we tried to build a bucket with the largest capacity.

This small example is described more precisely in our example guide . Our main goal here is to learn how to write and start a mathematical optimization model.

Construct a mathematical model (LSP code)

The following is a LocalSolver program ( LSP ) that models this nonlinear problem ( see examples/optimal_bucket examples in the installation directory ).

/********** optimal_bucket.lsp **********/
use io;
/* Declares the optimization model. */
function model() {
    
    
    PI = 3.14159265359;
    // Numerical decisions
    R <- float(0, 1);
    r <- float(0, 1);
    h <- float(0, 1);
    // Surface must not exceed the surface of the plain disc
    surface <- PI*pow(r, 2) + PI*(R + r)*sqrt(pow(R - r, 2) + pow(h, 2));
    constraint surface <= PI;
    // Maximize the volume
    volume <- PI*h/3*(pow(R, 2) + R*r + pow(r, 2));
    maximize volume;
}
/* Parameterizes the solver. */
function param() {
    
    
    if (lsTimeLimit == nil) lsTimeLimit = 2;
}
/* Writes the solution in a file with the following format:
 *  - surface and volume of the bucket
 *  - values of R, r and h */
function output() {
    
    
    if (solFileName == nil) return;
    local solFile = io.openWrite(solFileName);
    solFile.println(surface.value, "  ", volume.value);
    solFile.println(R.value, "  ", r.value, "  ", h.value);
}

All variables of the model (called expressions) are declared with the left arrow <- ). Use the built-in function float () (or bool (), int (), set (), list ()) to introduce decision variables. Intermediate expressions can use other operators or functions to build on these decision variables. For example, in the above model: power ( pow ), square root ( sqrt ), less than or equal to ( <= ). There are many other mathematical operators available that allow you to model and solve highly nonlinear combinatorial optimization problems. The keywords constrain or maximize are used to mark the expression as constrained or maximized. If you need help or obtain temporary permission, please contact the staff of Wuxi Xunhe Information Technology Co., Ltd., the general agent of localsolver. (To be continued~~~~)

Guess you like

Origin blog.csdn.net/qq_31243247/article/details/114980336