or-tools cp-sat CpModel.AddAutomaton
1 示例程序
链接: 官方文档
官方解释原文及翻译:
Adds an automaton constraint.
添加自动机约束。
An automaton constraint takes a list of variables (of size n), an initial state, a set of final states, and a set of transitions. A transition is a triplet (tail, transition, head), where tail and head are states, and transition is the label of an arc from head to tail, corresponding to the value of one variable in the list of variables.
自动机约束包含一个变量数组(元素个数为n)、一个初始状态、一组最终状态和一组过度元组,此过度元组包含三个值:结尾、过渡和起始,结尾和起始是状态,过渡是从起始到结尾的情节标签,对应的是变量数组中的一个变量的值。
This automaton will be unrolled into a flow with n + 1 phases. Each phase contains the possible states of the automaton. The first state contains the initial state. The last phase contains the final states.
本自动机会进入一个有n+1个阶段的流程中(n是变量数组元素个数),每一个阶段中都包含自动机的多个可能状态,第一个状态包含初始状态,最后一个阶段包含最终状态(译者注:可能是最后一个状态,而不是最后一个阶段,所以可以翻译为:最后一个状态是最终状态组中的一个)。
Between two consecutive phases i and i + 1, the automaton creates a set of arcs. For each transition (tail, transition, head), it will add an arc from the state tail of phase i and the state head of phase i + 1. This arc is labeled by the value transition of the variables variables[i]. That is, this arc can only be selected if variables[i] is assigned the value transition.
在两个相邻的阶段(i和i+1)之间,自动机会创建一组情节。对于每一个过渡(结尾、过渡和起始),自动机会在阶段i的结尾和阶段i+1的起始之间创建一个情节,这个情节代表的是变量数组中的第i个变量的值,也就是说,只有在第i个变量的值被赋值为过渡中的值时这个情节才会被选中。
A feasible solution of this constraint is an assignment of variables such that, starting from the initial state in phase 0, there is a path labeled by the values of the variables that ends in one of the final states in the final phase.
例如可以用如下方法来构建这个约束:从初始状态0开始,有一个由变量的各个值标定的路径,路径的结束点是最终状态组中的一个。
Args
transition_variables
A non-empty list of variables whose values correspond to the labels of the arcs traversed by the automaton.
过渡变量数组
starting_state
The initial state of the automaton.
起始状态
final_states
A non-empty list of admissible final states.
可采纳的结束状态
transition_triples
A list of transitions for the automaton, in the following format (current_state, variable_value, next_state).
过渡列表
Returns
An instance of the Constraint class.
约束实例
Raises
ValueError
if transition_variables, final_states, or transition_triples are empty.
x = [model.NewIntVar(0,100,f"X{i}") for i in range(3)]
initial_state = 0
accepting_states = [3,4]
transitions = [
(0,1,1),
(1,21,2),
(1,22,2),
(2,3,3)
]
model.AddAutomaton(x, initial_state, accepting_states, transitions)
# (0,1,1)->(1,21,2)->(2,3,3)
# (0,1,1)->(1,22,2)->(2,3,3)
# output: [1, 21, 3]
# output: [1, 22, 3]