我使用`clp-java进行线性优化,但当我尝试运行代码时,我会得到以下错误:该过程一直在运行,以下内容不断重复打印。
BridJ:LoadLibrary在加载C:\Users\Abhijay\AppData\Local\Temp\CLPExtractedLib1623275631902676\Clp.dll时出错:动态链接库(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);
}
}
dev分支的最新构建解决了这个问题。它很快就会被推到maven。
我实现了自己版本的线性编程,也支持混合整数线性编程。如果你有兴趣使用该工具,这里有链接。
问题:
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();