我正在开发高级 Petri 网编辑器/模拟器。首先,这里有一点词汇
圆圈=地方
矩形 =过渡
就地整数 =标记
过渡条件 =守卫
我坚持通过过渡的守卫。Guard 是一个条件,如果要执行转换,则必须为真。我知道我应该以某种方式使用回溯,但我不知道在程序开始之前进入转换的位置数量,所以我不能使用 for 循环,因为我不知道我需要多少个循环。
这是说明问题的图片
所以,我想从第一个地方拿第一个令牌,从第二个地方拿第一个令牌,然后尝试通过警卫,如果通过,然后保存令牌,并打破循环,如果为假,继续第二个地方的第二个令牌..等等...我终于以第一名的最后一个令牌(4)和第二名的最后一个令牌(2)通过了守卫。
我会知道如何编码,如果我有恒定数量的地方进入过渡,它看起来像这样
for token in place 1
for token in place 2
try pass guard
if (passed)
save tokens
break;
但正如我之前所说,我没有固定数量的地方进入过渡,所以我不能使用这种方法。所以,基本上,我需要尝试令牌的组合,并尝试通过警卫 - 直到我通过警卫,或者直到我尝试了所有组合。你有什么想法 ?伪代码就足够了。顺便说一句,我使用这些数据结构
地点列表 - 普通 java 列表,List places = new ArrayList();
并且每个地方都有自己的token列表,List tokens = new ArrayList();
///编辑:守卫具有以下格式:
op1 rel op2,其中 op1 是变量,op2 是常量或变量,rel 是关系 (<,>,=,...) 保护中可以有多个条件与逻辑运算符 AND 连接 - 例如:op1 rel op2 && op3 相对 op4 ...
- - 编辑:
所以我尝试实现 Rusil 算法,但它有很多错误,所以我发布了 SSCCE,所以你可以尝试一下,也许会有所帮助。
首先,创建 Place 类:
public class Place {
public List<Integer> tokens ;
//constructor
public Place() {
this.tokens = new ArrayList<Integer>();
}
}
然后测试类:
public class TestyParmutace {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
List<Place> places = new ArrayList<Place>();
Place place1 = new Place();
place1.tokens.add(1);
place1.tokens.add(2);
place1.tokens.add(3);
places.add(place1); //add place to the list
Place place2 = new Place();
place2.tokens.add(3);
place2.tokens.add(4);
place2.tokens.add(5);
places.add(place2); //add place to the list
Place place3 = new Place();
place3.tokens.add(6);
place3.tokens.add(7);
place3.tokens.add(8);
places.add(place3); //add place to the list
//so we have
//P1 = {1,2,3}
//P2 = {3,4,5}
//P3 = {6,7,8}
List<Integer> tokens = new ArrayList<Integer>();
Func(places,0,tokens);
}
/**
*
* @param places list of places
* @param index index of current place
* @param tokens list of tokens
* @return true if we passed guard, false if we did not
*/
public static boolean Func( List<Place> places, int index, List<Integer> tokens)
{
if (index >= places.size())
{
// if control reaches here, it means that we've recursed through a particular combination
// ( consisting of exactly 1 token from each place ), and there are no more "places" left
String outputTokens = "";
for (int i = 0; i< tokens.size(); i++) {
outputTokens+= tokens.get(i) +",";
}
System.out.println("Tokens: "+outputTokens);
if (tokens.get(0) == 4 && tokens.get(1) == 5 && tokens.get(2) == 10) {
System.out.println("we passed the guard with 3,5,8");
return true;
}
else {
tokens.remove(tokens.get(tokens.size()-1));
return false;
}
}
Place p = places.get(index);
for (int i = 0; i< p.tokens.size(); i++)
{
tokens.add(p.tokens.get(i));
//System.out.println("Pridali sme token:" + p.tokens.get(i));
if ( Func( places, index+1, tokens ) ) return true;
}
if (tokens.size()>0)
tokens.remove(tokens.get(tokens.size()-1));
return false;
}
}
这是这段代码的输出:
Tokens: 1,3,6,
Tokens: 1,3,7,
Tokens: 1,3,8,
Tokens: 3,4,6,
Tokens: 3,4,7,
Tokens: 3,4,8,
Tokens: 4,5,6,
Tokens: 4,5,7,
Tokens: 4,5,8,
Tokens: 2,3,6,
Tokens: 2,3,7,
Tokens: 2,3,8,
Tokens: 3,4,6,
Tokens: 3,4,7,
Tokens: 3,4,8,
Tokens: 4,5,6,
Tokens: 4,5,7,
Tokens: 4,5,8,
Tokens: 3,3,6,
Tokens: 3,3,7,
Tokens: 3,3,8,
Tokens: 3,4,6,
Tokens: 3,4,7,
Tokens: 3,4,8,
Tokens: 4,5,6,
Tokens: 4,5,7,
Tokens: 4,5,8,
所以,你看,有些组合是正确的,比如 1,3,6 和 1,3,7...但是 4,5,8 绝对是胡说八道,因为 4 甚至不是放在首位...也是完全缺失的组合......像2,4,6等......有人知道为什么会这样吗?
编辑:现在它工作正常。