我正在使用joptimizer解决线性编程问题。
我的问题是:
Maximize (x1*f1 + x2*f2 + x3*f3)
such that (x1*v1 + x2*v2 + x3*v3) <= h
我需要找到x1,x2和x3。
我不知道如何根据上面的等式创建joptimizer输入。
答案 0 :(得分:3)
这里有Java文档 http://www.joptimizer.com/apidocs/index.html
简单的例子 最小化3x + 4y使得2x + 3y> = 8,5x + 2y> = 12,x> = 0,y> = 0
我的解决简单线性规划问题的示例代码如下:
package test_joptimizer;
import com.joptimizer.functions.ConvexMultivariateRealFunction;
import com.joptimizer.functions.LinearMultivariateRealFunction;
import com.joptimizer.optimizers.JOptimizer;
import com.joptimizer.optimizers.OptimizationRequest;
import org.apache.log4j.BasicConfigurator;
/**
* @author K.P.L.Kanchana
*/
public class Main {
public static void main(String[] args) throws Exception {
// Objective function (plane)
LinearMultivariateRealFunction objectiveFunction = new LinearMultivariateRealFunction(new double[] {3.0, 4.0}, 0); //minimize 3x+4y
//inequalities (polyhedral feasible set G.X<H )
ConvexMultivariateRealFunction[] inequalities = new ConvexMultivariateRealFunction[4];
// x >= 0
inequalities[0] = new LinearMultivariateRealFunction(new double[]{-1.0, 0.00}, 0.0); // focus: -x+0 <= 0
// y >= 0
inequalities[1] = new LinearMultivariateRealFunction(new double[]{0.0, -1.00}, 0.0); // focus: -y+0 <= 0
// 2x+3y >= 8
inequalities[2] = new LinearMultivariateRealFunction(new double[]{-2.0, -3.00}, 8.0); // focus: -2x-3y+8 <= 0
// 5x+2y >= 12
inequalities[3] = new LinearMultivariateRealFunction(new double[]{-5.0, -2.00}, 12.0);// focus: -5x-2y+12 <= 0
//optimization problem
OptimizationRequest or = new OptimizationRequest();
or.setF0(objectiveFunction);
or.setFi(inequalities);
//or.setInitialPoint(new double[] {0.0, 0.0});//initial feasible point, not mandatory
or.setToleranceFeas(1.E-9);
or.setTolerance(1.E-9);
//optimization
JOptimizer opt = new JOptimizer();
opt.setOptimizationRequest(or);
int returnCode = opt.optimize();
double[] sol = opt.getOptimizationResponse().getSolution();
System.out.println("Length: " + sol.length);
for (int i=0; i<sol.length/2; i++){
System.out.println( "X" + (i+1) + ": " + Math.round(sol[i]) + "\ty" + (i+1) + ": " + Math.round(sol[i+1]) );
}
}
}
答案 1 :(得分:-1)
也许您也希望看一下以下示例代码。
请勿忘记从http://www.joptimizer.com/downloadWithAdd.html导入家属。
您应手动将外部三个jar文件(/joptimizer-4.0.0.jar
,/joptimizer-4.0.0-dependencies.zip
,/joptimizer-4.0.0-sources.jar
)下载并导入到项目中。 .zip
文件也需要解压缩。
import java.util.Arrays;
import com.joptimizer.optimizers.*;
public class LCLP {
// min (x) for J = C'*X;
// s.t.
// A*X = b;
// X <= 0;
// X = [x1;x2;x3;...xN]
public static void main(String[] args) throws Exception {
// Example from http://www.joptimizer.com/linearProgramming.html
//Objective function
double[] c = new double[] { -1., -1. };
//Inequalities constraints
double[][] G = new double[][] {{4./3., -1}, {-1./2., 1.}, {-2., -1.}, {1./3., 1.}};
double[] h = new double[] {2., 1./2., 2., 1./2.};
//Bounds on variables
double[] lb = new double[] {0 , 0};
double[] ub = new double[] {10, 10};
//optimization problem
LPOptimizationRequest or = new LPOptimizationRequest();
or.setC(c);
or.setG(G);
or.setH(h);
or.setLb(lb);
or.setUb(ub);
or.setDumpProblem(true);
//optimization
LPPrimalDualMethod opt = new LPPrimalDualMethod();
opt.setLPOptimizationRequest(or);
opt.optimize();
double[] sol = opt.getOptimizationResponse().getSolution();
System.out.println("Solution = " + Arrays.toString(sol));
}
}