LocalSolver快速入门指南(连载十二)----使用Java语言求解您的第一个LocalSolver数学模型

使用Java语言求解您的第一个LocalSolver数学模型

LocalSolver求解器是在C++语言中实施的。尽管如此,还是为Java7.0(或更高版本)提供了面向对象的API,允许在Java业务应用程序中完全集成LocalSolver方案。LocalSolverAPI是轻量级的,只需要操作几个类。请注意,LocalSolver是一个“建模-运行”的数学编程求解器:实例化了模型之后,不需要编写额外的代码来运行解算器。

在本节中,我们将向您展示如何用Java语言建模和解决第一个优化问题:桶形的优化。在使用有限的材料表面(S=π), 我们试着造一个容量最大的桶。这个小例子在我们的示例教程中有更精确的描述。在这里,我们的主要目标是学习如何编写数学模型和启动模型。

编写数学模型

下面是为这个非线性问题建模的Java程序(参见示例 examples/optimal_bucket)。更多可咨询无锡迅合信息科技有限公司技术人员。

/********** OptimalBucket.java **********/
import java.io.*;
import localsolver.*;
public class OptimalBucket {
   
   
    private static final double PI = 3.14159265359;
 
    // Solver.求解器
    private final LocalSolver localsolver;
 
    // LS Program variables.程序变量
    private LSExpression R;
    private LSExpression r;
    private LSExpression h;
 
    private LSExpression surface;
    private LSExpression volume;
 
    private OptimalBucket(LocalSolver localsolver) {
   
   
        this.localsolver = localsolver;
    }
    private void solve(int limit) {
   
   
        // Declares the optimization model.声明优化模型
        LSModel model = localsolver.getModel();
        LSExpression piConst = model.createConstant(PI);
        // Numerical decisions数值决策变量
        R = model.floatVar(0, 1);
        r = model.floatVar(0, 1);
        h = model.floatVar(0, 1);
        // Surface related expressions与表面相关的表达式
        // s1 = PI*r^2
        LSExpression s1 = model.prod(piConst, r, r);
        // s2 = (R - r)^2
        LSExpression s2 = model.pow(model.sub(R, r), 2);
        // s3 = h^2
        LSExpression s3 = model.pow(h, 2);
        // s4 = sqrt((R-r)^2 + h^2)
        LSExpression s4 = model.sqrt(model.sum(s2, s3));
        // s5 = R+r */
        LSExpression s5 = model.sum(R, r);
        // s6 = PI*(R + r)*sqrt((R - r)^2 + h^2)
        LSExpression s6 = model.prod(piConst, s5, s4);
 
        // surface = PI*r^2 + PI*(R+r)*sqrt((R - r)^2 + h^2)
        surface = model.sum(s1, s6);
        // Surface must not exceed the surface of the plain disc表面不能超过碟片的面积
        model.addConstraint(model.leq(surface, PI));
        // Volume related expressions和体积相关的表达式
        // v1 = R^2
        LSExpression v1 = model.pow(R, 2);
        // v2 = R*r
        LSExpression v2 = model.prod(R, r);
        // v3 = r^2
        LSExpression v3 = model.pow(r, 2);
 
        // volume = PI*h/3*(R^2 + R*r + r^2)
        volume = model.prod(piConst, model.div(h, 3), model.sum(v1, v2, v3));
        // Maximize the volume最大化容量
        model.maximize(volume);
        model.close();
        // Parameterizes the solver.给解算器进行参数化
        localsolver.getParam().setTimeLimit(limit);
        localsolver.solve();
    }
    // Writes the solution in a file with the following format:
    // - surface and volume of the bucket
    // - values of R, r and h
    private void writeSolution(String fileName) throws IOException {
   
   
        try (PrintWriter output = new PrintWriter(fileName)) {
   
   
            output.println(surface.getDoubleValue() + " " + volume.getDoubleValue());
            output.println(R.getDoubleValue() + " " + r.getDoubleValue() + " " + h.getDoubleValue());
        }
    }
    public static void main(String[] args) {
   
   
 
        String outputFile = args.length > 0 ? args[0] : null;
        String strTimeLimit = args.length > 1 ? args[1] : "2";
 
         try (LocalSolver localsolver = new LocalSolver()) {
   
   
            OptimalBucket model = new OptimalBucket(localsolver);
            model.solve(Integer.parseInt(strTimeLimit));
            if (outputFile != null) {
   
   
                model.writeSolution(outputFile);
            }
        } catch(Exception ex) {
   
   
            System.err.println(ex);
            ex.printStackTrace();
            System.exit(1);
        }
    }
}

创建LocalSolver环境LocalSolver()后,模型的所有决策变量都用函数floatVar()声明(也可以是boolVar()、intVar()、setVar()、listVar()等)。中间表达式可以使用其他运算符或函数建立在这些决策变量的基础上。例如,在上面的模型中:幂(pow)、平方根(sqrt)、小于或等于(leq)。还有许多其他的数学运算符是可用的,允许您建模和解决高度非线性的组合优化问题。函数constraintmaximize用于将表达式标记为constraint(约束的)或maximized(最大化)。更多问题请咨询LOCALSOLVER中国区总代理无锡迅合信息科技有限公司技术人员(2021-2023)。  未完待续---2021/5/5

Guess you like

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