Extracting path in A* search - algorithm

I'm trying to implement A* search with the following code:
void Map::findPath(Robot robot, Model target, vector<Node> nodeList) {
Node targetNode = target.getCurrentNode();
Node startNode = robot.getCurrentNode();
vector<Node> openList;
vector<Node> closedList;
openList.push_back(startNode);
while (!openList.empty()) {
Node curNode = nodeWithLowestFScore(openList);
if (curNode.equal(targetNode)) {
/*cout << "WTF" << endl;
Node *p = curNode.getParent();
int i = 0;
while (!p->equal(startNode)) {
cout << p->getX() << " " << p->getY() << endl;
p = p->getParent();
cout << i++ << endl;
}*/
break;
}
closedList.push_back(curNode);
removeFromVector(openList, curNode);
vector<Node> adjNodes;
curNode.getWalkableAdjacentNodes(nodeList, adjNodes);
for (int i = 0; i < adjNodes.size(); i++) {
if (inVector(closedList, adjNodes[i])) {
continue;
}
if (!inVector(closedList, adjNodes[i])) {
adjNodes[i].setParent(&curNode);
cout << "Child: " <<adjNodes[i].getX() << " " << adjNodes[i].getY() << endl;
cout << "Parent: " << adjNodes[i].getParent()->getX()
<< " " << adjNodes[i].getParent()->getY() << endl;
adjNodes[i].setG(curNode.getG() + 1);
adjNodes[i].setH(adjNodes[i].getDistance(targetNode, 'm'));
adjNodes[i].setF();
openList.push_back(adjNodes[i]);
}
if (inVector(closedList, adjNodes[i])) {
if (curNode.getG() + 1 < adjNodes[i].getG()) {
adjNodes[i].setParent(&curNode);
adjNodes[i].setG(curNode.getG() + 1);
adjNodes[i].setF();
}
}
}
}
}
Here's the console output of this code(StartP = [x:-7, y:6], EndP = [x:0, y:-2]:
Child: -1 -2
Parent: 0 -2
Child: 1 -2
Parent: 0 -2
Child: -2 -2
Parent: -1 -2
Child: -1 -3
Parent: -1 -2
Child: -3 -2
Parent: -2 -2
Child: -3 -1
Parent: -3 -2
Child: -3 0
Parent: -3 -1
Child: -3 1
Parent: -3 0
Child: -4 0
Parent: -3 0
Child: -5 0
Parent: -4 0
Child: -5 1
Parent: -5 0
Child: -5 -1
Parent: -5 0
Child: -5 2
Parent: -5 1
Child: -5 3
Parent: -5 2
Child: -5 4
Parent: -5 3
Child: -5 5
Parent: -5 4
Child: -6 4
Parent: -5 4
Child: -4 4
Parent: -5 4
Child: -7 4
Parent: -6 4
Child: -8 4
Parent: -7 4
Child: -5 6
Parent: -5 5
Child: -5 7
Parent: -5 6
Child: -4 6
Parent: -5 6
Child: -3 2
Parent: -3 1
Child: -3 3
Parent: -3 2
Child: -2 2
Parent: -3 2
Child: -3 4
Parent: -3 3
Child: -4 4
Parent: -3 4
Child: -2 4
Parent: -3 4
Child: -1 4
Parent: -2 4
Child: -1 2
Parent: -2 2
Child: -3 6
Parent: -4 6
Child: -5 8
Parent: -5 7
Child: -9 4
Parent: -8 4
Child: -5 -2
Parent: -5 -1
Child: -1 -4
Parent: -1 -3
Child: 2 -2
Parent: 1 -2
Child: 3 -2
Parent: 2 -2
Child: 2 -3
Parent: 2 -2
Child: -2 -4
Parent: -1 -4
Child: -3 -4
Parent: -2 -4
Child: -3 -5
Parent: -3 -4
Child: -5 -3
Parent: -5 -2
Child: -9 5
Parent: -9 4
Child: -9 6
Parent: -9 5
Child: -8 6
Parent: -9 6
Child: -7 6
Parent: -8 6
It's a bit messy but I followed it. It reaches the starting point. (-7, 6)-> (-8, 6) -> (-9, 6) ... (-1, -2)->(0, -2).
However, if I uncomment the line that starts with "WTF" and want to back trace it it just gets into an infinite loop that prints (-7, 6) which is my EndP.
Any help is much appreciated.

