java - Intellij 中的 Google OR 工具 : UnsatisfiedLinkError

标签 java or-tools intellij-13

我正在设置一个应使用 Google OR-Tools 的 java 框架。下面的代码编译成功,但在运行时抛出异常:

Exception in thread "main" java.lang.UnsatisfiedLinkError: com.google.ortools.linearsolver.operations_research_linear_solverJNI.MPSolver_CLP_LINEAR_PROGRAMMING_get()I
    at com.google.ortools.linearsolver.operations_research_linear_solverJNI.MPSolver_CLP_LINEAR_PROGRAMMING_get(Native Method)
    at com.google.ortools.linearsolver.MPSolver$OptimizationProblemType.<clinit>(MPSolver.java:221)
    at Main.main(Main.java:15)

我在 Windows 10 上使用 Intellij 2018.3。我花了很多时间尝试运行此程序,但没有成功。根据我在互联网上找到的信息,异常可能是由于链接不良和/或缺少 OR-Tools 所依赖的外部库引起的。但是,我没有解决这个问题的背景,而且 Intellij 也没有突出显示任何内容。知道问题出在哪里吗?

为了完成,这是我运行的代码:

import com.google.ortools.linearsolver.MPObjective;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;

public final class Main {

  public static void main(String[] args) {

    // Create the linear solver with the GLOP backend.
    MPSolver solver =
        new MPSolver("SimpleLpProgram", MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);

    // Create the variables x and y.
    MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
    MPVariable y = solver.makeNumVar(0.0, 2.0, "y");

    System.out.println("Number of variables = " + solver.numVariables());

    // Create a linear constraint, 0 <= x + y <= 2.
    MPConstraint ct = solver.makeConstraint(0.0, 2.0, "ct");
    ct.setCoefficient(x, 1);
    ct.setCoefficient(y, 1);

    System.out.println("Number of constraints = " + solver.numConstraints());

    // Create the objective function, 3 * x + y.
    MPObjective objective = solver.objective();
    objective.setCoefficient(x, 3);
    objective.setCoefficient(y, 1);
    objective.setMaximization();

    solver.solve();

    System.out.println("Solution:");
    System.out.println("Objective value = " + objective.value());
    System.out.println("x = " + x.solutionValue());
    System.out.println("y = " + y.solutionValue());
  }
}

最佳答案

就我而言,解决方案很简单 - 我只需要添加这一行代码:

Loader.loadNativeLibraries();

加载程序来自com.google.ortools.Loader

关于java - Intellij 中的 Google OR 工具 : UnsatisfiedLinkError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58819070/

相关文章:

java - java中Connection对象的使用

java - Intellij 文件未找到或已损坏

intellij-idea - Intellij:左右移动标签

当方程不可因式分解时,Java 仅返回 0

java - 如何删除 Hudson 的构建工件?

java - 防止正则表达式匹配斜线字符

python - 有没有办法使用 Google OR Tools for python 设置析取约束?

python - 将 Python OR Tools 路由解决方案保存到列表中

java - 使用 OR-Tools 表达多变量约束

spring - 如何在 intellij 中打开 spring bean 定义?