A* Heuristic implementation [closed] - algorithm

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am creating a program to solve this puzzle in the lowest cost
The sliding-title puzzle consists of three black titles, three white titles, and an empty space in the configuration shown in the Figure.
WWW_BBB
The goal of the puzzle is to get all white tiles to the right of the
black tiles while the location of the space does not matter
The puzzle has two legal moves(i.e. actions) with associated costs:
• A title may move into an adjacent empty location. – This has a step cost of
• A title can hop over one or two other tiles into the empty position.
– This has a step cost equal to the number of tiles jumped over.
I am having troubles understand how to create a Heuristic algorithm to be implemented in the algorithm.
I understand the implementation of Dijkstra's algorithm within this problem, but can't figure out how to then make it into the A* algorithm.

Assuming you want to use A* on the graph of puzzle states with edges to the states reachable through one of the two rules, then a good heuristic to use would be the number of inversions: https://en.wikipedia.org/wiki/Inversion_(discrete_mathematics)
That's the number of W,B pairs that are out of order (assuming that the relative order of same-colored tiles doesn't change). The W's and B's are in order when the number of versions is 0, and the number of inversions fixed by each type of move is less than or equal to its cost. Therefore the number of inversions as a heuristic will never overestimate the cost of the best sequence.

Related

Algorithmic question: Best angle to view trees from fixed camera [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I was asked this question in an interview, have no clue how to solve it.
"Given a fixed camera in a forest (with predefined trees), give the best angle in which the camera pictures the maximum of trees"
How would you approach it or at least what questions would you ask to get more requirements?
If trees don't obscure over trees then:
Sort all trees by angle around the camera position.
Use sliding window approach to find direction to look at.
If trees can obscure other trees then the second step is a bit trickier.
the idea is this:
convert the list of tree coordinates to a list of angles.
sort the list of angles
use a sliding window to find the starting and ending indices that maximize the number of trees.
note: because the best angle to position the camera might actually be very near the 360 degree, you need to take into account trees on the other side of the 360/0 line. The easiest way to handle that is to add duplicate trees to the list (in step 2) with a 360 shift. for example, a tree in degree 10 would be added twice, at degree 10 and 360+10. you don't actually need to add ALL the trees twice - you only really need to duplicate trees in the range 360+camera_angle, but its easy to just duplicate all the trees and it doesn't hurt.

How to find the largest circle that lies within boundaries of Polygon with genetic algorithm? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to implement code that finds the largest circle that lies within boundaries of polygon with genetic algorithm.
Does anyone ha any idea?
There probably is a known algorithm in computational geometry for doing this exactly. If you want to do it using a genetic algorithm and are satisifed with a good circle rather than an optimal circle, then that is certainly possible (although an evolutionary algorithm seems a bit more natural). Circles can be represented by triples of the form (x,y,r). Mutation operators can bump the coordinates in various ways (e.g. a normally distributed increment). Cross over would be something like e.g. (a,b,c) x (d,e,f) => (a,e,f), (d,b,c). You need an objective function. Conceptually it is area -- but it is hard to make sure that the constraints are always satisfied. What you could do is use as an objective function the area minus a penalty for each violated constraint. The penalty can be adjusted to eventually kill-off all circles which violate the constraint but shouldn't be so large that it prevents all parts of the solution space from being explored. Such parameters often need to be tweaked on a trial-and-error basis.

How to choose an algorithm? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I've to choose an algorithm for a given problem:
The game has two players: x and o. The players take alternate turns, with player x moving first at the beginning of each game.
Player x starts at position (1,1) while o starts at (8,8).
Each turn, a player can move like a queen in chess (in any of the eight directions) as long as her path does not cross a square already filled in or occupied. After moving, the space vacated by the player is designated as filled and cannot be moved to again. Notice that only the space that was occupied is filled, not the entire path.
The game ends when one player can no longer move, leaving the other player as the winner. Time given is 1 minute.
The coordinate (1 1) indicates the top left hand side of the board.
The board is specified as a list of rows. Each row is a list of entries:
is an empty square
is a filled in square
x is the current position of the x player
o is the current position of the o player
Would that be a local search algorithm or A*? It seems similar to chess, but at the same time I can't really see a goal of this game...
Loads of thanks!
This is a zero sum game, with two players, and each agent have full 'knowledge' of the world.
The general approach to handle such problems is minimax algorithm.
The algorithm idea in a nutshell is trying to find the maximal guaranteed "value" for each move. This is done by taking the "max" possibility in your turn and "min" possibility in the opponent's turn (it is assuming he is also trying to maximize his profit, so maximizing his profit is minimizing your profit).
The algorithm looks recursively into all possible game states up to certain depth, and choose the best strategy you can take.
You will likely need some heuristic function to evaluate each game state (since the algorithm is unlikely to be able to have the entire game tree, due to the large branch factor). The algorithm will use this heuristic to chose the best move for you, and for the oponent.

What is the shortest path that touches every tile in a square grid? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Suppose a ant is placed on the position (0,0) of a chess-board. That ant wants to walk through every single tile of the board, while walking the least it can to do so. What path it must follow? Is there a formula F(i) that returns the position of the ith tile on that path?
Edit: as requested, I've tried the following:
I tried googling for keywords such as "shortest path", "shortest path in square grid", but couldn't find anything relevant.
I then downloaded, configured and used a Traveling Salesman Problem solver in a square grid. Obviously, the solution wasn't satisfactory, but I could gain an insight on the problem. There is an illustration of my results:
I then, intuitively, speculated wether the answer could be something like the Hilbert Curve: . I googled about it and asked on a IRC programming channel, but I couldn't find any actual evidence this is better than spirals and similars, nor a proof this is the best possible solution.
EDIT 2: Further clarifications:
The ant can move diagonally. The distance refers to the euclidean length of the line defined by the path.
Walk in straight line, with the edge of the board on your left, until you either hit the edge of the chess board or a tile you have visited before. If you do, then take a right.
Or a thousand other obvious patterns.
Any path that takes 63 steps is the minimum and just as good as any other path.
This is going to depend on if you're taking the width of each square into consideration or is this just a double array question?
If we're talking a double-array question f(x,y), then the answer is that there is no least path because the ant will need to travel to each square f(x,y) = x*y, so f(8,8) = 64.
If we start taking the width of the tiles themselves into consideration, then the answer is somewhat different because we can use some strategies to get the least amount of distance traveled (such as starting in the center, staying by the grid separators and walking in a roughly spiral pattern r=xy^(theta)).

Find a perfect matching or proof that it's impossible [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have to either give a tiling of the figure below by dominoes, or give a proof that it is impossible.
I think to achieve this I have to find a perfect matching of the asociated graph of the figure (every space is a node of the graph and they are connected by edges in the vertical and horizontal way). So the graph is undirected and not bipartite. The number of nodes is 42, so could be possible due to there are an even number of nodes, but I think it's not possible. I thought about the definition that a graph has a perfect matching iff |V|=2·v(G) (where v(G) is the matching number of the graph).
Could you help me to find the tiling if it's exists or continue the proof that it's not possible?
According to Hall's matching theorem, if you choose any subset from one "part" of bipartite graph and the number of vertices adjacent to vertices of this subset is smaller than the subset size, there is no perfect matching.
If we choose 11 green tiles as shown below, we get only 10 adjacent tiles for them. Which means there is no perfect matching and you cannot cover the figure by dominoes.
That is impossible.
Each domino tile consists of one even and one odd square.
Blue area contains equal quantity of odd and even squares.
Yellow squares are even, green are odd.
Consider set of domino tiles that have at least one square inside Blue+Yellow area.
They may also cover some squares in green area.
But in any case it is impossible to equate number of even and odd squares for this set of domino tiles.
I chose to attack this problem using proof by cases. I haven't reached the end of each case, but the work so far suggests that every possibility leads to a dead end, so I think the tiling should be impossible. Have fun filling out the rest of the proof. =)

Resources