1

我正在使用 `clp-java 进行线性优化,但是当我尝试运行代码时,出现以下错误:进程一直在运行,并且以下内容不断重复打印。

BridJ:加载 C:\Users\Abhijay\AppData\Local\Temp\CLPExtractedLib1623275631902676\Clp.dll 时发生 LoadLibrary 错误:动态链接库 (DLL) 初始化例程失败。

我在 pom.xml 中添加了以下依赖项。

<dependency>
    <groupId>com.quantego</groupId>
    <artifactId>clp-java</artifactId>
    <version>1.16.10</version>
</dependency>

<dependency>
    <groupId>com.nativelibs4java</groupId>
    <artifactId>bridj</artifactId>
    <version>0.7.0</version>
</dependency>

这是我的代码:

import com.quantego.clp.CLP;
import com.quantego.clp.CLP.ALGORITHM;
import com.quantego.clp.CLPVariable;

public class ClpDemo {

    public static void main(String[] args) {
        CLP model = new CLP().algorithm(ALGORITHM.AUTO).maximization().presolve(false);
        CLPVariable x1 = model.addVariable().lb(0);
        CLPVariable x2 = model.addVariable().lb(0);
        model.createExpression().add(10, x1).add(20, x2).leq(120);
        model.createExpression().add(8, x1).add(8, x2).leq(80);
        model.createExpression().add(12, x1).add(16, x2).asObjective();
        model.solve();
        double x1Value = model.getSolution(x1);
        double x2Value = model.getSolution(x2);
        System.out.println("x1 :" + x1Value + " x2 :" + x2Value);
      }
} 
4

2 回答 2

0

我实现了自己的线性规划版本,也支持混合整数线性规划。如果您有兴趣使用该工具,这里是链接

问题:

max: x+y
-2x + 2y >= 1
-8x + 10y <= 13
x,y : real

线性规划的例子。

    //Function
    double[] function = new double[] {1,1};

    //Constraints
    List<Constraint> constraints = new ArrayList<>();
    constraints.add(new Constraint(new double[] {-2,2}, Constraint.Symbol.GREATER_THAN, 1));
    constraints.add(new Constraint(new double[] {-8,10}, Constraint.Symbol.LESS_THAN, 13));

    LinearProgramming lp = new LinearProgramming(LinearProgramming.Objective.Maximize);

    int status = lp.Solve(function, constraints);
    switch(status){
        case LinearProgramming.INFEASIBLE:
            System.out.println("Infeasible solution");
        break;
        case LinearProgramming.OPTIMAL:
            System.out.println("Optimal solution found");
        case LinearProgramming.UNBOUNDED:
            System.out.println("Unbounded solution");
    }

    double[] coef = lp.getCoefficients();
    double solution = lp.getSolution();

问题:

max: x+y
-2x + 2y >= 1
-8x + 10y <= 13
x: real
y: integer

混合整数线性规划的示例。

    //Function
    double[] function = new double[] {1,1};

    //Constraints
    List<Constraint> constraints = new ArrayList<>();
    constraints.add(new Constraint(new double[] {-2,2}, Constraint.Symbol.GREATER_THAN, 1));
    constraints.add(new Constraint(new double[] {-8,10}, Constraint.Symbol.LESS_THAN, 13));

    //Define integer variables
    //1: integer
    //0: double
    int[] types = new int[] {0,1};

    MixedIntegerLinearProgramming lp = new MixedIntegerLinearProgramming(MixedIntegerLinearProgramming.Objective.Maximize);
    lp.setType(types);

    int status = lp.Solve(function, constraints);
    switch(status){
        case LinearProgramming.INFEASIBLE:
            System.out.println("Infeasible solution");
        break;
        case LinearProgramming.OPTIMAL:
            System.out.println("Optimal solution found");
        case LinearProgramming.UNBOUNDED:
            System.out.println("Unbounded solution");
    }

    double[] coef = lp.getCoefficients();
    double solution = lp.getSolution();
于 2018-12-20T16:53:28.670 回答
0

dev 分支的最新版本解决了这个问题。它很快就会被推送到maven。

于 2018-12-04T10:00:25.013 回答