How to get the functionality of break in PDDL forall - artificial-intelligence

I am trying to solve the Light Up puzzle using PDDL. To implement the effect of the action placing a bulb in a cell, I have to place a bulb in (x, y) and then lit up the same row and column as long as there is no black cell. I am unable to lit up the cells till the black cell.
For example, for a 5*5 grid, if I want to place a bulb at (0,1) and there is black cell at (3, 1), I would want to lit up only cells (0,1), (1,1), (2,1) if I am only considering the column. But I would not want to lit up cell (4,1) as it is after the black cell.
So, if I use something like forall, how would I break the for all loop when it finds a black cell on it's way in a row/column?

I think it would be worth taking a look at axioms and derived predicates in PDDL. This makes it possible to build the transitive closure and derive information from other variables.
In fact, you can derive which cells have light from the information of the grid structure (which cells are black) and the light bulbs. So you can define a derived predicate lit((x,y)) for each cell and write a background theory with axioms:
lit((x,y)) <= bulb((x,y))
lit((x,y)) <= lit((x',y')) and connected((x,y),(x',y')) and ~black((x,y))
The goal formula would probably be something like: "lit((0,0)) and ... and ... lit((n,m))" for all cells that are not black.

Related

Counting Subgrids satisfying specific conditions

So I came upon an interesting algorithms problem and I'm trying to think of the most efficient solution. So there is a 2d array, and each square in the array is either red, white, or blue. We want to count the number of subrectangles of the array which contain at least one red square, no blue squares, and white squares don't matter. What is the best way to go about this?
It seems like a simple problem statement. Clearly we can iterate through all possible subgrids and test if they work, but this is very inefficient. Any help? Thanks.
You need a way to avoid duplicates; for example, you can identify a rectangle using x0,y0,x1,y1.
open, valid = empty sets of rectangles
add all red squares to a set `open`
while not `open` empty,
take the first `open` rectangle
for each direction,
try to grow it 1 square in that direction
if growth possible (not outside matrix, not blue),
and result not in `valid` or `open`,
add to `open`
add it to `valid`
The size of valid is the count you seek. Could probably be more optimal, but at least it seems correct, and should be substantially faster than looking at all possible rectangles because it a) never looks a rectangles without a red, and b) never keeps looking once a rectangle would involve a blue.
A "look at all possible rectangles" algorithm on a size-n grid would have to look at ~n^4 rectangles (back-of-the envelope, as there are n^2 x0,y0 pairs & n^2 x1,y1 pairs); and, for each rectangle, check that there is a blue and a red (so, O(n^5) is a low-ball estimate; O(n^6) may be closer to the mark). Importantly, it will have that cost regardless of distribution of red & blue. My proposal has better complexity if there are less valid results: O(n^2) if there are no reds, or if they are all surrounded by blues (checkerboard pattern). It is also of lower complexity in the worst case, as it only grows when valid -- probably around O(n^4) worst-case, defined as all-red map.

algorithm to find how many circles there are in 2d array

