简体   繁体   English

Microsoft Solver Foundation中的决策矩阵

[英]Decision matrix in Microsoft Solver Foundation

I am trying to use the Microsoft Solver Foundation to optimize a problem with a matrix of binary decision variables. 我正在尝试使用Microsoft Solver Foundation来优化二进制决策变量矩阵的问题。 Here is the format of my decision matrix: 这是我的决策矩阵的格式:

X[i,j] = { { x11, x12, ..., x1n }, { x21, x22, ..., x2n }, ... { xm1, xm2, ..., xmn }, };

I also have a vector of parameters, which is dependent on Xij matrix (each element of the vector is sum of one column of Xij: 我还有一个参数向量,它取决于Xij矩阵(向量的每个元素都是Xij的一列之和:

Y[i] = { Sum(x11, x21, ..., xm1), Sum(x12, x22, ..., xm2), ..., Sum(x1n, x2n, ..., xmn) }

I know that i should work with indexed Decision objects, but I have trouble doing that. 我知道我应该使用索引的Decision对象,但是这样做很麻烦。 Could anyone please help me. 谁能帮我。 I understand there is two ways of indexing Decisions: 我了解索引索引有两种方法:

Decision Xij = new Decision(Domain.Any, "x", Some Set, Some other set);

and also there's: 还有:

`Decision[,] = new Decsion [i, j];`

What's the difference? 有什么不同?

I created 2D arrays for Xij as follows: 我为Xij创建了2D数组,如下所示:

static Decision[,] XijMatrix()
    {
        Decision[,] d = new Decision[int rows, int cols];
        for (int row = 0; row < rows; row++)
            for (int col = 0; col < cols; col++)
                d[row, col] = new Decision(Domain.Boolean, "X" + row + col);

        return d;
    }

and another array for Yj: 和另一个Yj数组:

static Decision[,] YjMatrix()
    {
        Decision[,] d = new Decision[1, int cols];
        for (int col = 0; col < cols; col++)
            d[0, col] = new Decision(Domain.Boolean, "Y" + col);
        return d;
    }

and to bound these two matrices together, I added a constraint to moedl: 为了将这两个矩阵绑定在一起,我在moedl中添加了一个约束:

for (int i = 0; i < Yj.GetLength(1); i++)
     {
        model.AddConstraint("C" + i, Yj[0, i] == matColSum(Xij, i));
     }

and the matColSum is used to add elements of a column (i): 而matColSum用于添加列(i)的元素:

static Term matColSum(Decision[,] Xij, int i)
    {
        Term r = Xij[0, i];
        for (int row = 1; row < Xij.GetLength(0); row++)
        {
            r += Xij[row, i];
        }
        return r;
    }

Now this creates an Xij matrix, which in every column has just one true value (1); 现在,这将创建一个Xij矩阵,该矩阵的每一列中只有一个真实值(1); It's like the matColSum considers Xij and Yj elements as integers! 就像matColSum将Xij和Yj元素视为整数一样! What am I doing wrong? 我究竟做错了什么? I don't understand. 我不明白

Yes, you can either use a C# array to define a non-scalar Decision variable or use indexed Decision objects as explained in Nathan Brixius' blog . 是的,您可以使用C#数组定义非标量Decision变量,也可以按照Nathan Brixius的博客中所述使用索引的Decision对象。

There might be better ways to do it, but I would define one Sum constraint for every element in your parameter vector Y[i] . 可能会有更好的方法,但是我将为参数向量Y[i]每个元素定义一个Sum约束。 The parameter vector or its elements can be defined as Decision variables, or you could declare them as Term objects, which might be more efficient. 可以将参数向量或其元素定义为Decision变量,也可以将它们声明为Term对象,这样可能会更有效。

Be aware that Microsoft seems no longer be working on the Solver Foundation . 请注意, Microsoft似乎不再致力于 Solver Foundation So, it might make sense to look for other solvers. 因此,寻找其他求解器可能很有意义。 My personal favorite is MiniZinc , but this certainly depends on the problem type to solve. 我个人最喜欢的是MiniZinc ,但这当然取决于要解决的问题类型。 Some people prefer Google OR-Tools . 有些人更喜欢Google OR-Tools

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM