• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java LinearConstraint类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.apache.commons.math3.optim.linear.LinearConstraint的典型用法代码示例。如果您正苦于以下问题:Java LinearConstraint类的具体用法?Java LinearConstraint怎么用?Java LinearConstraint使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



LinearConstraint类属于org.apache.commons.math3.optim.linear包,在下文中一共展示了LinearConstraint类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: solve

import org.apache.commons.math3.optim.linear.LinearConstraint; //导入依赖的package包/类
@Override
public void solve(double[] dir)
{
    /* Check the dimension of the vectorspace where lives dir */
    if (dir.length != d)
        throw new LinearProgrammingSolverException(dddirMessage);
    /* Update the constraints set of the simplex solver if modification */
    if (lcListModified)
    {
        List <LinearConstraint> lcList = new ArrayList <LinearConstraint>();
        int n = dirList.size();
        for (int i = 0 ; i < n ; i++) lcList.add(new LinearConstraint(dirList.get(i), Relationship.LEQ, valList.get(i)));
        lcSet = new LinearConstraintSet(lcList);
    }
    /* Evaluation */
    PointValuePair res = solver.optimize(new LinearObjectiveFunction(dir, 0), lcSet, GoalType.MAXIMIZE);
    /* Update the results and the flags */
    point = res.getPoint ();
    value = res.getSecond ();
    evaluated = true;
    lcListModified = false;
}
 
开发者ID:viryfrederic,项目名称:3plib,代码行数:23,代码来源:ACMLinearProgrammingSolver.java


示例2: getOptimalFeasibleSolution

import org.apache.commons.math3.optim.linear.LinearConstraint; //导入依赖的package包/类
private double[] getOptimalFeasibleSolution(double[] histA, 
		double[] histB, double[] bins, int dimension) {
	int num = bins.length / dimension;
	Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
	double[] coefficients = new double[num * 2];
	for (int i = 0; i < num; i++) {
		for (int j = 0; j < num; j++) {
			coefficients = setIJ(coefficients, i, j);
			double dist = DistanceUtil.getGroundDist(DistanceUtil.getBin(bins, dimension, i),
					DistanceUtil.getBin(bins, dimension, j), DistanceType.LTWO, null);
			constraints.add(new LinearConstraint(coefficients, Relationship.LEQ, dist));
		}
	}
	
	for (int i = 0; i < num; i++) {
		coefficients[i] = histA[i];
		coefficients[i + num] = histB[i];
	}
	
	LinearObjectiveFunction objective = new LinearObjectiveFunction(coefficients, 0);
	LinearConstraintSet consSet = new LinearConstraintSet(constraints);
	NonNegativeConstraint nonNeg = new NonNegativeConstraint(false);
	optimal = solver.optimize(objective, consSet, nonNeg, GoalType.MAXIMIZE, maxIter);
	return optimal.getPoint();
}
 
开发者ID:jinhuang,项目名称:melody-join,代码行数:26,代码来源:DualBound.java


示例3: calculatePrimal

import org.apache.commons.math3.optim.linear.LinearConstraint; //导入依赖的package包/类
public PointValuePair calculatePrimal() {
    SimplexSolver solver = new SimplexSolver();
    //All variables are nonnegative
    NonNegativeConstraint nonneg = new NonNegativeConstraint(true);
    //objective function is of the form MAX w+ - w- (+0x1,etc)
    LinearObjectiveFunction objective = new LinearObjectiveFunction(new double[] {1.0, -1.0, 0.0,0.0,0.0}, 0.0);
    ArrayList<LinearConstraint> constraints = new ArrayList<>();
    //probabilities must sum to 1.0
    double[] probabilityConstraint = new double[values.length + 2];
    probabilityConstraint[0] = probabilityConstraint[1] = 0.0;
    for(int i=2; i<probabilityConstraint.length; i++)
        probabilityConstraint[i]=1.0;
    constraints.add(new LinearConstraint(probabilityConstraint, Relationship.EQ, 1.0));
    //input game parameters
    //constraints supports two-sided equation
    double[] wConstraint = new double[values.length + 2];
    wConstraint[0] = 1.0;
    wConstraint[1] = -1.0;
    for(int i=0; i<values.length; i++) {
        double[] constr = new double[values.length+2];
        for(int j=2; j<constr.length; j++)
            constr[j]=values[i][j-2];
        constraints.add(new LinearConstraint(wConstraint, 0.0, Relationship.LEQ, constr, 0.0));
    }
    LinearConstraintSet constraintSet = new LinearConstraintSet(constraints);
    PointValuePair optimal = solver.optimize(objective, nonneg, constraintSet, GoalType.MAXIMIZE);
    return optimal;
}
 
开发者ID:matthewjwolff,项目名称:MatrixGameTool,代码行数:29,代码来源:PayoffMatrix.java


示例4: calculateDual

