2016-08-31 31 views
0

如何在不滿足所有約束條件的情況下強制MS求解器找到模型的解決方案? 我找到了一個方法GetInfeasibilitySet(顯然它返回了模型中不滿足的約束),但是我無法實例化這個對象,因爲它需要我無法使用的ISolver和LinearSolutionMappings參數實例也是如此。強制MS求解器找到解決方案

回答

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; 
     } 
    } 
相關問題