|
| 1 | +Treatment the puzzle: [wiki](https://en.wikipedia.org/wiki/Tower_of_Hanoi) |
| 2 | +```cs |
| 3 | +public class HanoiObj //It cannot be abstract |
| 4 | +{ |
| 5 | + public int HanoiObjSizeUpSide = 0; |
| 6 | + public bool IsEmptyUpSide; |
| 7 | +} |
| 8 | + |
| 9 | +public class HanoiBrick : HanoiObj |
| 10 | +{ |
| 11 | + readonly public int Size; |
| 12 | +} |
| 13 | + |
| 14 | +public class HanoiTable : HanoiObj |
| 15 | +{ |
| 16 | + public readonly int no; |
| 17 | +} |
| 18 | +``` |
| 19 | +```mermaid |
| 20 | +
|
| 21 | +classDiagram |
| 22 | +
|
| 23 | +namespace Legend { |
| 24 | +
|
| 25 | + class Class{ |
| 26 | + Its a block representant some class |
| 27 | + } |
| 28 | +
|
| 29 | + class Object { |
| 30 | + Its a block representant some object / class instance |
| 31 | + } |
| 32 | +
|
| 33 | +} |
| 34 | +
|
| 35 | + style Object fill:#391, stroke-style:.. |
| 36 | + style Class fill:#139, stroke-style:.. |
| 37 | +
|
| 38 | +namespace HanoiTower { |
| 39 | +
|
| 40 | + class HanoiObj{ |
| 41 | + +int HanoiObjSizeUpSide |
| 42 | + +bool IsEmptyUpSide |
| 43 | + } |
| 44 | +
|
| 45 | + class HanoiBrick{ |
| 46 | + +int Size |
| 47 | + } |
| 48 | +
|
| 49 | + class HanoiTable { |
| 50 | + +int no |
| 51 | + } |
| 52 | +} |
| 53 | + HanoiObj <|-- HanoiBrick |
| 54 | + HanoiObj <|-- HanoiTable |
| 55 | +
|
| 56 | + style HanoiObj fill:#139, stroke-style:.. |
| 57 | + style HanoiBrick fill:#139, stroke-style:.. |
| 58 | + style HanoiTable fill:#139, stroke-style:.. |
| 59 | +
|
| 60 | +namespace SharpPDDL { |
| 61 | +
|
| 62 | + class Root_TreeNode{ |
| 63 | + ~SingleTypeOfDomain Content |
| 64 | + ~List~TreeNode~ Children |
| 65 | + } |
| 66 | +
|
| 67 | + class HanoiObj_SingleTypeOfDomain { |
| 68 | + ~Type Type : BaseShapes.HanoiObj |
| 69 | + ~List~ValueOfThumbnail~ CumulativeValues |
| 70 | + } |
| 71 | +
|
| 72 | + class 0_TreeNode{ |
| 73 | + ~SingleTypeOfDomain Content |
| 74 | + ~List~TreeNode~ Children |
| 75 | + } |
| 76 | +
|
| 77 | + class HanoiBrick_SingleTypeOfDomain { |
| 78 | + ~Type Type : BaseShapes.HanoiObj |
| 79 | + ~List~ValueOfThumbnail~ CumulativeValues |
| 80 | + } |
| 81 | +
|
| 82 | + class 1_TreeNode{ |
| 83 | + ~SingleTypeOfDomain Content |
| 84 | + ~List~TreeNode~ Children |
| 85 | + } |
| 86 | +
|
| 87 | + class HanoiTable_SingleTypeOfDomain { |
| 88 | + ~Type Type : BaseShapes.HanoiObj |
| 89 | + ~List~ValueOfThumbnail~ CumulativeValues |
| 90 | + } |
| 91 | +} |
| 92 | + style Root_TreeNode fill:#391, stroke-style:.. |
| 93 | + style 0_TreeNode fill:#391, stroke-style:.. |
| 94 | + style 1_TreeNode fill:#391, stroke-style:.. |
| 95 | + style HanoiObj_SingleTypeOfDomain fill:#391, stroke-style:.. |
| 96 | + style HanoiBrick_SingleTypeOfDomain fill:#391, stroke-style:.. |
| 97 | + style HanoiTable_SingleTypeOfDomain fill:#391, stroke-style:.. |
| 98 | + |
| 99 | + Root_TreeNode --> "Children[0]" 0_TreeNode |
| 100 | + Root_TreeNode --> "Children[1]" 1_TreeNode |
| 101 | + 0_TreeNode --> "Content" HanoiBrick_SingleTypeOfDomain |
| 102 | + 1_TreeNode --> "Content" HanoiTable_SingleTypeOfDomain |
| 103 | + Root_TreeNode --> "Content" HanoiObj_SingleTypeOfDomain |
| 104 | + HanoiObj_SingleTypeOfDomain ..> "≙" HanoiObj |
| 105 | + HanoiBrick_SingleTypeOfDomain ..> "≙" HanoiBrick |
| 106 | + HanoiTable_SingleTypeOfDomain ..> "≙" HanoiTable |
| 107 | +
|
| 108 | + note for HanoiObj_SingleTypeOfDomain "CumulativeValues:<br> 1: HanoiObSizeUpSide<br> 2: IsEmptyUpSide" |
| 109 | + note for HanoiTable_SingleTypeOfDomain "CumulativeValues:<br> 1: HanoiObSizeUpSide<br> 2: IsEmptyUpSide<br> // int:no is not use in any action" |
| 110 | + note for HanoiBrick_SingleTypeOfDomain "CumulativeValues:<br> 1: HanoiObSizeUpSide<br> 2: IsEmptyUpSide<br> 3: Size" |
| 111 | +
|
| 112 | +``` |
| 113 | +Instances of class used to define action shouldn't be use in other part of program. In time of create actions library create class instance excluding use the class constructor. |
| 114 | + |
| 115 | +For these classes one can define rules in library like "Move brick onto another brick" or "Move brick on table". Preconditions, effect etc. are phrased by library's user as Expressions (System.Linq.Expressions): |
| 116 | + |
| 117 | +```cs |
| 118 | +HanoiBrick MovedBrick = null; //you can take brick... |
| 119 | +HanoiObj ObjBelowMoved = null; //...from table or another brick... |
| 120 | +HanoiBrick NewStandB = null; //...and put it into bigger brick... |
| 121 | +HanoiTable NewStandT = null; //...or empty table spot. |
| 122 | +
|
| 123 | +Expression<Predicate<HanoiObj>> ObjectIsNoUp = (HO => HO.IsEmptyUpSide); //Moved brick have to be empty up side |
| 124 | +Expression<Predicate<HanoiBrick, HanoiBrick>> PutSmallBrickAtBigger = ((MB, NSB) => (MB.Size < NSB.Size)); //you can put smaller brick onto bigger one |
| 125 | +Expression<Predicate<HanoiBrick, HanoiObj>> FindObjBelongMovd = ((MB, OBM) => (MB.Size == OBM.HanoiObjSizeUpSide)); |
| 126 | + |
| 127 | +ActionPDDL moveBrickOnBrick = new ActionPDDL("Move brick onto another brick"); //1st action with 3 parameters: MovedBrick, ObjBelowMoved, NewStandB |
| 128 | +
|
| 129 | +moveBrickOnBrick.AddPartOfActionSententia(ref MovedBrick, "Place the {0}-size brick ", MB => MB.Size); |
| 130 | +moveBrickOnBrick.AddPartOfActionSententia(ref NewStandB, "onto {0}-size brick.", MB => MB.Size); |
| 131 | + |
| 132 | +moveBrickOnBrick.AddPrecondition("Moved brick is no up", ref MovedBrick, ObjectIsNoUp); //MovedBrick.IsEmptyUpSide == true |
| 133 | +moveBrickOnBrick.AddPrecondition("New stand is empty", ref NewStandB, ObjectIsNoUp); //NewStandB.IsEmptyUpSide == true |
| 134 | +moveBrickOnBrick.AddPrecondition("Small brick on bigger one", ref MovedBrick, ref NewStandB, PutSmallBrickAtBigger); //MovedBrick.Size < NewStandB.Size |
| 135 | +moveBrickOnBrick.AddPrecondition("Find brick bottom moved one", ref MovedBrick, ref ObjBelowMoved, FindObjBelongMovd); //MovedBrick.Size == ObjBelowMoved.HanoiObjSizeUpSide |
| 136 | +
|
| 137 | +moveBrickOnBrick.AddEffect("New stand is full", ref NewStandB, NS => NS.IsEmptyUpSide, false); //NewStandB.IsEmptyUpSide = false |
| 138 | +moveBrickOnBrick.AddEffect("Old stand is empty", ref ObjBelowMoved, NS => NS.IsEmptyUpSide, true); //ObjBelowMoved.IsEmptyUpSide = true |
| 139 | +moveBrickOnBrick.AddEffect("UnConsociate Objs", ref ObjBelowMoved, OS => OS.HanoiObjSizeUpSide, 0); //ObjBelowMoved.HanoiObjSizeUpSide = 0 |
| 140 | +moveBrickOnBrick.AddEffect("Consociate Bricks", ref NewStandB, NSB => NSB.HanoiObjSizeUpSide, ref MovedBrick, MB => MB.Size); //NewStandB.HanoiObjSizeUpSide = MovedBrick.Size |
| 141 | +
|
| 142 | +newDomain.AddAction(moveBrickOnBrick); //Putting empty brick onto bigger one |
| 143 | +
|
| 144 | +ActionPDDL moveBrickOnTable = new ActionPDDL("Move brick on table"); //2st action with 3 parameters: MovedBrick, ObjBelowMoved, NewStandT |
| 145 | +
|
| 146 | +moveBrickOnTable.AddPartOfActionSententia(ref MovedBrick, "Place the {0}-size brick ", MB => MB.Size); |
| 147 | +moveBrickOnTable.AddPartOfActionSententia(ref NewStandT, "onto table no {0}.", NS => NS.no); |
| 148 | + |
| 149 | +moveBrickOnTable.AddPrecondition("Moved brick is no up", ref MovedBrick, ObjectIsNoUp); //MovedBrick.IsEmptyUpSide == true |
| 150 | +moveBrickOnTable.AddPrecondition("New table is empty", ref NewStandT, ObjectIsNoUp); //NewStandT.IsEmptyUpSide == true |
| 151 | +moveBrickOnTable.AddPrecondition("Find brick bottom moved one", ref MovedBrick, ref ObjBelowMoved, FindObjBelongMovd); //MovedBrick.Size == ObjBelowMoved.HanoiObjSizeUpSide |
| 152 | +
|
| 153 | +moveBrickOnTable.AddEffect("New stand is full", ref NewStandT, NS => NS.IsEmptyUpSide, false); //NewStandT.IsEmptyUpSide = false |
| 154 | +moveBrickOnTable.AddEffect("Old stand is empty", ref ObjBelowMoved, NS => NS.IsEmptyUpSide, true); //ObjBelowMoved.IsEmptyUpSide = true |
| 155 | +moveBrickOnTable.AddEffect("UnConsociate Objs", ref ObjBelowMoved, OS => OS.HanoiObjSizeUpSide, 0); //ObjBelowMoved.HanoiObjSizeUpSide = 0 |
| 156 | +moveBrickOnTable.AddEffect("Consociate Bricks", ref NewStandT, NST => NST.HanoiObjSizeUpSide, ref MovedBrick, MB => MB.Size); //NewStandT.HanoiObjSizeUpSide = MovedBrick.Size |
| 157 | +
|
| 158 | +newDomain.AddAction(moveBrickOnTable); //Putting empty brick onto empty table spot |
| 159 | +``` |
| 160 | + |
| 161 | +Solution output for 3-bricks-hanoi-tower problem: |
| 162 | +``` |
| 163 | +Transfer bricks onto table no. 3 determined!!! Total Cost: 7 |
| 164 | +Move brick on table: Place the 1-size brick onto table no 2. |
| 165 | +Move brick on table: Place the 2-size brick onto table no 1. |
| 166 | +Move brick onto another brick: Place the 1-size brick onto 2-size brick. |
| 167 | +Move brick on table: Place the 3-size brick onto table no 2. |
| 168 | +Move brick on table: Place the 1-size brick onto table no 0. |
| 169 | +Move brick onto another brick: Place the 2-size brick onto 3-size brick. |
| 170 | +Move brick onto another brick: Place the 1-size brick onto 2-size brick. |
| 171 | +``` |
| 172 | + |
| 173 | +```mermaid |
| 174 | +
|
| 175 | +xychart-beta |
| 176 | + title "Time of making the solution" |
| 177 | + x-axis "Number of blocks" [1,2,3,4,5,6,7,8,9,10] |
| 178 | + y-axis "Time (in sec)" 0 --> 30 |
| 179 | + bar [0.29, 0.29, 0.30, 0.30, 0.39, 0.54, 1.13, 2.75, 8.14, 28.1] |
| 180 | +
|
| 181 | +``` |
0 commit comments