Skip to content

Commit 69dffed

Browse files
committed
Merge branch 'master' of https://github.com/ArBom/SharpPDDL
2 parents e71b718 + fdfd53b commit 69dffed

File tree

1 file changed

+157
-124
lines changed

1 file changed

+157
-124
lines changed

README.md

Lines changed: 157 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![LoC](https://raw.githubusercontent.com/ArBom/SharpPDDL/refs/heads/loc/badge.svg)](https://github.com/ArBom/SharpPDDL/blob/master/.github/workflows/loc.yml)
66
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/ArBom/SharpPDDL?style=plastic&logo&color=4bc721)
77
[![NuGet Version](https://img.shields.io/nuget/vpre/SharpPDDL?style=plastic&logo=nuget&label=NuGet&color=004880&cacheSeconds=7200)](https://www.nuget.org/packages/SharpPDDL)
8-
[![NuGet Downloads](https://img.shields.io/nuget/dt/SharpPDDL?style=plastic&color=004880)](https://nugettrends.com/packages?ids=SharpPDDL&months=6)
8+
[![NuGet Downloads](https://img.shields.io/nuget/dt/SharpPDDL?style=plastic&color=004880)](https://nugettrends.com/packages?ids=SharpPDDL&months=10)
99

1010
---
1111

@@ -16,6 +16,114 @@ This is the class library based on PDDL intellection and in effect it's a implem
1616
1717
One can to use previously defined classes which are using in other part of one's programm. At this version library can return the plan of doing and execute it to realize the goal. Examples of problems possible to solution by this algorithm:
1818

19+
<details>
20+
<summary>Peg solitaire</summary>
21+
22+
Treatment the game: [wiki](https://en.wikipedia.org/wiki/Peg_solitaire)
23+
24+
In this example used triangular, 15-holes variant as easiest to solve.
25+
26+
Due to shape of game board it needs to use 3 possible action of jump. One of it, the horizontal move is shown below:
27+
```cs
28+
Expression<Predicate<Spot>> FullSpot = S => S.Full;
29+
Expression<Predicate<Spot>> EmptySpot = S => !S.Full;
30+
31+
ActionPDDL HorizontalJump = new ActionPDDL("Horizontal jump");
32+
33+
HorizontalJump.AddPrecondiction<Spot, Spot>("Jumping peg exists", ref JumpingPeg, FullSpot);
34+
HorizontalJump.AddPrecondiction<Spot, Spot>("Remove peg exists", ref RemovePeg, FullSpot);
35+
HorizontalJump.AddPrecondiction<Spot, Spot>("Final position of peg is empty", ref FinalPegPos, EmptySpot);
36+
37+
Expression<Predicate<Spot, Spot, Spot>> Horizontalcollinear = ((JP, RP, FPP) => (JP.Row == RP.Row && RP.Row == FPP.Row));
38+
HorizontalJump.AddPrecondiction("The same vertical line", ref JumpingPeg, ref RemovePeg, ref FinalPegPos, Horizontalcollinear);
39+
40+
Expression<Predicate<Spot, Spot>> VerticalClose = ((S1, S2) => ((S1.Column - S2.Column) == 1 || (S1.Column - S2.Column) == -1));
41+
HorizontalJump.AddPrecondiction("Jumper is close", ref JumpingPeg, ref RemovePeg, VerticalClose);
42+
HorizontalJump.AddPrecondiction("Hole is close", ref FinalPegPos, ref RemovePeg, VerticalClose);
43+
44+
HorizontalJump.AddEffect("Jumping Peg Spot is empty", ref JumpingPeg, JP => JP.Full, false);
45+
HorizontalJump.AddEffect("Remove Peg Spot is empty", ref RemovePeg, RP => RP.Full, false);
46+
HorizontalJump.AddEffect("Final Peg Spot is full", ref FinalPegPos, RP => RP.Full, true);
47+
```
48+
The execution of it uses effects from above and static voids of Spot class.
49+
```cs
50+
HorizontalJump.AddExecution("Reset colours", () => Reset(), false);
51+
HorizontalJump.AddExecution("Jumping Peg Spot is empty");
52+
HorizontalJump.AddExecution("Remove Peg Spot is empty");
53+
HorizontalJump.AddExecution("Final Peg Spot is full");
54+
HorizontalJump.AddExecution("Draw it", () => Board.Draw(spots), true);
55+
HorizontalJump.AddExecution("Wait", () => Thread.Sleep(1500), true);
56+
```
57+
At this case it's possible to reach 3016 possible states, which is generated in time of about 13s.
58+
59+
![Peg_solitaire_solution](https://github.com/user-attachments/assets/4c7a440f-be36-4bcc-a737-d9266bb88809)
60+
61+
</details>
62+
63+
<details>
64+
<summary>River crossing puzzle</summary>
65+
66+
Treatment the puzzle: [wiki](https://en.wikipedia.org/wiki/Wolf,_goat_and_cabbage_problem)
67+
68+
Putting a thing to the boat:
69+
```cs
70+
ActionPDDL TakingCabbage = new ActionPDDL("TakingCabbage");
71+
TakingCabbage.AddPartOfActionSententia("Take the cabbage.");
72+
TakingCabbage.AddPrecondiction("Boat is near the bank", ref nextToBank, b => b.IsBoat);
73+
TakingCabbage.AddPrecondiction("Cabbage is at the bank", ref nextToBank, b => b.IsCabbage);
74+
TakingCabbage.AddPrecondiction("Boat is empty", ref boat, b => !b.IsCabbage && !b.IsGoat && !b.IsWolf);
75+
TakingCabbage.AddEffect("Remove the cabbage from the bank", ref nextToBank, b => b.IsCabbage, false);
76+
TakingCabbage.AddEffect("Put the cabbage on the boat", ref boat, b => b.IsCabbage, true);
77+
RiverCrossing.AddAction(TakingCabbage);
78+
```
79+
80+
Putting a thing away:
81+
```cs
82+
ActionPDDL PutCabbageAway = new ActionPDDL("PuttingCabbageAway");
83+
PutCabbageAway.AddPartOfActionSententia("Put the cabbage away.");
84+
PutCabbageAway.AddPrecondiction("Boat is near the bank", ref nextToBank, b => b.IsBoat);
85+
PutCabbageAway.AddPrecondiction("Goat is on the bank", ref boat, b => b.IsCabbage);
86+
PutCabbageAway.AddEffect("Remove the goat from the bank", ref nextToBank, b => b.IsCabbage, true);
87+
PutCabbageAway.AddEffect("Add the goat to the boat", ref boat, b => b.IsCabbage, false);
88+
RiverCrossing.AddAction(PutCabbageAway);
89+
```
90+
91+
One need to use the above 3 times. For the cabbage, goat and wolf.
92+
93+
Going to the other river bank:
94+
```cs
95+
ActionPDDL CrossTheRiver = new ActionPDDL("CrossingTheRiver");
96+
CrossTheRiver.AddPartOfActionSententia("Cross the river.");
97+
CrossTheRiver.AddPrecondiction("Boat is near the bank", ref nextToBank, b => b.IsBoat);
98+
CrossTheRiver.AddPrecondiction("Nothing won't be eaten", ref nextToBank, b => b.IsGoat ? (!b.IsCabbage && !b.IsWolf) : true );
99+
RiverBank SecendBank = null;
100+
CrossTheRiver.AddEffect("Leave the river bank", ref nextToBank, b => b.IsBoat, false);
101+
CrossTheRiver.AddEffect("Go to the other bank", ref SecendBank, b => b.IsBoat, true);
102+
RiverCrossing.AddAction(CrossTheRiver);
103+
```
104+
105+
Generated plan:
106+
```
107+
1: Take the goat.
108+
2: Cross the river.
109+
3: Put the goat away.
110+
4: Cross the river.
111+
5: Take the wolf.
112+
6: Cross the river.
113+
7: Put the wolf away.
114+
8: Take the goat.
115+
9: Cross the river.
116+
10: Put the goat away.
117+
11: Take the cabbage.
118+
12: Cross the river.
119+
13: Put the cabbage away.
120+
14: Cross the river.
121+
15: Take the goat.
122+
16: Cross the river.
123+
17: Put the goat away.
124+
```
125+
</details>
126+
19127
<details>
20128
<summary>Tower of Hanoi</summary>
21129

@@ -192,128 +300,6 @@ Move brick onto another brick: Place the 1-size brick onto 2-size brick.
192300
```
193301
</details>
194302

195-
<details>
196-
<summary>River crossing puzzle</summary>
197-
198-
Treatment the puzzle: [wiki](https://en.wikipedia.org/wiki/Wolf,_goat_and_cabbage_problem)
199-
200-
Putting a thing to the boat:
201-
```cs
202-
ActionPDDL TakingCabbage = new ActionPDDL("TakingCabbage");
203-
TakingCabbage.AddPartOfActionSententia("Take the cabbage.");
204-
TakingCabbage.AddPrecondiction("Boat is near the bank", ref nextToBank, b => b.IsBoat);
205-
TakingCabbage.AddPrecondiction("Cabbage is at the bank", ref nextToBank, b => b.IsCabbage);
206-
TakingCabbage.AddPrecondiction("Boat is empty", ref boat, b => !b.IsCabbage && !b.IsGoat && !b.IsWolf);
207-
TakingCabbage.AddEffect("Remove the cabbage from the bank", ref nextToBank, b => b.IsCabbage, false);
208-
TakingCabbage.AddEffect("Put the cabbage on the boat", ref boat, b => b.IsCabbage, true);
209-
RiverCrossing.AddAction(TakingCabbage);
210-
```
211-
212-
Putting a thing away:
213-
```cs
214-
ActionPDDL PutCabbageAway = new ActionPDDL("PuttingCabbageAway");
215-
PutCabbageAway.AddPartOfActionSententia("Put the cabbage away.");
216-
PutCabbageAway.AddPrecondiction("Boat is near the bank", ref nextToBank, b => b.IsBoat);
217-
PutCabbageAway.AddPrecondiction("Goat is on the bank", ref boat, b => b.IsCabbage);
218-
PutCabbageAway.AddEffect("Remove the goat from the bank", ref nextToBank, b => b.IsCabbage, true);
219-
PutCabbageAway.AddEffect("Add the goat to the boat", ref boat, b => b.IsCabbage, false);
220-
RiverCrossing.AddAction(PutCabbageAway);
221-
```
222-
223-
One need to use the above 3 times. For the cabbage, goat and wolf.
224-
225-
Going to the other river bank:
226-
```cs
227-
ActionPDDL CrossTheRiver = new ActionPDDL("CrossingTheRiver");
228-
CrossTheRiver.AddPartOfActionSententia("Cross the river.");
229-
CrossTheRiver.AddPrecondiction("Boat is near the bank", ref nextToBank, b => b.IsBoat);
230-
CrossTheRiver.AddPrecondiction("Nothing won't be eaten", ref nextToBank, b => b.IsGoat ? (!b.IsCabbage && !b.IsWolf) : true );
231-
RiverBank SecendBank = null;
232-
CrossTheRiver.AddEffect("Leave the river bank", ref nextToBank, b => b.IsBoat, false);
233-
CrossTheRiver.AddEffect("Go to the other bank", ref SecendBank, b => b.IsBoat, true);
234-
RiverCrossing.AddAction(CrossTheRiver);
235-
```
236-
237-
Generated plan:
238-
```
239-
1: Take the goat.
240-
2: Cross the river.
241-
3: Put the goat away.
242-
4: Cross the river.
243-
5: Take the wolf.
244-
6: Cross the river.
245-
7: Put the wolf away.
246-
8: Take the goat.
247-
9: Cross the river.
248-
10: Put the goat away.
249-
11: Take the cabbage.
250-
12: Cross the river.
251-
13: Put the cabbage away.
252-
14: Cross the river.
253-
15: Take the goat.
254-
16: Cross the river.
255-
17: Put the goat away.
256-
```
257-
258-
</details>
259-
260-
<details>
261-
<summary>Water pouring puzzle</summary>
262-
263-
Treatment the puzzle: [wiki](https://en.wikipedia.org/wiki/Water_pouring_puzzle)
264-
265-
```cs
266-
public class WaterJug
267-
{
268-
public readonly float Capacity;
269-
public float flood;
270-
271-
}
272-
```
273-
```cs
274-
DomeinPDDL DecantingDomein = new DomeinPDDL("Decanting problems"); //In this problem...
275-
276-
ActionPDDL DecantWater = new ActionPDDL("Decant water"); //...you need one action with 2 arguments:
277-
WaterJug SourceJug = null; //The jug from which you pour,
278-
WaterJug DestinationJug = null; // and the jug you pour into.
279-
280-
DecantWater.AddPartOfActionSententia(ref SourceJug, "from {0}-liter jug ", SJ => SJ.Capacity);
281-
DecantWater.AddPartOfActionSententia(ref DestinationJug, "to the {0}-liter jug.", DJ => DJ.Capacity);
282-
283-
//In the effect of decanting the level in the jug from which you pour is maked smaller after that,...
284-
DecantWater.AddEffect( //SourceJug.flood = DestinationJug.flood + SourceJug.flood >= DestinationJug.Capacity ? SourceJug.flood - DestinationJug.Capacity + DestinationJug.flood : 0
285-
"Reduce source jug flood",
286-
ref SourceJug,
287-
Source_Jug => Source_Jug.flood,
288-
ref DestinationJug,
289-
(Source_Jug, Destination_Jug) => Destination_Jug.flood + Source_Jug.flood >= Destination_Jug.Capacity ? Source_Jug.flood - Destination_Jug.Capacity + Destination_Jug.flood : 0);
290-
291-
//...the level in the jug you pour into is maked bigger.
292-
DecantWater.AddEffect( //DestinationJug.flood = DestinationJug.flood + SourceJug.flood >= DestinationJug.Capacity ? DestinationJug.Capacity : DestinationJug.flood + SourceJug.flood
293-
"Increase destination jug flood",
294-
ref DestinationJug,
295-
Destination_Jug => Destination_Jug.flood,
296-
ref SourceJug,
297-
(Destination_Jug, Source_Jug) => Destination_Jug.flood + Source_Jug.flood >= Destination_Jug.Capacity ? Destination_Jug.Capacity : Destination_Jug.flood + Source_Jug.flood);
298-
299-
//One need to do as fast as possible
300-
DecantWater.DefineActionCost(ref SourceJug, ref DestinationJug, (S, D) => WaterJug.DecantedWater(S.flood, D.Capacity, D.flood));
301-
302-
DecantingDomein.AddAction(DecantWater);
303-
```
304-
```
305-
SharpPDDL : Divide in half determined!!! Total Cost: 22
306-
Decant water: from 8-liter jug to the 5-liter jug. Action cost: 5
307-
Decant water: from 5-liter jug to the 3-liter jug. Action cost: 3
308-
Decant water: from 3-liter jug to the 8-liter jug. Action cost: 3
309-
Decant water: from 5-liter jug to the 3-liter jug. Action cost: 2
310-
Decant water: from 8-liter jug to the 5-liter jug. Action cost: 5
311-
Decant water: from 5-liter jug to the 3-liter jug. Action cost: 1
312-
Decant water: from 3-liter jug to the 8-liter jug. Action cost: 3
313-
all states generated
314-
```
315-
</details>
316-
317303
<details>
318304
<summary>Travelling salesman problem</summary>
319305

@@ -381,8 +367,55 @@ Travel: Go to Kraków. Action cost: 304
381367
Travel: Go to Koszalin. Action cost: 700
382368
```
383369

384-
Make you sure about the solution with another program: [AtoZmath.com](https://cbom.atozmath.com/CBOM/Assignment.aspx?q=tsnn&q1=0%2C245%2C700%2C372%2C250%2C520%2C687%3B245%2C0%2C456%2C165%2C48%2C293%2C448%3B700%2C456%2C0%2C364%2C458%2C290%2C304%3B372%2C165%2C364%2C0%2C227%2C109%2C295%3B250%2C48%2C458%2C227%2C0%2C311%2C478%3B520%2C293%2C290%2C109%2C311%2C0%2C173%3B687%2C448%2C304%2C295%2C478%2C173%2C0%60MIN%60Koszalin%2CGniezno%2CKrak%C3%B3w%2CP%C5%82ock%2CPozna%C5%84%2CWarszawa%2CLublin%60Koszalin%2CGniezno%2CKrak%C3%B3w%2CP%C5%82ock%2CPozna%C5%84%2CWarszawa%2CLublin%60false%60false&do=1#tblSolution)
370+
You can make you sure about the solution with another program: [AtoZmath.com](https://cbom.atozmath.com/CBOM/Assignment.aspx?q=tsnn&q1=0%2C245%2C700%2C372%2C250%2C520%2C687%3B245%2C0%2C456%2C165%2C48%2C293%2C448%3B700%2C456%2C0%2C364%2C458%2C290%2C304%3B372%2C165%2C364%2C0%2C227%2C109%2C295%3B250%2C48%2C458%2C227%2C0%2C311%2C478%3B520%2C293%2C290%2C109%2C311%2C0%2C173%3B687%2C448%2C304%2C295%2C478%2C173%2C0%60MIN%60Koszalin%2CGniezno%2CKrak%C3%B3w%2CP%C5%82ock%2CPozna%C5%84%2CWarszawa%2CLublin%60Koszalin%2CGniezno%2CKrak%C3%B3w%2CP%C5%82ock%2CPozna%C5%84%2CWarszawa%2CLublin%60false%60false&do=1#tblSolution)
371+
372+
</details>
385373

374+
<details>
375+
<summary>Water pouring puzzle</summary>
376+
377+
Treatment the puzzle: [wiki](https://en.wikipedia.org/wiki/Water_pouring_puzzle)
378+
379+
```cs
380+
public class WaterJug
381+
{
382+
public readonly float Capacity;
383+
public float flood;
384+
385+
}
386+
```
387+
```cs
388+
DomeinPDDL DecantingDomein = new DomeinPDDL("Decanting problems"); //In this problem...
389+
390+
ActionPDDL DecantWater = new ActionPDDL("Decant water"); //...you need one action with 2 arguments:
391+
WaterJug SourceJug = null; //The jug from which you pour,
392+
WaterJug DestinationJug = null; // and the jug you pour into.
393+
394+
DecantWater.AddPartOfActionSententia(ref SourceJug, "from {0}-liter jug ", SJ => SJ.Capacity);
395+
DecantWater.AddPartOfActionSententia(ref DestinationJug, "to the {0}-liter jug.", DJ => DJ.Capacity);
396+
397+
//In the effect of decanting the level in the jug from which you pour is maked smaller after that,...
398+
DecantWater.AddEffect( //SourceJug.flood = DestinationJug.flood + SourceJug.flood >= DestinationJug.Capacity ? SourceJug.flood - DestinationJug.Capacity + DestinationJug.flood : 0
399+
"Reduce source jug flood",
400+
ref SourceJug,
401+
Source_Jug => Source_Jug.flood,
402+
ref DestinationJug,
403+
(Source_Jug, Destination_Jug) => Destination_Jug.flood + Source_Jug.flood >= Destination_Jug.Capacity ? Source_Jug.flood - Destination_Jug.Capacity + Destination_Jug.flood : 0);
404+
405+
//...the level in the jug you pour into is maked bigger.
406+
DecantWater.AddEffect( //DestinationJug.flood = DestinationJug.flood + SourceJug.flood >= DestinationJug.Capacity ? DestinationJug.Capacity : DestinationJug.flood + SourceJug.flood
407+
"Increase destination jug flood",
408+
ref DestinationJug,
409+
Destination_Jug => Destination_Jug.flood,
410+
ref SourceJug,
411+
(Destination_Jug, Source_Jug) => Destination_Jug.flood + Source_Jug.flood >= Destination_Jug.Capacity ? Destination_Jug.Capacity : Destination_Jug.flood + Source_Jug.flood);
412+
413+
//One need to do as fast as possible
414+
DecantWater.DefineActionCost(ref SourceJug, ref DestinationJug, (S, D) => WaterJug.DecantedWater(S.flood, D.Capacity, D.flood));
415+
416+
DecantingDomein.AddAction(DecantWater);
417+
```
418+
![Water_pouring_solution](https://github.com/user-attachments/assets/3e35f26a-d4fe-46c9-a1e2-c4bba66b5225)
386419
</details>
387420

388421
---

0 commit comments

Comments
 (0)