Created
October 2, 2023 20:44
-
-
Save hwayne/846faf9a841eb90a37221cebaba6d74b to your computer and use it in GitHub Desktop.
Petri net solver in Picat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import planner. | |
lookup(Val, Arr1, Arr2) = Out => | |
nth(I, Arr1, Val), | |
nth(I, Arr2, Out). | |
main => | |
Machine = [{{load1, 1}, {proc1, 0}, {done1, 0}, {load2, 1}, {proc2, 0}, {done2, 0}, {lock, 1}}, | |
[ {load1, | |
{{load1, 1}, {lock, 1}}, % Maybe the inner ones should be lists, they're only iterated over | |
{{proc1, 1}}}, | |
{load2, {{load2, 1}, {lock, 1}}, {{proc2, 1}}}, | |
{proc1, {{proc1, 1}}, {{done1, 1}, {lock, 1}}}, | |
{proc2, {{proc2, 1}}, {{done2, 1}, {lock, 1}}} | |
] | |
], | |
nl, | |
if plan(Machine, 4, Plan) then | |
writeln(Plan), | |
else | |
writeln(impossible), | |
end, | |
nl. | |
final([Nodes, Arcs]) => % Solution is hardcoded, srry | |
Nodes = { | |
{load1, 0}, | |
{proc1, 0}, | |
{done1, 1}, | |
{load2, 0}, | |
{proc2, 0}, | |
{done2, 1}, | |
{lock, _} | |
}. | |
action(S0, S1, Action, ActionCost) => | |
Action = {fire, ArcId}, | |
ActionCost = 1, | |
Arc = {ArcId, Sources, Sinks}, | |
member(Arc, S0[2]), | |
Nodes = copy_term(S0[1]), % Needed for updates | |
% remove dots | |
foreach({S, ToRemove} in Sources) | |
nth(I, Nodes, {S, D}), | |
D >= ToRemove, % Has enough dots | |
Nodes[I] := {S, D - ToRemove} | |
end, | |
% add dots | |
foreach({S, ToAdd} in Sinks) | |
nth(I, Nodes, {S, D}), | |
Nodes[I] := {S, D + ToAdd} | |
end, | |
writeln(Nodes), | |
S1 = [Nodes, S0[2]]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment