最新google ortools 在springboot+maven中的使用

一、起因

大家好! 这次想做一个排班的功能,于是找到了 google-ortools 。然后找了很多教程,就是启动不
了,大多都说有.dll文件的原因。

二、经过

但我发现maven引入后,这个文件已经内置了,而且内部还包含了jna图中为jna依赖
因而我相信不用单独调用了,内部肯定是有的,但是去查了官方文档也没看到怎么加载dll,但转念想到gitee上面肯定有大佬用过,所以就去查了,功夫不负有心人,终于找到了
引入依赖
此处奉上前辈的代码链接,以示敬意!链接: orService
注:其实官方文档的示例中也有,写下这篇博客时,没有完整阅读官方文档,因为可能需要翻墙。

三、结果

这里展示我写的单元测试整个过程:

1.引入依赖

引入maven依赖,version是当前最新的
依赖导入

2.编写单元测试

编写单元测试,内容来自网络,具体执行的内容在写这篇文章时仍不清楚,但好在运行起来了

 	//其他教程都是这样导入,貌似是以前的做法
 	//    static {
	//        System.out.println(System.getProperty("java.library.path"));
	//        System.loadLibrary("jniortools");
	//    }
	
 	@Test
    void contextLoads() {
    	//加载dll
        Loader.loadNativeLibraries();
        MPSolver solver = new MPSolver(
                "LinearProgrammingExample", MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
        double infinity = java.lang.Double.POSITIVE_INFINITY;
        // x and y are continuous non-negative variables.
        MPVariable x = solver.makeNumVar(0.0, infinity, "x");
        MPVariable y = solver.makeNumVar(0.0, infinity, "y");
        System.out.println("Number of variables = " + solver.numVariables());

        // x + 2*y <= 14.
        MPConstraint c0 = solver.makeConstraint(-infinity, 14.0, "c0");
        c0.setCoefficient(x, 1);
        c0.setCoefficient(y, 2);

        // 3*x - y >= 0.
        MPConstraint c1 = solver.makeConstraint(0.0, infinity, "c1");
        c1.setCoefficient(x, 3);
        c1.setCoefficient(y, -1);

        // x - y <= 2.
        MPConstraint c2 = solver.makeConstraint(-infinity, 2.0, "c2");
        c2.setCoefficient(x, 1);
        c2.setCoefficient(y, -1);
        System.out.println("Number of constraints = " + solver.numConstraints());

        // Maximize 3 * x + 4 * y.
        MPObjective objective = solver.objective();
        objective.setCoefficient(x, 3);
        objective.setCoefficient(y, 4);
        objective.setMaximization();

        final MPSolver.ResultStatus resultStatus = solver.solve();
        // Check that the problem has an optimal solution.
        if (resultStatus != MPSolver.ResultStatus.OPTIMAL) {
            System.err.println("The problem does not have an optimal solution!");
            return;
        }

        // The value of each variable in the solution.
        System.out.println("Solution");
        System.out.println("x = " + x.solutionValue());
        System.out.println("y = " + y.solutionValue());

        // The objective value of the solution.
        System.out.println("Optimal objective value = " + solver.objective().value());
    }

运行结果展示:
运行结果
参考过的资料网址:
OR-Tools官网: About OR-Tools
官网资源下载:Installing OR-Tools Java from Source on Windows
参考博客: Java使用google-ortools库的方法
参考博客: ortools在Java+maven项目中引入
参考博客: IDEA用JNA调用C++编写的dll动态链接库完整流程
参考博客: Intellij 中的 Google OR-Tools:UnsatisfiedLinkError

注:
1.上述链接有的需要翻墙才能查到
2.引用了前辈的内容,如有问题,还请见谅,告知本人后会及时更正
3.上述表述如有问题,还请斧正

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