import org.apache.commons.math3.optim.linear.LinearConstraint; //导入依赖的package包/类
public PointValuePair calculateDual() {
        SimplexSolver solver = new SimplexSolver();
        //All variables are nonnegative
        NonNegativeConstraint nonneg = new NonNegativeConstraint(true);
        //objective function is of the form MAX w+ - w- (+0x1,etc)
        LinearObjectiveFunction objective = new LinearObjectiveFunction(new double[] {1.0, -1.0, 0.0,0.0,0.0}, 0.0);
        ArrayList<LinearConstraint> constraints = new ArrayList<>();
        //probabilities must sum to 1.0
        double[] probabilityConstraint = new double[values.length + 2];
        probabilityConstraint[0] = probabilityConstraint[1] = 0.0;
        for(int i=2; i<probabilityConstraint.length; i++)
            probabilityConstraint[i]=1.0;
        constraints.add(new LinearConstraint(probabilityConstraint, Relationship.EQ, 1.0));
        //input game parameters
        //constraints supports two-sided equation
        double[] wConstraint = new double[values.length +2];
        wConstraint[0] = 1.0;
        wConstraint[1] = -1.0;
        for(int i=0; i<values.length; i++) {
            double[] constr = new double[values.length+2];
            for(int j=2; j<constr.length; j++)
                constr[j]=values[j-2][i];
            constraints.add(new LinearConstraint(wConstraint, 0.0, Relationship.GEQ, constr, 0.0));
        }
//        constraints.add(new LinearConstraint(new double[] {1.0,-1.0,0.0,0.0,0.0}, 0.0, Relationship.GEQ, new double[] {0.0,0.0,values[0][0],values[1][0],values[2][0]}, 0.0));
//        constraints.add(new LinearConstraint(new double[] {1.0,-1.0,0.0,0.0,0.0}, 0.0, Relationship.GEQ, new double[] {0.0,0.0,values[0][1],values[1][1],values[2][1]}, 0.0));
//        constraints.add(new LinearConstraint(new double[] {1.0,-1.0,0.0,0.0,0.0}, 0.0, Relationship.GEQ, new double[] {0.0,0.0,values[0][2],values[1][2],values[2][2]}, 0.0));
        LinearConstraintSet constraintSet = new LinearConstraintSet(constraints);
        PointValuePair optimal = solver.optimize(objective, nonneg, constraintSet, GoalType.MINIMIZE);
        return optimal;
    }
 
开发者ID:matthewjwolff,项目名称:MatrixGameTool,代码行数:32,代码来源:PayoffMatrix.java


示例5: getConstraints

import org.apache.commons.math3.optim.linear.LinearConstraint; //导入依赖的package包/类
private Collection<LinearConstraint> getConstraints()
{
    return patternKind.getImageKinds()
            .parallelStream()
            .map(ik ->
            {
                double[] pattern = new double[patterns.length];
                for(int i = 0; i < pattern.length; i++)
                    pattern[i] = patterns[i].getImageNumber(ik);
                return new LinearConstraint(pattern, Relationship.GEQ, ik.getDemand());
            })
            .collect(Collectors.toList());
}
 
开发者ID:Polytech-AdrienCastex,项目名称:2D-Cutting-Stock-Problem-with-Setup-Cost,代码行数:14,代码来源:Solution.java


示例6: simplexDouble

import org.apache.commons.math3.optim.linear.LinearConstraint; //导入依赖的package包/类
/**
     * 
     * @param A ресурсы на выпуск всех товаров (юнитов) [номер ресурса][номер юнита]
     * @param b предел ресурсов (ограничение)
     * @param c выгода
     * @param x out кол-во выпуска (план выпуска, ответ)
     * @return суммарная выгода плана x
     */
public static double simplexDouble(double[][] A, double[] b, double[] c, double[] x) {
      
//    return justOneMaximize(A,b,c, x); // maybe fast, but not precission
    
    // - - -
    LinearObjectiveFunction f = new LinearObjectiveFunction(c, 0); // !!!

    ArrayList<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    for(int i=0; i<A.length; i++) { // Ограничения
        constraints.add( new LinearConstraint(A[i], Relationship.LEQ, b[i]) ); // _c[i]*x[i]<=V_Res[i]
        double[] zeroV = new double[A[i].length];
        Arrays.fill(zeroV, 0);
        constraints.add( new LinearConstraint(zeroV, Relationship.GEQ, 0) ); // _c[i]*x[i]<=V_Res[i]
    }
    // TODO при переработке энергии в металл можно учесть с помощью двух переменных и ограничения!!!

    // FIXME precission - do not use between 0.00001 and 1.0, only 1,2,3...
    SimplexSolver solver = new SimplexSolver();
    PointValuePair optSolution = solver.optimize(new MaxIter(1000), f,
            new LinearConstraintSet(constraints),
            GoalType.MAXIMIZE,
            new NonNegativeConstraint(true));

    //double[] solution = optSolution.getPoint();
    double[] x_=optSolution.getPointRef(); // TODO it is bad fix for n*1.0. To do iteration.
    for (int i=0;i<x_.length;i++) if (x_[i]>0) x_[i]=Math.round(x_[i]-0.5);

    System.arraycopy(x_, 0, x, 0, x.length);
    return optSolution.getValue();
    // - - -    
}
 
开发者ID:playerO1,项目名称:FieldBOT,代码行数:40,代码来源:MathOptimizationMethods.java


