当并非像在 Excel 中那样满足所有约束时,如何强制 MS 求解器找到模型的解决方案?我在 LinearReport 对象中找到了一个 GetInfeasibilitySet 方法(显然它返回模型中不满足的约束),但我无法实例化该对象,因为它需要我无法实例化的 Isolver 和 LinearSolutionMappings 参数也是。
问问题
74 次
1 回答
0
我解决了这个问题。这是我的代码。
//constraints avoiding the optimal solution
List<int> Restricoes = new List<int>();
bool solucaoOtima = false;
//index = 1, because the first constraint is always in the model
int index = 1;
while (solucaoOtima == false)
{
var restricao = auxModel.Constraints.ToList()[index];
restricao.Enabled = false;
auxSolution = auxContext.Solve();
//that's the trick part, I assumed that if I find the optimal
//solution by removing the current index, then I assumed
//the problem was this index
if (auxSolution.Quality.ToString().Equals("Optimal"))
{
Restricoes.Add(index);
VoltarRestricoes(auxModel, Restricoes);
auxSolution = auxContext.Solve();
solucaoOtima = auxSolution.Quality.ToString().Equals("Optimal") ? true : false;
}
index = index == limiteIteracao ? 1 : index + 1;
}
/// <summary>
/// Reset all the constraints of the model, except those indexes that are in Restricoes list.
/// </summary>
/// <param name="auxModel"></param>
/// <param name="Restricoes"></param>
private void VoltarRestricoes(Model auxModel, List<int> Restricoes)
{
for (int i = 0; i < auxModel.Constraints.ToList().Count; i++)
{
var restricao = auxModel.Constraints.ToList()[i];
restricao.Enabled = Restricoes.Contains(i) ? false : true;
}
}
于 2016-08-31T18:15:35.420 回答