I'm given a 2d binary array. Some of the dots are on, some are off (1 for on, 0 for off).
I know that the "on" dots were created before by putting circles on the 2d array.
The circles are of the same radius, and each time a circle was put, the dots inside it changed to 1 instead of 0.
All the circles are within the edges of the array and dot touching the edge of the circle is lit.
An illustration can be seen below. The circles are ordered randomly and may touch.
Notice that the dots inside the circles are 1 and all other are 0.
Can you find how many circles were there just by looking at the 2d array without the circles after I had put them? Is this problem solvable?
My attempt at solving this problem was:
First, I assumed that my circles can contain dots as in the figure (radius big enough to contain 4 to 7 dots.
Then I tried to categorize what possible orientation can the circles have, however there are just a lot.
I would like to find these two circles. Notice that they can cannot overlap but can be just one near the other.
If your circles don't overlap, you can use connected component labeling algorithm and get number of circles:
NCircles = (NComponents - 1) / 2
(if inner empty regions of circles and outer empty place form separate components)
Edit: with these dots it is worth to select only connected conponents with size in some range to exclude dots and other false regions.
Simple kind of CCL suitable for this picture:
scan image until black pixel is met
do flood fill while possible, keep bounding box of scanned black pixels
if box corresponds to circle size, count it
scan further from any unmarked pixel
One more possible approach: you can try Hough algorithm for circles of predefined radius.
For example, OpenCV library contains labeling function that works with images and arrays (and Hough transform too)
Why not just generate randomly generate circles and count them?
When you insert a new circle, just check if they do not overlap.
And stop inserting new circles after you tried a certain times and failed to insert a new circle. With this last value you probably need to play a bit.
You can probably repeat this a couple of times and average the result like that.

Discover that a filled-in grid has a circle; fill in the area

Hi, I'm currently in the middle of a project where a new grid is added on to a chain of blocks on the grid every timestep. How would I be able to detect that a circle has been made in the grid? Given that all I have are the coordinates (x,y) and the color of each cell. By "circle" I mean an area that is sealed off, as shown in the picture.
Thanks in advance! By the way, I'm not asking how to click on a cell and apply the flood-fill algorithm.
The aftermath of the algorithm should produce this:
You need to split all of your white (unfilled) squares into sets of squares adjacent to each other. Start with any white square, add all of its unfilled adjacent squares to the set, and keep doing it until you've included all of the squares.
Once you have those sets, you will have a "circle" (as you named it) if there are non-empty sets that do not contain any border squares. Then to fill these sets you just change the color of each member to blue.
If you have the sets from the previous step, when you add another brick you just need to consider the set that included the affected square to see if it has been split into two sets and whether either of these new sets may be a "circle".

Grid image values to 2D array

I have a grid like the image below which I want to put in an array in this format:
;x = wall(black cells and grey boxes), s= start (red circle), g = goal(blue circle), 0 = available path(everything else)
$data[5][5] = [["x","x","x","x","x"], _
["x","s","0","x","x"], _
["x","x","0","x","x"], _
["x","x","0","g","x"], _
["x","x","x","x","x"]]
I thought I could use the colors but I'm not sure how.
looks like you have the view with fixed angles
create function that converts screen position to grid position and back. It should be easy just 2x linear interpolation. if the camera pan is not with cell based step then you need the corner point of grid lines and use that as a start point ...
for example something like this (hope I measured the pixels correctly):
x = 236 + (+(u-uh)-(v-vh))*60;
y = 133 + (-(u-uh)-(v-vh))*30;
60,30is the cell size in x,y
(236,133) is position of center of mid cell (uh,vh) in pixels
uh,vh are coordinates in your grid of center cell
add the views pan offset to (uv,hv) or (236,133) now just compute the also the reverse transform from this (u=?,v=?). Do not forget that the map is not rectangle! It is something like this:
0000x0000,
000xxx000,
00xxxxx00,
0xxxxxxx0,
xxxxxxxxx,
0xxxxxxx0,
00xxxxx00,
000xxx000,
0000x0000,
create a set of images of all objects that you can encounter
this can be done on the run, each time you do not found a match add cell to item list as new object type.
loop through all grid cell locations and compare to object types
for pixel precise rendered images you can compare directly pixels 1:1 if that is not the case the you need to compare objects more robustly. But to make a valid algorithm we need to see all the types of object you can encounter usually you can compare:
average,min and max colors, histograms,
aspect ratio,...
FFT/DCT
center of mass position,density, and more
do not forget to mask comparison to area of cell only to not include the neighboring cells on corners of bounding rectangle
[Notes]
Can not be more specific without further info

Complex depth sorting issue based on MC location

I need help rescuing a project concerning some extreme depth management. We're out of ideas!
The project concerns stacking boxes of various sizes, each box is isometric - so thats where the depth comes in to play (we need to weave this box in front of that box, but on top of two boxes and below another, etc - anything goes!)
Here's the project as it stands:
http://clearlytrained.com/depth/
You can drag the boxes then let go over the grid if the grid space is green - if you were to take the two boxes that are one grid space high, place then next to eachother, then take a wider box (2x2) and place it over both (so the gap in the two lower boxes is in the middle of the box you placed on top) you'll see that the far right lower box's depth is now higher and above the box on top. I completley understand why this happens, but no matter how we sweep through the grid in order, this will happen to some extent the way it currently stands.
The way we're currently sorting depth, is sweeping through the grid array, which stores the name of every box on screen, based on the direction we sweep through the rows and columns we get an order to which we then set each box's depth. The problem is that even though a box might take up 6 grid spaces, we don't set the depth 6 times - we only set it the first time we come to its instance name in the array - so if we're sweeping through the grid bottom to top, left to right, a box that's physically under another box, yet further to the right will always have a higher depth. not good!
The only way i can see to fix this is to figure out some sort of box to box comparison of first X then Y values, setting up some sort of double for loop/if statemenet conditions, and more or less, programatically hand place each boxe's depth every time we move or place a new box. So the complexity goes from setting depth by order of box instance name, to comparing this box to every other box, then every other box to every other box, and coming up with the real order we need to stack their depth. In some cases we might go left to right, then bottom to top, then back to left to right - there's no smooth pattern.
Please play around with the link provided, let me know if you have any other questions or need more info, I'm desperate!
I would add all boxes to an array and sort that by both X and Y value (tile X and Y). It's not a large set, so you don't really have to worry about performance.
Sort by X, then if X is the same, sort by Y. That shoooould solve it, unless I missed something obvious.

Resources