I found this algorithm:
sem empty = 1;
sem full = 0;
public void send() {
P(empty)
<<inserting data>>
V(full)
}
public T receive(){
T data;
P(full)
<<data = extract()>>
V(empty);
return data;
}
To demonstrate that it is deadlock-free, the author of the book has wrote this demonstration:
-Hypothesis for absurd:
𝑣𝑎𝑙(𝑒𝑚𝑝𝑡𝑦, 𝑡) = 0 → 𝑛𝑝(𝑒𝑚𝑝𝑡𝑦, 𝑡) = 𝑛𝑣(𝑒𝑚𝑝𝑡𝑦, 𝑡) + 1
𝑣𝑎𝑙(𝑓𝑢𝑙𝑙, 𝑡) = 0 → 𝑛𝑝(𝑓𝑢𝑙𝑙, 𝑡) = 𝑛𝑣(𝑓𝑢𝑙𝑙, 𝑡)
-Then we can say that:
𝑛𝑝(𝑒𝑚𝑝𝑡𝑦, 𝑡) = 𝑛𝑣(𝑒𝑚𝑝𝑡𝑦, 𝑡) + 1 = 𝑛𝑝(𝑓𝑢𝑙𝑙) + 1 = 𝑛𝑝(𝑓𝑢𝑙𝑙) + 1 = 𝑛𝑝(𝑒𝑚𝑝𝑡𝑦) + 1 → 𝑛𝑝(𝑒𝑚𝑝𝑡𝑦) = 𝑛𝑝(𝑒𝑚𝑝𝑡𝑦) + 1 → 0 = 1.
Can you help me understand why he repeated the line 𝑛𝑝(𝑓𝑢𝑙𝑙) + 1 = 𝑛𝑝(𝑓𝑢𝑙𝑙) + 1? Is it an error or does it have substituted something that didn't affect the value?
If the value of the semaphore full would have been more than 0, should he had to put a different value than +1?
Related
Making a game in GameMaker and I'm having a problem where the variable is not set and I'm not sure why. When pressing down on either W,A,S, or D and clicking LMB it works but crashes if I pressing down on W,A,S, or D then let go and afterwards click LMB.
Code:
var look
var bullet
if (keyboard_check(ord("A"))){
x = x - 5;
sprite_index = spr_west_hiro
image_xscale = 3
image_yscale = 3
look = 180
} else if (keyboard_check(ord("D"))){
x = x + 5;
sprite_index = spr_east_hiro
image_xscale = 3
image_yscale = 3
look = 0
} else if (keyboard_check(ord("S"))){
y = y + 5;
sprite_index = spr_south_hiro
image_xscale = 3
image_yscale = 3
look = 270
} else if (keyboard_check(ord("W"))){
y = y - 5;
sprite_index = spr_north_hiro
image_xscale = 3
image_yscale = 3
look = 90
}
//------------------------------------------------------------------------------
if (keyboard_check_released(ord("A"))){
sprite_index = spr_idlewest_hiro
image_xscale = 3
image_yscale = 3
look = 180
} else if (keyboard_check_released(ord("D"))){
sprite_index = spr_idleast_hiro
image_xscale = 3
image_yscale = 3
look = 0
} else if (keyboard_check_released(ord("S"))){
sprite_index = spr_idlesouth_hiro
image_xscale = 3
image_yscale = 3
look = 270
} else if (keyboard_check_released(ord("W"))){
sprite_index = spr_idlenorth_hiro
image_xscale = 3
image_yscale = 3
look = 90
}
//--------------------------------------------------------------------------------
if (mouse_check_button_pressed(mb_left)){
var bullet = instance_create_layer(x,y, "Instances", obj_bullet)
bullet.direction = look
}
Error:
ERROR!!! :: ############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_hiro:
local variable look(100001, -2147483648) not set before reading it.
at gml_Object_obj_hiro_Step_0 (line 61) - bullet.direction = look
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_hiro_Step_0 (line 61)
I have reviewed the code multiple times and I am still stumped. Particularly because of the fact that it seems as though the variable doesn't save the coefficient set to it despite the fact that it should when W,A,S or D is pressed down then released.
Firstly, you would want to assign a value to your look variable, as otherwise it will remain not set to anything if no buttons had been pressed.
Secondly, you may want to do so in Create event, as you probably want your character to shoot in last pressed direction, not just "somewhere"
I am wondering if there is a way by which I can convert a format, let's say a tree to a format I want. Take the following example:
a -> b, d
f - > c
f-> v
I want to have this as an output:
a implies (b and d)
f implies (c or v)
This is practically the algorithm that you are looking for, but please take note that this algorithm doesn't support the deep conditions.
function mathLogic(inputs) {
var rows = inputs.split("\n");
for (var i in rows)
rows[i] = rows[i].replace(/\s/g, "");
console.log("Rows: ", rows)
var logics = {};
for (var i in rows) {
var row = rows[i];
var rowSplit = row.split("->");
var from = rowSplit[0];
var to = rowSplit[1];
var ands = to.split(",");
if (!logics[from])
logics[from] = [];
logic = logics[from];
logic.push(ands);
}
for (var i in logics) {
var implyString = i + " implies ";
var orLogic = logics[i];
var ors = [];
for (var j in orLogic) {
var or = orLogic[j]
if (or.length > 1)
ors.push("(" + or.join(" and ") + ")");
else
ors.push(or.join(" and "));
}
if (ors.length > 1)
implyString += "(" + ors.join(" or ") + ")";
else
implyString += ors.join(" or ");
console.log(implyString);
}
}
mathLogic(`a -> b, d
f - > c
f-> v`);
writing code to test the Hailstone Sequence, also called Collatz conjecture. Code will print out the number of iterations of the Hailstone sequence.
def main():
start_num = eval (input ("Enter starting number of the range: "))
end_num = eval (input ("Enter ending number of the range: "))
The main problem is that my code returns an infinite loop. I want to check all of these conditions in one statement
while (start_num > 0 and end_num > 0 and end_num > start_num):
cycle_length = 0
max_length = 0
max_number = 0
my code seems inefficient, there is probably a better way to approach the problem
for i in range(start_num, (end_num + 1)):
cycle_length = 0
while (i != 1):
if (i % 2 == 0):
i = i // 2
cycle_length += 1
if (i % 2 == 1):
i = ((3 * i) + 1)
cycle_length += 1
print (cycle_length)
I just started coding, and I always know that there is a more efficient way to approach these problems. Any suggestions on methodology, problem solving, or stylistic advice would be greatly appreciated.
Here is an answer in java. I assume that we will not start with 1.
public static void main(String[] args) {
int counter =0;
Scanner sc = new Scanner(System.in);
System.out.println("Give us a number to start with:");
int start = sc.nextInt();
System.out.println("Give us a number to end with:");
int end = sc.nextInt();
if (end > start) {
for (int i = 0; i <= end - start; i++) {
counter = 0;
int num = start + i;
int temp = num;
while(temp != 1) {
if ( temp % 2 == 0 ) {
temp = temp / 2;
} else {
temp = 3* temp +1;
}
counter++;
}
System.out.println(num + " takes " + counter + "iterations.");
}
} else {
System.out.println("Your numbers do not make sense.");
}
}
Here's an answer in python in case you're staying up late trying to solve this problem. :P Have a good night.
start_num = 1
end_num = 10
for i in range(start_num, (end_num + 1)):
cycle_length=0
num = i
while (num != 1):
if (num % 2 == 0):
num = num // 2
cycle_length+=1
else:
num = ((3 * num) + 1)
cycle_length+=1
print(cycle_length)
I wanted to find whether a given point exists in image area. Image width and height is 40. The four corners are (10,10),(50,10),(10,50),(50,50). Is there a way to find a given (x,y) exists or not in the area?
to check whether point lies inside the rect or not, i uses line equation and compare the point with all four lines.
The key to this approach is the sequence of points.
check the below code:-
function isPointInside(fourPointsArray, pointToCheck) {
var counter = 0;
var ele = pointToCheck;
var A, B, C, D, p1, p2, indx;
for (var k = 0; k < fourPointsArray.length; k++) {
p1 = fourPointsArray[k];
indx = k + 1;
if (indx >= fourPointsArray.length)
indx = 0;
p2 = fourPointsArray[indx];
A = -(p2.y - p1.y);
B = p2.x - p1.x;
C = -(A * p1.x + B * p1.y);
D = A * ele.x + B * ele.y + C;
if (D >= 0) {
counter++;
}
}
if (counter >= fourPointsArray.length) {
return true;
}
return false;
}
var arraypoints=[{x:10,y:10},{x:50,y:10},{x:50,y:50},{x:10,y:50}];
var pointtocheck={
x:5,
y:10
}
var flag=isPointInside(arraypoints,pointtocheck);
console.log(flag);
I am trying to implement Jump Point Search, an optimization on A* that reduces the number of nodes in the open list by na average of 20x, as well as memory consumption. The algorithm was implemented according to the original research paper and some online tutorials, but the program enters an infinite recursion loop when searching for natural and forced neighbors, which causes a stack overflow. Pathfinding A* is already working and I am just adding the JPS functions to it.
Here is the code for those functions:
void Movement::GetNaturalNeighbors(std::vector<Node>& output, Node& currentNode)
{
int nextX = currentNode.pos.x + currentNode.direction.x;
int nextY = currentNode.pos.y + currentNode.direction.y;
// Straight line pruning excludes all neighbors except the one directly in front of currentNode
if(!g_terrain.IsWall(nextX, nextY))
{
Map[nextX][nextY].parent = &Map[currentNode.pos.x][currentNode.pos.y];
Map[nextX][nextY].direction.x = Map[nextX][nextY].parent->direction.x;
Map[nextX][nextY].direction.y = Map[nextX][nextY].parent->direction.y;
Map[nextX][nextY].heurCost = GetHeuristicCost(nextX, nextY);
// Movement cost for diagonals is square root of 2
Map[nextX][nextY].movCost = currentNode.movCost + 1.0f;
Map[nextX][nextY].pos.x = nextX;
Map[nextX][nextY].pos.y = nextY;
if ((Map[nextX][nextY].movCost + Map[nextX][nextY].heurCost) < (currentNode.movCost + currentNode.heurCost))
{
output.push_back(Map[nextX][nextY]);
}
}
int dirX = currentNode.direction.x;
int dirY = currentNode.direction.y;
// If we are moving diagonally, add the adjacent nodes in the same direction
if(dirX!= 0 && dirY!=0)
{
if((dirY == +1) &&(!g_terrain.IsWall(currentNode.pos.x+ neighborX[TOP], currentNode.pos.y+ neighborY[TOP])))
{
int naturalNeighborX = currentNode.pos.x+ neighborX[TOP];
int naturalNeighborY = currentNode.pos.y+ neighborY[TOP];
Map[naturalNeighborX][naturalNeighborY].direction.x = neighborX[TOP];
Map[naturalNeighborX][naturalNeighborY].direction.y = neighborY[TOP];
Map[naturalNeighborX][naturalNeighborY].parent = &Map[currentNode.pos.x][currentNode.pos.y];
Map[naturalNeighborX][naturalNeighborY].heurCost = GetHeuristicCost(nextX, nextY);
// Movement cost for diagonals is square root of 2
Map[naturalNeighborX][naturalNeighborY].movCost = currentNode.movCost + 1.41421f;
Map[naturalNeighborX][naturalNeighborY].pos.x = naturalNeighborX;
Map[naturalNeighborX][naturalNeighborY].pos.y = naturalNeighborY;
if ((Map[naturalNeighborX][naturalNeighborY].movCost + Map[naturalNeighborX][naturalNeighborY].heurCost) < (currentNode.movCost + currentNode.heurCost))
{
output.push_back(Map[naturalNeighborX][naturalNeighborY]);
}
}
else if(!g_terrain.IsWall(currentNode.pos.x+ neighborX[BOTTOM], currentNode.pos.y+ neighborY[BOTTOM]))
{
int naturalNeighborX = currentNode.pos.x+ neighborX[BOTTOM];
int naturalNeighborY = currentNode.pos.y+ neighborY[BOTTOM];
Map[naturalNeighborX][naturalNeighborY].direction.x = neighborX[BOTTOM];
Map[naturalNeighborX][naturalNeighborY].direction.y = neighborY[BOTTOM];
Map[naturalNeighborX][naturalNeighborY].parent = &Map[currentNode.pos.x][currentNode.pos.y];
Map[naturalNeighborX][naturalNeighborY].heurCost = GetHeuristicCost(nextX, nextY);
// Movement cost for diagonals is square root of 2m
Map[naturalNeighborX][naturalNeighborY].movCost = currentNode.movCost + 1.41421f;
Map[naturalNeighborX][naturalNeighborY].pos.x = nextX;
Map[naturalNeighborX][naturalNeighborY].pos.y = nextY;
if ((Map[naturalNeighborX][naturalNeighborY].movCost + Map[naturalNeighborX][naturalNeighborY].heurCost) < (currentNode.movCost + currentNode.heurCost))
{
output.push_back(Map[naturalNeighborX][naturalNeighborY]);
}
}
if((dirX == +1) && (!g_terrain.IsWall(currentNode.pos.x+ neighborX[RIGHT], currentNode.pos.y+ neighborY[RIGHT])))
{
int naturalNeighborX = currentNode.pos.x+ neighborX[RIGHT];
int naturalNeighborY = currentNode.pos.y+ neighborY[RIGHT];
Map[naturalNeighborX][naturalNeighborY].direction.x = neighborX[RIGHT];
Map[naturalNeighborX][naturalNeighborY].direction.y = neighborY[RIGHT];
Map[naturalNeighborX][naturalNeighborY].parent = &Map[currentNode.pos.x][currentNode.pos.y];
Map[naturalNeighborX][naturalNeighborY].heurCost = GetHeuristicCost(nextX, nextY);
// Movement cost for diagonals is square root of 2
Map[naturalNeighborX][naturalNeighborY].movCost = currentNode.movCost + 1.41421f;
Map[naturalNeighborX][naturalNeighborY].pos.x = naturalNeighborX;
Map[naturalNeighborX][naturalNeighborY].pos.y = naturalNeighborY;
if ((Map[naturalNeighborX][naturalNeighborY].movCost + Map[naturalNeighborX][naturalNeighborY].heurCost) < (currentNode.movCost + currentNode.heurCost))
{
output.push_back(Map[naturalNeighborX][naturalNeighborY]);
}
}
else if(!g_terrain.IsWall(currentNode.pos.x+ neighborX[LEFT], currentNode.pos.y+ neighborY[LEFT]))
{
int naturalNeighborX = currentNode.pos.x+ neighborX[LEFT];
int naturalNeighborY = currentNode.pos.y+ neighborY[LEFT];
Map[naturalNeighborX][naturalNeighborX].direction.x = neighborX[LEFT];
Map[naturalNeighborX][naturalNeighborX].direction.y = neighborX[LEFT];
Map[naturalNeighborX][naturalNeighborX].parent = &Map[currentNode.pos.x][currentNode.pos.y];
Map[naturalNeighborX][naturalNeighborX].heurCost = GetHeuristicCost(nextX, nextY);
// Movement cost for diagonals is square root of 2
Map[naturalNeighborX][naturalNeighborY].movCost = currentNode.movCost + 1.41421f;
Map[naturalNeighborX][naturalNeighborY].pos.x = nextX;
Map[naturalNeighborX][naturalNeighborY].pos.y = nextY;
if ((Map[naturalNeighborX][naturalNeighborY].movCost + Map[naturalNeighborX][naturalNeighborY].heurCost) < (currentNode.movCost + currentNode.heurCost))
{
output.push_back(Map[naturalNeighborX][naturalNeighborY]);
}
}
}
}
void Movement::GetForcedNeighbors(std::vector<Node>& output, Node& currentNode)
{
int dir_x = currentNode.direction.x;
int dir_y = currentNode.direction.y;
int nextNodeX = currentNode.pos.x + dir_x;
int nextNodeY = currentNode.pos.y + dir_y;
// Forced neighbors only exist for diagonal moves
if((dir_x != 0) && (dir_y != 0))
{
// Check for diagonal forced neighbors
if(dir_x == +1)
{
// Does the top right diagonal have any forced neighbors ?
if(g_terrain.IsWall(nextNodeX + neighborX[TOP_RIGHT], nextNodeY + neighborY[TOP_RIGHT]) && !g_terrain.IsWall(nextNodeX + neighborX[TOP], nextNodeY + neighborY[TOP]))
{
Map[nextNodeX][nextNodeY].direction.x = neighborX[TOP];
Map[nextNodeX][nextNodeY].direction.y = neighborY[TOP];
Map[nextNodeX][nextNodeY].parent = &Map[currentNode.pos.x][currentNode.pos.y];
Map[nextNodeX][nextNodeY].heurCost = GetHeuristicCost(nextNodeX, nextNodeY);
// Movement cost for diagonals is square root of 2
Map[nextNodeX][nextNodeY].movCost = currentNode.movCost + 1.41421f;
Map[nextNodeX][nextNodeY].pos.x = nextNodeX;
Map[nextNodeX][nextNodeY].pos.y = nextNodeY;
if ((Map[nextNodeX][nextNodeY].movCost + Map[nextNodeX][nextNodeY].heurCost) < (currentNode.movCost + currentNode.heurCost))
{
output.push_back(Map[nextNodeX][nextNodeY]);
}
}
// Does the bottom right diagonal have any forced neighbors ?
if(g_terrain.IsWall(nextNodeX + neighborX[BOTTOM_RIGHT], nextNodeY + neighborY[BOTTOM_RIGHT]) && !g_terrain.IsWall(nextNodeX + neighborX[BOTTOM], nextNodeY + neighborY[BOTTOM]))
{
Map[nextNodeX][nextNodeY].direction.x = neighborX[BOTTOM];
Map[nextNodeX][nextNodeY].direction.y = neighborY[BOTTOM];
Map[nextNodeX][nextNodeY].parent = &Map[currentNode.pos.x][currentNode.pos.y];
Map[nextNodeX][nextNodeY].heurCost = GetHeuristicCost(nextNodeX, nextNodeY);
// Movement cost for diagonals is square root of 2
Map[nextNodeX][nextNodeY].movCost = currentNode.movCost + 1.41421f;
Map[nextNodeX][nextNodeY].pos.x = nextNodeX;
Map[nextNodeX][nextNodeY].pos.y = nextNodeY;
output.push_back(Map[nextNodeX][nextNodeY]);
}
}
if(dir_x == -1)
{
// Does the top left diagonal have any forced neighbors ?
if(g_terrain.IsWall(nextNodeX + neighborX[TOP_LEFT], nextNodeY + neighborY[TOP_LEFT]) && !g_terrain.IsWall(nextNodeX + neighborX[TOP], nextNodeY + neighborY[TOP]))
{
Map[nextNodeX][nextNodeY].direction.x = neighborX[TOP];
Map[nextNodeX][nextNodeY].direction.y = neighborY[TOP];
Map[nextNodeX][nextNodeY].parent = &Map[currentNode.pos.x][currentNode.pos.y];
Map[nextNodeX][nextNodeY].heurCost = GetHeuristicCost(nextNodeX, nextNodeY);
// Movement cost for diagonals is square root of 2
Map[nextNodeX][nextNodeY].movCost = ((dir_x == 0) || (dir_y == 0))? currentNode.movCost + 1.0f: currentNode.movCost + 1.41421f;
Map[nextNodeX][nextNodeY].pos.x = nextNodeX;
Map[nextNodeX][nextNodeY].pos.y = nextNodeY;
output.push_back(Map[nextNodeX][nextNodeY]);
}
// Does the bottom left diagonal have any forced neighbors ?
if(g_terrain.IsWall(nextNodeX + neighborX[BOTTOM_LEFT], nextNodeY + neighborY[BOTTOM_LEFT]) && !g_terrain.IsWall(nextNodeX + neighborX[BOTTOM], nextNodeY + neighborY[BOTTOM]))
{
Map[nextNodeX][nextNodeY].direction.x = neighborX[BOTTOM];
Map[nextNodeX][nextNodeY].direction.y = neighborY[BOTTOM];
Map[nextNodeX][nextNodeY].parent = &Map[currentNode.pos.x][currentNode.pos.y];
Map[nextNodeX][nextNodeY].heurCost = GetHeuristicCost(nextNodeX, nextNodeY);
// Movement cost for diagonals is square root of 2
Map[nextNodeX][nextNodeY].movCost = ((dir_x == 0) || (dir_y == 0))? currentNode.movCost + 1.0f: currentNode.movCost + 1.41421f;
Map[nextNodeX][nextNodeY].pos.x = nextNodeX;
Map[nextNodeX][nextNodeY].pos.y = nextNodeY;
output.push_back(Map[nextNodeX][nextNodeY]);
}
}
}
}
Everything else follows the logic of the original document, but these two functions are the only two I am unsure about. Both the creators and many tutorials do not explain in depth how to identify them.
My implementation of A* stores the world information in a 2D matrix called Map instead of employing a closed list. The open list is merely a vector of what nodes to open. Nodes contain their x and y positions, heuristic cost and movement cost (more commonly known as given cost) and the direction the player was facing in when he stepped on the tile. The player only moves in x and y.
I apologize for the wall of text, but this is my first post and I wanted to post as much relevant information as possible.