简体   繁体   English

GEN_NO_GENERATABLE_NOTIF

[英]GEN_NO_GENERATABLE_NOTIF

I want to choose index from list, so the element[index] complies my condition.我想从列表中选择索引,所以 element[index] 符合我的条件。 MyList[index].num==0 MyList[索引].num==0

I tried the code bellow:我尝试了下面的代码:

gen DescIdx2Choose keeping {
    it < MyList.size();
    MyList[it].num==0;//I tried a few way of read_only
};

How can I do it without using all_indices?不使用 all_indices 怎么办? Thanks谢谢

Since you generating DescIdx2Choose then MyList will be input to the problem.由于您生成 DescIdx2Choose,因此 MyList 将被输入到问题中。 Therefore,所以,

If seeking for the first index (if exists) then using random generation isn't required.如果寻找第一个索引(如果存在),则不需要使用随机生成。 Use the procedural code "first_index" as user3467290 suggested which is much more efficient:使用 user3467290 建议的程序代码“first_index”,效率更高:

var fidx := MyList.first_index(.num == 0);
if ( fidx != UNDEF ) {
   DescIdx2Choose = fidx;
} else {
   // error handling
};

If there are multiple indices and it is required to choose a random one, the most efficient way would be using "all_indices" as Thorsten suggested:如果有多个索引并且需要选择一个随机索引,最有效的方法是使用 Thorsten 建议的“all_indices”:

gen DescIdx2Choose keeping {
  it in read_only( MyList.all_indices(.num == 0) );
};

The reason is the random generator doesn't need to read all possible values of "MyList.num" only a shorter list of valid indices.原因是随机生成器不需要读取“MyList.num”的所有可能值,只需要一个较短的有效索引列表。

This should do it, but MyList must fulfill the condition otherwise you get a contradiction.应该这样做,但 MyList 必须满足条件,否则您会遇到矛盾。 Pseudo-method first_index() is not bi-directional which is what we need here.伪方法 first_index() 不是双向的,这正是我们需要的。

gen DescIdx2Choose keeping {
  MyList.first_index(.num == 0) == it;
};

Maybe i missed something in the question, but If you always want the index of the first element that its num == 0, then why use constraints?也许我错过了问题中的某些内容,但是如果您总是想要第一个元素的索引 num == 0,那么为什么要使用约束呢? can assign DescIdx2Choose == MyList.first_index(.num == 0).可以分配 DescIdx2Choose == MyList.first_index(.num == 0)。 To ensure that there is at least one such element, can constrain MyList.has(.num == 0).为确保至少有一个这样的元素,可以约束 MyList.has(.num == 0)。 Do you have additional constraints on DescIdx2Choose?您对 DescIdx2Choose 有其他限制吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM