2016-08-31 2 views
0

Excel에서와 같이 모든 제약 조건이 만족스럽지 않을 때 MS 해답을 강제로 모델에 대한 솔루션을 찾을 수 있습니까? LinearReport 개체에있는 GetInfeasibilitySet 메서드를 발견했습니다 (모델에서 만족하지 않는 제약 조건을 반환 함). 그러나이 개체를 사용할 수 없었던 ISolver 및 LinearSolutionMappings 매개 변수가 필요하기 때문에이 개체를 인스턴스화 할 수 없습니다. 인스턴스뿐만 아니라.force 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; 
     } 
    }