Local Solver快速入门指南(连载七)--使用C++求解您的第一个LocalSolver模型

使用C++求解您的第一个LocalSolver模型

LocalSolver是在C++语言中实现的。一个高度可移植的C++库提供了面向对象的应用程序编程接口(API),允许在业务应用程序中完全集成LoalSoverLocalSolverapi是轻量的,只需要操纵几个对象类。请注意,LocalSolver是一个“建模和运行”的数学编程求解器:实例化了模型之后,用户不需要编写额外的代码来运行解算器。

在这一章节中,我们将展示如何在C++语言中建模和解决第一个优化问题:桶形状的优化。在有限的材料表面(S=π)情况下,我们试图建造一个容量最大的桶。

这个小例子在我们的示例教程中有更精确的描述。在这里,我们的主要目标是学习如何编写和启动这个模型。如有更多问题可联系LOCAL SOLVER中国区代理商无锡迅合信息科技有限公司技术人员。

编写LocalSolver模型

下面是C++程序代码,该模型对上述这个非线性问题进行建模。

 (参考安装目录下examples/optimal_bucket实例).

//********* optimal_bucket.cpp *********
#include <iostream>
#include <fstream>
#include <vector>
#include "localsolver.h"
using namespace localsolver;
using namespace std;
class OptimalBucket {
   
   
public:
    // Solver. 
    LocalSolver localsolver;
    // LS Program variables.程序变量 
    LSExpression R;
    LSExpression r;
    LSExpression h;
    LSExpression surface;
    LSExpression volume;
    void solve(int limit) {
   
   
        lsdouble PI = 3.14159265359;
        // Declares the optimization model. 声名优化模型
        LSModel model = localsolver.getModel();
        // Numerical decisions数值决策变量
        R = model.floatVar(0.0, 1.0);
        r = model.floatVar(0.0, 1.0);
        h = model.floatVar(0.0, 1.0);
 
        // Surface must not exceed the surface of the plain disc表面积不得超过光碟表面
        surface = PI*model.pow(r, 2) + PI*(R + r)*model.sqrt(model.pow(R - r, 2) + model.pow(h, 2));
        model.constraint(model.leq(surface, PI));
 
        // Maximize the volume最大化容量
        volume = PI*h/3*(model.pow(R, 2) + R*r + model.pow(r, 2));
        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  R,rh的值
    void writeSolution(const string& fileName) {
   
   
        ofstream outfile;
        outfile.exceptions(ofstream::failbit | ofstream::badbit);
        outfile.open(fileName.c_str());
 
        outfile << surface.getDoubleValue() << " " << volume.getDoubleValue() << endl;
        outfile << R.getDoubleValue() << " " << r.getDoubleValue() << " " << h.getDoubleValue() << endl;
    }
};
int main(int argc, char** argv) {
   
   
    const char* solFile = argc > 1 ? argv[1] : NULL;
    const char* strTimeLimit = argc > 2 ? argv[2] : "2";    
 
    try {
   
   
        OptimalBucket model;
        model.solve(atoi(strTimeLimit));
        if (solFile != NULL) model.writeSolution(solFile);
        return 0;
    } catch (const exception& e) {
   
   
        cerr << "An error occurred:" << e.what() << endl;
        return 1;
    }
}

创建LocalSolver环境LocalSolver()后,模型的所有决策变量都用函数floatVar()声明(也可以是boolVar()、intVar()、setVar()、listVar())。中间表达式可以使用其他运算符或函数建立在这些决策变量的基础之上。例如,在上面的模型中:幂(pow)、平方根(sqrt)、小于或等于(leq)。还有许多其他的数学运算符是可用的,允许您建模和解决高度非线性的组合优化问题。函数constraintmaximize用于将表达式标记为constraint(约束)或maximized(最大化)。

猜你喜欢

转载自blog.csdn.net/qq_31243247/article/details/115062507
今日推荐