Parallel processing 并行计算NFA转换

Parallel processing 并行计算NFA转换,parallel-processing,f#,finite-automata,Parallel Processing,F#,Finite Automata,我用F#编写了这段代码: 类型非确定性FiniteAutomaton={ initialState:字符串 最后状态:字符串列表 过渡:映射 } 让私有nextState(符号:char)(状态:string)(转换:Map)= 转换|>Map.tryFind(状态、符号) let rec private haltState(输入:string)(索引:int)(状态:string)(转换:Map)= 与索引匹配 |当x=input.Length->state时为x | _ -> 将nextSt

我用F#编写了这段代码:

类型非确定性FiniteAutomaton={
initialState:字符串
最后状态:字符串列表
过渡:映射
}
让私有nextState(符号:char)(状态:string)(转换:Map)=
转换|>Map.tryFind(状态、符号)
let rec private haltState(输入:string)(索引:int)(状态:string)(转换:Map)=
与索引匹配
|当x=input.Length->state时为x
| _ ->
将nextState输入。[index]状态转换与匹配
|当x.IsNone->null时为x
|x->haltState输入(索引+1)x.值转换
在最后一行,
x.Value
将返回我的自动机可以输入的状态列表,比如
[“s1”;“s2”;“s3”]
。对于此列表中的每个状态,我希望并行调用
haltState
,从而并行计算每个可能的路径

  • 我怎么能把它们并列起来呢
  • 完成后,我如何“加入”他们

我建议先编写完整的顺序版本。然后,您可以看到添加并行性对于您将要进行的计算是否有意义

至于加入结果,即使在顺序版本中也需要这样做。您的
haltState
函数返回一个字符串,但是如果这是一个NFA,那么它可能以多个不同的状态结束。因此,您可以将其更改为返回一系列可能的结果:

let rec private haltState (input:string) (index:int) (state:string) (transitions:Map<string * char, string List>) =
    match index with
    | x when x = input.Length -> Seq.singleton x
    | _ ->
        match nextState input.[index] state transitions with
        | None -> Seq.empty
        | Some states -> 
            states |> Seq.collect (fun state -> 
              haltState input (index+1) state transitions)

我建议首先编写完整的顺序版本。然后,您可以看到添加并行性对于您将要进行的计算是否有意义

至于加入结果,即使在顺序版本中也需要这样做。您的
haltState
函数返回一个字符串,但是如果这是一个NFA,那么它可能以多个不同的状态结束。因此,您可以将其更改为返回一系列可能的结果:

let rec private haltState (input:string) (index:int) (state:string) (transitions:Map<string * char, string List>) =
    match index with
    | x when x = input.Length -> Seq.singleton x
    | _ ->
        match nextState input.[index] state transitions with
        | None -> Seq.empty
        | Some states -> 
            states |> Seq.collect (fun state -> 
              haltState input (index+1) state transitions)

关于您的第一个代码:
Seq.singleton x
应该是
Seq.singleton state
,对吗?为什么是Seq,而不是List?是的,它应该是
Seq.singleton state
。您可以使用
Seq
List
在这两种情况下都有效。如果您想惰性地使用结果,然后可以将结果转换为任何其他集合,那么Seq很好,但是您需要自己做实验,以找出最适合您需要的集合。关于您的第一个代码:
Seq.singleton x
应该是
Seq.singleton state
,对吗?为什么是Seq,而不是List?是的,它应该是
Seq.singleton state
。您可以使用
Seq
List
在这两种情况下都有效。如果您想惰性地使用结果,然后可以将结果转换为任何其他集合,那么Seq是很好的选择,但是您需要自己做实验,找出最适合您需要的。
let rec private haltState (input:string) (index:int) (state:string) (transitions:Map<string * char, string List>) =
    match index with
    | x when x = input.Length -> [| state |]
    | _ ->
        match nextState input.[index] state transitions with
        | None -> [| |]
        | Some states -> 
            states
            |> Array.ofSeq
            |> Array.Parallel.collect (fun state -> haltState input (index+1) state transitions)