From c62ba7bbb6e97aba83ca7f40f1b59769a8e5bf36 Mon Sep 17 00:00:00 2001
From: Xuan Trinh <xuan.trinh@student.unimelb.edu.au>
Date: Sat, 27 Mar 2021 22:02:48 +1100
Subject: [PATCH] add more rule to rate an action{take travelled path into
 consideration} <- to avoid infiniteloop XUAN 10:00PM 27 mar 2021

---
 Test_case/level_2/14.json                     |  43 +++++++
 Test_case/level_3/13.json                     | 108 ++++++++++++++++++
 search/__pycache__/main.cpython-38.pyc        | Bin 1976 -> 2000 bytes
 search/__pycache__/search_algo.cpython-38.pyc | Bin 8742 -> 9309 bytes
 search/main.py                                |  44 +++++++
 search/search_algo.py                         |  41 ++++++-
 6 files changed, 234 insertions(+), 2 deletions(-)
 create mode 100644 Test_case/level_2/14.json
 create mode 100644 Test_case/level_3/13.json

diff --git a/Test_case/level_2/14.json b/Test_case/level_2/14.json
new file mode 100644
index 0000000..f95e9c9
--- /dev/null
+++ b/Test_case/level_2/14.json
@@ -0,0 +1,43 @@
+{
+  "upper": [
+    [
+      "r",
+      1,
+      -1
+    ]
+  ],
+  "lower": [
+    [
+      "s",
+      -3,
+      1
+    ]
+  ],
+  "block": [
+    [
+      "",
+      -1,
+      -1
+    ],
+    [
+      "",
+      -1,
+      0
+    ],
+    [
+      "",
+      -1,
+      1
+    ],
+    [
+      "",
+      0,
+      -2
+    ],
+    [
+      "",
+      0,
+      0
+    ]
+  ]
+}
\ No newline at end of file
diff --git a/Test_case/level_3/13.json b/Test_case/level_3/13.json
new file mode 100644
index 0000000..40787b5
--- /dev/null
+++ b/Test_case/level_3/13.json
@@ -0,0 +1,108 @@
+{
+  "upper": [
+    [
+      "p",
+      0,
+      -3
+    ],
+    [
+      "s",
+      0,
+      0
+    ]
+  ],
+  "lower": [
+    [
+      "r",
+      0,
+      1
+    ],
+    [
+      "p",
+      0,
+      2
+    ],
+    [
+      "r",
+      2,
+      0
+    ]
+  ],
+  "block": [
+    [
+      "",
+      -1,
+      -3
+    ],
+    [
+      "",
+      -1,
+      -2
+    ],
+    [
+      "",
+      -1,
+      -1
+    ],
+    [
+      "",
+      -1,
+      0
+    ],
+    [
+      "",
+      -1,
+      1
+    ],
+    [
+      "",
+      -1,
+      2
+    ],
+    [
+      "",
+      -1,
+      3
+    ],
+    [
+      "",
+      0,
+      3
+    ],
+    [
+      "",
+      1,
+      -4
+    ],
+    [
+      "",
+      1,
+      -3
+    ],
+    [
+      "",
+      1,
+      -2
+    ],
+    [
+      "",
+      1,
+      -1
+    ],
+    [
+      "",
+      1,
+      0
+    ],
+    [
+      "",
+      1,
+      1
+    ],
+    [
+      "",
+      1,
+      2
+    ]
+  ]
+}
\ No newline at end of file
diff --git a/search/__pycache__/main.cpython-38.pyc b/search/__pycache__/main.cpython-38.pyc
index 1ac4f3a1eb06acd9bcb68a9797aeb04de23944bd..306b913ed92d9ff2c02f03382457c5518ae3a453 100644
GIT binary patch
delta 146
zcmdnNe}SJbl$V!_fq{XclsP`(lfp*6#mtOelQ%KzF;+}|$ehQL$xy;N*_%a~@y6tQ
z7DdKilP9uRYO>X^*f11ss$q6vh?S}3EMZ^3QNvlnB*{?2S;8#IkjAu-k&$5nW1+_8
z&n#t(jGU9R*kt*lc*-)1GfU!&Qj_zGQYN>v^)OmZ7GYOp7GdO?tiUcl*_S;V0BV0H
AS^xk5

delta 145
zcmcb>zk{DIl$V!_fq{X+?pIvGMe&V%i<udnCU0WaV=S2bkU4L1IEy^vg~{bCij1Eo
z&t$RGWUXPbVJKWx!|cKkD^tr^!nT0DhO>l8lA(sPgjtdyjcFkxBf|v7LWRx0S;`pY
zIHFiWA_H7+F&CE<MKK5YhexqF`}>6Y`W5jrFfdG>%htnaF<FaUkx^{2AA1u3tIsE~

diff --git a/search/__pycache__/search_algo.cpython-38.pyc b/search/__pycache__/search_algo.cpython-38.pyc
index 9c6190551c3a2812e7abaca1b7123fa02b9eb34a..7f185dc34de8bb722305f1df201ef8f424c48ac6 100644
GIT binary patch
delta 1403
zcmZ4Ha@T_|l$V!_fq{Y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n`r`LkrYVD=6=C7jG}C}n2S?O
zij)`_7>X1@?qp6(Ntv7|EX8OzxklKY%YcD_VIjzFlgUSgHLcm0Wtg}a1(*aFSr~bk
zia@f-%pea#F&hH|0|+~Vv`k=NU`S!CWv*dxVTcWyd_ly=EQP6-xsaoVxu~avsfMwc
zv4p9HshKgGrKkt2NUWBnhNXrno2{s+u%v{!hNYR2k)g0;bCf7EBL@rAm6Nl?ay4&)
zERX})%Fe>b!6?Vb!6?MY27xRhj2uiHj7&^iOhs0knZ)liF`7+&B&o_~%D}+zonbPs
zln$fmW-BQb7DnmK@p2cK7;PpSC_2a%se%H4zqFtrwa6tixg;PnH957oNCOl$CX;6=
z2C!N(Ffc?<ey^y_4r2Lf%1@S2_I9-aX=4uZ55L9c?C%rm>sMq4(qa!H%s_-AhyVpi
zkt>Mh1R}tu8iQCaAcAZ1Mr8>VeGt<KM1VrJ$O>$5W?o4V$mK=4APLLK-<8!E%_hsK
csE9Iih;Z<7$aBbYh;xW>Sa8Vksc<*|0I*rYLI3~&

diff --git a/search/main.py b/search/main.py
index d463cb9..adf2170 100644
--- a/search/main.py
+++ b/search/main.py
@@ -76,6 +76,7 @@ def parse_input(data):
             nums = nums + 1
             keyWrite = "S" + str(nums)
         upperDictPieces[keyWrite] = (piece[ROW], piece[COLUMN])
+        visit_record[keyWrite] = {}
 
     # parse the Lower player's token
     nump, numr, nums = 0, 0, 0
@@ -146,3 +147,46 @@ def parse_input(data):
     #          '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'
     #             |     |     |     |     |     |
     #             '-._.-'-._.-'-._.-'-._.-'-._.-'
+    # Situation 2.B (B are blocks)
+    # how to tell if there are no way to reach the target
+    #
+    #              .-'-._.-'-._.-'-._.-'-._.-'-.
+    #             |     |     |     |     |     |
+    #           .-'-._.-'-._.-'-._.-'-._.-'-._.-'-.
+    #          |     |     |     |     |     |     |
+    #        .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-.
+    #       |     |     |     |     |  r  |     |     |
+    #     .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-.
+    #    |  B  |  B  |  B  |  B  |  B  |  B  |  B  |     |
+    #  .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-.
+    # |  B  |  P  |     |     |  S  |  r  |  p  |  B  |     |
+    # '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'
+    #    |  B  |  B  |  B  |  B  |  B  |  B  |  B  |     |
+    #    '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'
+    #       |     |     |     |     |     |     |     |
+    #       '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'
+    #          |     |     |     |     |     |     |
+    #          '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'
+    #             |     |     |     |     |     |
+    #             '-._.-'-._.-'-._.-'-._.-'-._.-'
+    # Scenario 3:
+    # can cause a infinite loops.
+    #              .-'-._.-'-._.-'-._.-'-._.-'-.
+    #             |     |     |     |     |     |
+    #           .-'-._.-'-._.-'-._.-'-._.-'-._.-'-.
+    #          |     |     |     |     |     |     |
+    #        .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-.
+    #       |     |     |     |     |     |     |     |
+    #     .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-.
+    #    |     |     |  R1 |     |     |     |     |     |
+    #  .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-.
+    # |     |     |  B  |     |  B  |     |     |     |     |
+    # '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'
+    #    |     |     |  B  |  B  |  B  |     |     |     |
+    #    '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'
+    #       |     |     |     |     |     |     |     |
+    #       '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'
+    #          |     |     |  s1 |     |     |     |
+    #          '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'
+    #             |     |     |     |     |     |
+    #             '-._.-'-._.-'-._.-'-._.-'-._.-'
diff --git a/search/search_algo.py b/search/search_algo.py
index 5b9dfef..9c6415f 100644
--- a/search/search_algo.py
+++ b/search/search_algo.py
@@ -19,6 +19,14 @@ targetDict = {}
 targeted_l_piece = set()
 setBlocks = set()
 
+# to handle the issue of hit dead end by just choose the minimal action.
+# { piece_key: {
+#       (row, column) : #visit
+#       ...
+#       }
+# }
+# each time a token visit a tile: mark it , the more time it visit a tile, the less appeal it is .
+visit_record = {}
 
 def make_board(lowerPieces, upperPieces, setBlocks):
     """
@@ -128,10 +136,10 @@ def make_priority_list_of_action(upperDict, piece, targetDict, board):
 
     # add all the swing action to queue
     position_list.extend(add_swing_action(upperDict, piece, board))
-
+    print(visit_record)
     # sort the list base on the how close it is to target
     if piece in targetDict.keys():
-        position_list.sort(key=(lambda x: distance_between(x, targetDict[piece])))
+        position_list.sort(key=(lambda x: rank_by_appeal(x, targetDict,piece)))
 
     return position_list
 
@@ -199,6 +207,8 @@ def choose_optimal_combination(possible_action, upperPieces, board, targetDict):
 
                 board[possible_action[piece][index]] = piece
                 moved = True
+
+                decrease_appeal(possible_action[piece][index], piece)
             else:
                 # check if no collision happen:
                 if piece_collision(board[possible_action[piece][index]], piece) == DRAW:
@@ -208,11 +218,22 @@ def choose_optimal_combination(possible_action, upperPieces, board, targetDict):
 
                     board[possible_action[piece][index]] = piece
                     moved = True
+
+                    decrease_appeal(possible_action[piece][index], piece)
             index += 1
 
     return upperPieces
 
 
+def decrease_appeal(position, piece):
+    global visit_record
+
+    if position not in visit_record[piece].keys():
+        visit_record[piece][position] = 1
+    else:
+        visit_record[piece][position] += 1
+    return visit_record
+
 
 def check_if_piece_hit_target(upperPieces, lowerPieces, targetDict):
     """
@@ -229,6 +250,8 @@ def check_if_piece_hit_target(upperPieces, lowerPieces, targetDict):
                    removed = removed_piece
             del targetDict[piece]
             del lowerPieces[removed]
+            # reset the visited node since piece start a new journey
+            visit_record[piece] = {}
             removed = True
     # once the piece hit it target, it is free to find a new target.
     # if there are more lower piece then which already targeted {some lower piece is not target}
@@ -383,3 +406,17 @@ def find_target(piece_key):
         targetDict[piece_key] = lowerDictPieces[targetPiece]
         targeted_l_piece.add(targetPiece)
 
+
+def rank_by_appeal(x, targetDict, piece):
+    # distance_between(x, targetDict[piece])
+    # make_priority_list_of_action(upperDict, piece, targetDict, board)
+    # the reduce would base on how close it is to its target and how many time we visit this.
+    base = distance_between(x, targetDict[piece])
+    reduce_factor = 0
+
+    if x in visit_record[piece].keys():
+        reduce_factor = visit_record[piece][x]
+
+    adjust_score = base +    reduce_factor
+    print(str(x) + ": " + str(adjust_score))
+    return adjust_score
-- 
GitLab