Okay problematic line was this: adjNodes[i].setParent(&curNode); I just had to reallocate a new Node, copy the contents of curNode to it and set that one as parent. Algorithm was fine.

Related

Generation of a counter variable for episodes in panel data in stata [duplicate]

This question already has an answer here:
Calculating consecutive ones
(1 answer)
Closed 1 year ago.
I am trying to generate a counter variable that describes the duration of a temporal episode in panel data.
I am using long format data that looks something like this:
clear
input byte id int time byte var1 int aim1
1 1 0 .
1 2 0 .
1 3 1 1
1 4 1 2
1 5 0 .
1 6 0 .
1 7 0 .
2 1 0 .
2 2 1 1
2 3 1 2
2 4 1 3
2 5 0 .
2 6 1 1
2 7 1 2
end
I want to generate a variable like aim1 that starts with a value of 1 when var1==1, and counts up one unit with each subsequent observation per ID where var1 is still equal to 1. For each observation where var1!=1, aim1 should contain missing values.
I already tried using rangestat (count) to solve the problem, however the created variable does not restart the count with each episode:
ssc install rangestat
gen var2=1 if var1==1
rangestat (count) aim2=var2, interval(time -7 0) by (id)
Here are two ways to do it: (1) from first principles, but see this paper for more and (2) using tsspell from SSC.
clear
input byte id int time byte var1 int aim1
1 1 0 .
1 2 0 .
1 3 1 1
1 4 1 2
1 5 0 .
1 6 0 .
1 7 0 .
2 1 0 .
2 2 1 1
2 3 1 2
2 4 1 3
2 5 0 .
2 6 1 1
2 7 1 2
end
bysort id (time) : gen wanted = 1 if var1 == 1 & var1[_n-1] != 1
by id: replace wanted = wanted[_n-1] + 1 if var1 == 1 & missing(wanted)
tsset id time
ssc inst tsspell
tsspell, cond(var1 == 1)
list, sepby(id _spell)
+---------------------------------------------------------+
| id time var1 aim1 wanted _seq _spell _end |
|---------------------------------------------------------|
1. | 1 1 0 . . 0 0 0 |
2. | 1 2 0 . . 0 0 0 |
|---------------------------------------------------------|
3. | 1 3 1 1 1 1 1 0 |
4. | 1 4 1 2 2 2 1 1 |
|---------------------------------------------------------|
5. | 1 5 0 . . 0 0 0 |
6. | 1 6 0 . . 0 0 0 |
7. | 1 7 0 . . 0 0 0 |
|---------------------------------------------------------|
8. | 2 1 0 . . 0 0 0 |
|---------------------------------------------------------|
9. | 2 2 1 1 1 1 1 0 |
10. | 2 3 1 2 2 2 1 0 |
11. | 2 4 1 3 3 3 1 1 |
|---------------------------------------------------------|
12. | 2 5 0 . . 0 0 0 |
|---------------------------------------------------------|
13. | 2 6 1 1 1 1 2 0 |
14. | 2 7 1 2 2 2 2 1 |
+---------------------------------------------------------+
The approach of tsspell is very close to what you ask for, except (a) its counter (by default _seq is 0 when out of spell, but replace _seq = . if _seq == 0 gets what you ask (b) its auxiliary variables (by default _spell and _end) are useful in many problems. You must install tsspell before you can use it with ssc install tsspell.

swift2 How to parse variable spaced text

I have the following data (a portion shown below) and I'm trying to determine the best way to parse this into an array where each line would be parsed separately. The problem I'm running into is that each "column" is separated by a various number of spaces.
I have tried using .componentsSeparatedBySpaces(" ") but that doesn't give me a consistent number items in the array. I thought of using whiteSpace but some team names have 2 words in them and some have 3.
A sample of the text follows:
1 New England Patriots = 28.69 5 0 0 20.34( 13) 1 0 0 | 3 0 0 | 28.68 1 | 28.95 1 | 28.66 2
2 Green Bay Packers = 27.97 6 0 0 17.80( 28) 1 0 0 | 1 0 0 | 27.47 2 | 28.73 2 | 29.01 1
3 Denver Broncos = 26.02 6 0 0 19.02( 23) 0 0 0 | 2 0 0 | 25.21 5 | 27.25 3 | 27.98 3
4 Cincinnati Bengals = 25.96 6 0 0 19.91( 18) 1 0 0 | 3 0 0 | 25.71 4 | 26.38 4 | 26.36 4
5 Arizona Cardinals = 25.01 4 2 0 18.05( 27) 0 1 0 | 0 1 0 | 26.47 3 | 24.17 6 | 23.37 7
6 Pittsburgh Steelers = 24.87 4 2 0 21.17( 10) 1 1 0 | 1 2 0 | 25.17 6 | 24.53 5 | 24.39 5
7 Seattle Seahawks = 24.04 2 4 0 20.92( 12) 0 2 0 | 0 3 0 | 24.47 7 | 23.29 7 | 23.37 6
8 Philadelphia Eagles = 23.87 3 3 0 20.02( 17) 1 1 0 | 2 2 0 | 24.28 8 | 23.01 8 | 23.23 8
9 New York Jets = 22.95 4 1 0 18.41( 25) 0 1 0 | 0 1 0 | 23.83 9 | 22.77 10 | 21.69 11
10 Atlanta Falcons = 22.18 5 1 0 19.31( 21) 1 0 0 | 3 0 0 | 22.36 10 | 22.33 11 | 21.86 10

Image won't stop rotating in pygame

I have this section of code (similar to the previous piece of code). It's supposed to just sweep 50 degrees left, then stop and sweep 100 degrees right, then stop and sweep 100 degrees to the left, and so forth. Only problem is it stops at the left, goes right, then continues to go right, even though I have a stop there to prevent that from happening.
try:
screen.blit(Turrets[1], (TurretCoords[1]))
if Rotation == 1:
print(TurretRotation[1])
print(Rotation)
TurretRotation[1] = TurretRotation[1] - 1
Turrets[1] = pygame.transform.rotozoom(Turret, TurretRotation[1], 1)
if TurretRotation[1] == -50:
Rotation = -1
else:
print(TurretRotation[1])
print(Rotation)
TurretRotation[1] = TurretRotation[1] + 1
Turrets[1] = pygame.transform.rotozoom(Turret, TurretRotation[1], 1)
if TurretRotation[1] == 50:
Rotation = 1
Yes, I know, I was supposed to change the variables capital letters, and I haven't gotten around to it yet.
This is the output I get from the console.
-1 = Rotate
1 = TurretRotation[1]
-1
2
-1
3
-1
4
-1
5
-1
6
-1
7
-1
8
-1
9
-1
10
-1
11
-1
12
-1
13
-1
14
-1
15
-1
16
-1
17
-1
18
-1
19
-1
20
-1
21
-1
22
-1
23
-1
24
-1
25
-1
26
-1
27
-1
28
-1
29
-1
30
-1
31
-1
32
-1
33
-1
34
-1
35
-1
36
-1
37
-1
38
-1
39
-1
40
-1
41
1
40
1
39
1
38
1
37
1
36
1
35
1
34
1
33
1
32
1
31
1
30
1
29
1
28
1
27
1
26
1
25
1
24
1
23
1
22
1
21
1
20
1
19
1
18
1
17
1
16
1
15
1
14
1
13
1
12
1
11
1
10
1
9
1
8
1
7
1
6
1
5
1
4
1
3
1
2
1
1
1
0
1
-1
1
-2
1
-3
1
-4
1
-5
1
-6
1
-7
1
-8
1
-9
1
-10
1
-11
1
-12
1
-13
1
-14
1
-15
1
-16
1
-17
1
-18
1
-19
1
-20
1
-21
1
-22
1
-23
1
-24
1
-25
1
-26
1
-27
1
-28
1
-29
1
-30
1
-31
1
-32
1
-33
1
-34
1
-35
1
-36
1
-37
1
-38
1
-39
1
-40
1
-41
1
-42
1
-43
1
-44
1
-45
1
-46
1
-47
1
-48
1
-49
1
-50
1
-51
1
-52
1
-53
1
-54
1
-55
1
-56
1
-57
1
-58
1
-59
1
-60
1
-61
1
-62
1
-63
1
-64
1
-65
1
-66
1
-67
1
-68
1
-69
1
-70
1
-71
1
-72
1
-73
1
-74
1
-75
1
-76
1
-77
1
-78
1
-79
1
-80
1
-81
1
-82
1
-83
1
-84
1
I can't run it so I can only advice - I see you have print - so see what's going on with variables. Maybe one of them is changed in different place.
But you can do one this - use <= and >= - it should help.
if TurretRotation[1] >= 50:
if TurretRotation[1] <= -50:

What is "minlex order" when representing a sudoku puzzle?

For a sudoku puzzle:
000000010400000000020000000000050407008000300001090000300400200050100000000806000
in it's minlex order form:
000000001000000020000003000000040500006000300007810000010020004030000070950000000
What is the meaning of minlex order and how the above puzzle is drawn in a 9x9 layout?
Source: Minimum Sudoku 1st puzzle.
UPDATE:
Diagram 1:
0 0 0 0 0 0 0 1 0
4 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0
0 0 0 0 5 0 4 0 7
0 0 8 0 0 0 3 0 0
0 0 1 0 9 0 0 0 0
3 0 0 4 0 0 2 0 0
0 5 0 1 0 0 0 0 0
0 0 0 8 0 6 0 0 0
Diagram 2:
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 2 0
0 0 0 0 0 3 0 0 0
0 0 0 0 4 0 5 0 0
0 0 6 0 0 0 3 0 0
0 0 7 8 1 0 0 0 0
0 1 0 0 2 0 0 0 4
0 3 0 0 0 0 0 7 0
9 5 0 0 0 0 0 0 0
The puzzle is drawn from the specification by writing each number in a grid space, starting from the top left, going across the top row, and then starting on the second row from left to right until all nine rows are filled in. A zero corresponds to a blank space.
Like this:
1 2 3 4 5 6 7 8 9
+---------------+---------------+---------------+
A | . . . | . . . | . 1 . |
B | 4 . . | . . . | . . . |
C | . 2 . | . . . | . . . |
|---------------+---------------+---------------|
D | . . . | . 5 . | 4 . 7 |
E | . . 8 | . . . | 3 . . |
F | . . 1 | . 9 . | . . . |
|---------------+---------------+---------------|
G | 3 . . | 4 . . | 2 . . |
H | . 5 . | 1 . . | . . . |
I | . . . | 8 . 6 | . . . |
+---------------+---------------+---------------+
The minlex form of a Sudoku puzzle is the equivalent puzzle that sorts first in alphabetical order (from left to right). Puzzles are considered equivalent if one can be converted to the other by interchanging row and columns and renumbering without changing the puzzle.

Diag Function to create a matrix

How would I go about creating the matrix
[1 2 0 0 0;
-1 1 2 0 0;
0 -1 1 2 0;
0 0 -1 1 2;
0 0 0 -1 1]
using the diag command in MatLab?
Here is one way:
> diag(ones(1,5),0)+diag(ones(1,4),1)*2+diag(ones(1,4),-1)*-1
ans =
1 2 0 0 0
-1 1 2 0 0
0 -1 1 2 0
0 0 -1 1 2
0 0 0 -1 1
>
This just creates three diagonals at 0, +1 and -1, scales them as needed, then adds them.

Resources