【问题标题】:Java library to find parametric solutions to a linear algebra system with singular matrix用于查找具有奇异矩阵的线性代数系统的参数解决方案的 Java 库
【发布时间】:2015-01-23 15:38:23
【问题描述】:

我想用Java解决这个整数线性代数问题,其中xi,yi,zi是整数变量(都是正数,xi≥0,yi≥0,zi≥0), 而a, b, c, d, e, f, g, h是常数(正整数≥0,例如a=20, b=12, c=28, d=24, e=19, f=5 , g=6, h=6)。

x1+x2+x3+x4+x5 = a
y1+y2+y3+y4+y5 = b
z1+z2+z3+z4+z5 = c
x1+y1+z1 = d
x2+y2+z2 = e
x3+y3+z3 = f
x4+y4+z4 = g
x5+y5+z5 = h


It could be viewed as:
 - sum constraint on the rows
 - sum constraint on the columns

 | x1 | x2 | x3 | x4 | x5 | → sum equal to a
 | y1 | y2 | y3 | y4 | y5 | → sum equal to b
 | z1 | z2 | z3 | z4 | z5 | → sum equal to c
   ↓    ↓    ↓    ↓    ↓
   d    e    f    g    h

显然,这个问题有很多整数解(但不是无限的)。如果可能的话,我想轻松地收集一堆这些整数解决方案,并从集合中随机弹出一个。

提前致谢!


已解决(找到一个解决方案):

感谢阿佩特!我使用ojalgo 找到了解决此问题的方法。 这是我的代码:

import org.ojalgo.OjAlgoUtils;
import org.ojalgo.netio.BasicLogger;
import org.ojalgo.optimisation.Expression;
import org.ojalgo.optimisation.ExpressionsBasedModel;
import org.ojalgo.optimisation.Optimisation;
import org.ojalgo.optimisation.Variable;

/**
 * @author madx
 */
public abstract class ojAlgoTest {

    static int[] row_constraints = new int[]{20,12,28};
    static int[] col_constraints = new int[]{24,19,5,6,6};

    public static void main(final String[] args) {

        BasicLogger.debug();
        BasicLogger.debug(ojAlgoTest.class.getSimpleName());
        BasicLogger.debug(OjAlgoUtils.getTitle());
        BasicLogger.debug(OjAlgoUtils.getDate());
        BasicLogger.debug();

        int rows = row_constraints.length;
        int cols = col_constraints.length;

        // Create variables expressing servings of each of the considered variable
        // Set lower and upper limits on the number of servings as well as the weight (cost of a
        // serving) for each variable.
        final Variable matrix[][] = new Variable[rows][cols];
        for(int i=0; i<rows;i++){
            for(int j=0;j<cols;j++){
                matrix[i][j] = Variable.make("Matrix" + i + "_" + j).lower(0).upper(24).weight(1);
            }
        }

        // Create a model and add the variables to it.
        final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel();
        for(int i=0; i<rows;i++){
            for(int j=0;j<cols;j++){
                tmpModel.addVariable(matrix[i][j]);
            }
        }

        // Create contraints

        for(int i=0; i<cols;i++){
            final Expression cat = tmpModel.addExpression("Col_Constraint_"+i).lower(col_constraints[i]).upper(col_constraints[i]);
            for(int j=0; j<rows;j++){
                cat.setLinearFactor(matrix[j][i], 1);
            }
        }

        for(int j=0; j<rows;j++){
            final Expression cat = tmpModel.addExpression("Row_Constraint_"+j).lower(row_constraints[j]).upper(row_constraints[j]);
            for(int i=0; i<cols;i++){
                cat.setLinearFactor(matrix[j][i], 1);
            }
        }

        // Solve the problem - minimise the cost
        Optimisation.Result tmpResult = tmpModel.minimise();

        // Print the result
        BasicLogger.debug();
        BasicLogger.debug(tmpResult);
        BasicLogger.debug();

        // Modify the model to require an integer valued solution.
        BasicLogger.debug("Adding integer constraints...");
        for(int i=0; i<rows;i++){
            for(int j=0;j<cols;j++){
                matrix[i][j].integer(true);
            }
        }

        // Solve again
        tmpResult = tmpModel.minimise();

        // Print the result, and the model
        BasicLogger.debug();
        BasicLogger.debug(tmpResult);
        BasicLogger.debug();
        BasicLogger.debug(tmpModel);
        BasicLogger.debug();
    }
}

【问题讨论】:

  • 到目前为止你做了什么? SO 用于回答特定问题,发布您迄今为止尝试过的内容。这似乎是家庭作业。
  • 如果是家庭作业完全没问题,但到目前为止我们需要看看你的工作。
  • 当然不是功课,我正在寻找一个可以帮助我解决这个问题的库,我不想重新发明轮子......但是我只是尝试使用commons.apache.org/proper/commons-math/userguide/linear.html,但我认为它对奇异矩阵没有帮助......
  • 也许我可以在真实空间中解决这个问题,找到超平面参数解决方案,然后通过寻找一些整数解决方案,但我认为存在更简单的方法。而且,如果我能做到这一点,我不知道/找不到任何 Java 库来做这件事,我希望在你的库知识中
  • 我不了解 Java 库,但是像 Matlab 这样的程序可以求解方程组。我也知道有一些算法可以在 Java 中应用,比如高斯消元法

标签: java linear-algebra solution solver singular


【解决方案1】:

这不能被表述为网络流问题吗? (从列约束到行约束的流程,带有额外的行/列来处理差异)如果这是真的,那么网络单纯形算法可用于产生整数解(所有问题参数必须是整数)。

如果我错了,它肯定可以表述为一般的 MIP 问题。有许多免费和商业软件包可以帮助解决这些问题。 ojAlgo 是一种开源纯 Java 替代方案(我编写的)。

Here's an example 关于如何使用 ojAlgo 对优化问题进行建模。

【讨论】:

  • 非常感谢!它完美地工作!我在上面的问题中粘贴了我的代码!我用你的 ojalgo lib 达到了这个目标!还有一个问题。我很容易设法获得这个问题的单一解决方案(它总是返回具有相同约束的相同解决方案)......但我的目标是随机找到一个尊重约束的解决方案......从这个意义上说,所有解决方案将具有相同的 target_cost,并且使用优化可能超出了需要。但是,有没有办法获得不止一个解决方案?还是仅以最低成本返回第一个?
  • 随机化权重以获得随机解似乎有点过头了,也许该工具提供了一些其他方法......
  • ojAlgo 无法为您提供 MIP 的所有(或多个)解决方案。您唯一能做的就是改变可变权重(不要重建整个模型,只需更新权重)并再次求解。使用不同的权重进行求解,但以已知整数解作为起点应该比从头开始求解新模型要快得多。
猜你喜欢
  • 2019-10-21
  • 2016-01-30
  • 1970-01-01
  • 2011-05-18
  • 2018-01-17
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
  • 2015-04-16
相关资源
最近更新 更多