Dear all,I am trying to solve a basic linear system, but I am getting the wrong constraints when I run the code below.
The system (with solution [1, 2, 3]) is:
| 2 1 1 | | A | | 7 |
| 1 1 4 | * | B | = | 15 |
| 0 3 2 | | C | | 12 |
The constraints I am getting are:
0 <= 2*A + A + A - 7 <= 0
0 <= B + B + 4*B - 15 <= 0
0 <= 0*C + 3*C + 2*C - 12 <= 0
Instead of:
0 <= 2*A + B + C - 7 <= 0
0 <= A + B + 4*C - 15 <= 0
0 <= 0*A + 3*B + 2*C - 12 <= 0
The code I am using to set up the solver is presented below:
using (var scope = new ModelScope())
{
var model = new OPTANO.Modeling.Optimization.Model();
double[,] mtxA = new double[,] {
{ 2, 1, 1 },
{ 1, 1, 4 },
{ 0, 3, 2 },
};
var indices = Enumerable.Range(0, 3).ToList();
double[] vecB = new double[] { 7, 15, 12 };
// Variável de decisão
var vecX = new VariableCollection<int>(
model,
indices,
"x",
(x) => $"var_{x}",
null, // use default lower bound.
null, // use default upper bound.
(x) => VariableType.Continuous
);
// Constraints
foreach (var row in indices)
{
model.AddConstraint(Expression.Sum(indices.Select(col => mtxA[row, col] * vecX[row])) == vecB[row]);
}
// Objetivo
var objective = new Objective(Expression.Sum(indices.Select(col => vecX[col])), "meta", ObjectiveSense.Minimize);
model.AddObjective(objective);
using (var solver = new OPTANO.Modeling.GLPK.GLPKSolver())
{
var solution = solver.Solve(model);
foreach (var entry in solution.ObjectiveValues)
{
string straux = string.Format("Objetivo: {0}, Valor {1}", entry.Key, entry.Value);
MessageBox.Show(straux);
}
}
}
}
Could you please shed some light on this?
Thank you for your attention,
Marcelo.