示例7: getEmd

import org.apache.commons.math3.optim.linear.LinearConstraint; //导入依赖的package包/类
/**
 * Compute the EMD between two histograms, using Simplex method
 * @param histA the first histogram
 * @param histB the second histogram
 * @param dimension the dimension of bins
 * @param bins the bins of the histogram
 * @param distType the ground distance type 
 * @param costMatrix specific cost matrix used for ground distance, only valid if distType is ARBITRARY
 * @return the exact EMD
 */
public static double getEmd(double[] histA, double[] histB, int dimension, double[] bins, DistanceType distType, HashMap<String, Double> costMatrix) {
	if (HistUtil.sum(histA) != HistUtil.sum(histB)) {
		//System.out.println("Infeasible: " + FormatUtil.toString(histA) + " - " + FormatUtil.toString(histB));
		histA = HistUtil.normalizeArray(histA);
		histB = HistUtil.normalizeArray(histB);
	}
	int numBins = bins.length / dimension;
	double[] coefficients = new double[numBins*numBins];
	if (numBins != histA.length || histA.length != histB.length) {
		return -1.0;
	}
	Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
	for (int i = 0; i < numBins; i++) {
		for (int j = 0; j < numBins; j++) {
			coefficients[i*numBins + j] = getGroundDist(getBin(bins, dimension, i), getBin(bins, dimension, j), distType, costMatrix);
		}
	}
	for (int i = 0; i < numBins; i++) {
		double[] flowFrom = getFlow(numBins, i, true);
		double[] flowTo = getFlow(numBins, i, false);
		constraints.add(new LinearConstraint(flowFrom, Relationship.LEQ, histA[i]));
		constraints.add(new LinearConstraint(flowTo, Relationship.LEQ, histB[i]));
		if (histA[i] - histB[i] > 0) {
			constraints.add(new LinearConstraint(subtract(flowFrom, flowTo), Relationship.EQ, histA[i] - histB[i]));
		}
		else {
			constraints.add(new LinearConstraint(subtract(flowTo, flowFrom), Relationship.EQ, histB[i] - histA[i]));
		}
	}
	LinearObjectiveFunction minF = new LinearObjectiveFunction(coefficients, 0);
	LinearConstraintSet constraintSet = new LinearConstraintSet(constraints);
	NonNegativeConstraint nonNegConstraint = new NonNegativeConstraint(true);
	solution = solver.optimize(minF, constraintSet, nonNegConstraint, GoalType.MINIMIZE, maxIter);
	return solution.getValue();
}
 
开发者ID:jinhuang,项目名称:melody-join,代码行数:46,代码来源:DistanceUtil.java


示例8: getIndMinEmd

import org.apache.commons.math3.optim.linear.LinearConstraint; //导入依赖的package包/类
public static double getIndMinEmd(double[] histA, double[] histB, int dimension, double[] bins, DistanceType distType, HashMap<String, Double> costMatrix) {
		if (HistUtil.sum(histA) != HistUtil.sum(histB)) {
			//System.out.println("Infeasible: " + FormatUtil.toString(histA) + " - " + FormatUtil.toString(histB));
			histA = HistUtil.normalizeArray(histA);
			histB = HistUtil.normalizeArray(histB);
		}
		int numBins = bins.length / dimension;
		double[] coefficients = new double[numBins*numBins];
		if (numBins != histA.length || histA.length != histB.length) {
			return -1.0;
		}
		Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
		for (int i = 0; i < numBins; i++) {
			for (int j = 0; j < numBins; j++) {
				coefficients[i*numBins + j] = getGroundDist(getBin(bins, dimension, i), getBin(bins, dimension, j), distType, costMatrix);
			}
		}
		for (int i = 0; i < numBins; i++) {
			double[] flowFrom = getFlow(numBins, i, true);
			double[] flowTo = getFlow(numBins, i, false);
			constraints.add(new LinearConstraint(flowFrom, Relationship.LEQ, histA[i]));
//			constraints.add(new LinearConstraint(flowTo, Relationship.LEQ, histB[i]));
			if (histA[i] - histB[i] > 0) {
				constraints.add(new LinearConstraint(subtract(flowFrom, flowTo), Relationship.EQ, histA[i] - histB[i]));
			}
//			else {
//				constraints.add(new LinearConstraint(subtract(flowTo, flowFrom), Relationship.LEQ, histB[i] - histA[i]));
//			}
		}
		LinearObjectiveFunction minF = new LinearObjectiveFunction(coefficients, 0);
		LinearConstraintSet constraintSet = new LinearConstraintSet(constraints);
		NonNegativeConstraint nonNegConstraint = new NonNegativeConstraint(true);
		solution = solver.optimize(minF, constraintSet, nonNegConstraint, GoalType.MINIMIZE, maxIter);
		return solution.getValue();
	}
 
开发者ID:jinhuang,项目名称:melody-join,代码行数:36,代码来源:DistanceUtil.java



注:本文中的org.apache.commons.math3.optim.linear.LinearConstraint类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java HSnapshotDescription类代码示例发布时间:2022-05-23
下一篇:
Java SubscriptionStateHeader类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap