" OR" -constraint with simple-score calculator并不起作用

时间:2014-01-05 18:51:58

标签: optaplanner

我正在尝试使用optaplanner构建一个简单的计分器,但是 我的“OR”-Constraint无法正常工作。 Optaplanner表示得分为-1,但得分必须为0。“ 解决方案应该是:“A”只能在索引2处使用。

方法“no_A_at_Index3”效果很好。 “A_at_Index2_or_Index3”似乎是错误的。 有谁知道我做错了什么?

P.S。我将方法从“no_A_at_Index2”更改为“no_A_at_Index3”。

很奇怪:

  • (!(this.A_at_Index2_or_Index3(nCells))||!(this.no_A_at_Index2(nCells)))效果很好。
  • (!(this.A_at_Index2_or_Index3(nCells))||!(this.no_A_at_Index3(nCells)))不起作用。

    @Override
    public SimpleScore calculateScore(NCells nCells) {
       int score = 0;
       if (!(this.A_at_Index2_or_Index3(nCells))){
          score--;
       }
    
       if (!(this.no_A_at_Index3(nCells))){
          score--;
       }
       return SimpleScore.valueOf(score);
    }
    
    public boolean A_at_Index2_or_Index3(NCells nCells){
        List<Cell> cellList = nCells.getCellList();
        ChomskyRule ruleAtIndex2 = cellList.get(2).getRule();
        ChomskyRule ruleAtIndex3 = cellList.get(3).getRule();   
        int a_counter = 0;
        if ( ruleAtIndex2 != null && ruleAtIndex2.getLeftSide().equals("A")){
                a_counter++;
        }
        if ( ruleAtIndex3 != null && ruleAtIndex3.getLeftSide().equals("A")){
                a_counter++;
        }
    
        if (a_counter==0 && ruleAtIndex2!=null && ruleAtIndex3!=null){ 
            return false;
        }
        return true;
    }
    
    public boolean no_A_at_Index3(NCells nCells){ 
       List<Cell> cellList = nCells.getCellList(); 
       ChomskyRule ruleAtIndex3 = cellList.get(3).getRule(); 
       if(ruleAtIndex3!=null && ruleAtIndex3.getLeftSide().equals("A")){return false;} 
       return true; 
     }
    

1 个答案:

答案 0 :(得分:0)

我想你想要这个:

int score = 0;
if (!(this.A_at_Index2_or_Index3(nCells)) || !(this.no_A_at_Index2(nCells)))
   score--;
}
return SimpleScore.valueOf(score);
相关问题