How to optimise grid allocation according to some constraints - optimization

Problem:
Given a grid and elements with height 1 and width 2, 3 or 4, determine if a new element with given width (w) and position ((x1,y), (x2,y)) can be allocated so that the grid has (and will have) as less empty cells as possible between existing and future elements.
Constaints:
You can't move the elements, you can only determine if an element with given position and width can be allocated
An element with width j has k probability to be allocated in the future, width 2 (high), width 3 (medium), width 4 (low)
You can't have more than 3 elements with the same x1 or x2
Minimise the number of empty cells between elements along the x axis
Example of grid:
The tricky part is that I don't know what elements will be allocated, I can only predict how the grid will be filled based on the probability logic defined above.
I'm looking for an algorithm to solve this problem, any tips appreciated.
Thanks a lot!

Suppose Grid is a 2-dim-array, initialised with Empty-Values. A Solution in Python could be:
def fitsInGrid(x1,y,w):
return all([Grid[x1+x,y] is Empty for x in range(w)])

Related

How to model fixed width columns for 2d bin/strip packing problem?

I want to create a mathematical model for 2d bin packing optimization problem. I am not quite sure if it is bin packing problem it may be called strip packing, anyway let me introduce the problem.
1- There are some group of boxes to be placed on strips (see article 3.)
2- Each group contains a number of boxes which have same width and same hight. For example,
group A
100 boxes with width = 80cm and height = 120cm
group B
250 boxes with width = 150cm and height = 200cm
etc.
3- There are unlimited number of equal sized strips which have fixed width and height, for example
infinite number of Width = 800cm and Height 1400cm
4- The main goal is packing these boxes into minimum number of the strips. However, there are some restrictions to do this job.
5- If we think of the strips as a 2d row and column plane, at each column must has a fixed width of boxes. For example, if (column 0 and row 0) has a box w=100,h=80 then (column 0 and row 1) also has to has a box w=100,h=80. It is not allowed to be in the same column for diferent sized boxes. This rule is not valid for rows. Each row can have different sized boxes, there is no restriction.
6- It is not important to fill the whole strip. We want to fill strips with minimum space between boxes. The heighest column indicates a stop line through other columns and we calculate the loss value (space ratio over the whole strip area).
I tried to implement this optimization problem with GLPK linear programming tool. I have used a mathematical model from the paper (C. Blum, V. Schmid Solving the 2D bin packing problem by means of a hybrid evolutionary algorithm)
C. Blum, V. Schmid Solving the 2D bin packing problem by means of a hybrid evolutionary algorithm
This math model works great in the GLPK. However, it is designed for boxes for packing in x,y coordinates. If you see article 5 we want them in a fixed-width column fashion.
Can you please help me to modify the mathematical model to make possible to implement article 5.
Thank you all,

imshow non unifrom matrix bin size

I am trying to create an image with imshow, but the bins in my matrix are not equal.
For example the following matrix
C = [[1,2,2],[2,3,2],[3,2,3]]
is for X = [1,4,8] and for Y = [2,4,9]
I know I can just do xticks and yticks, but I want the axis to be equal..This means that I will need the squares which build the imshow to be in different sizes.
Is it possible?
This seems like a job for pcolormesh.
From When to use imshow over pcolormesh:
Fundamentally, imshow assumes that all data elements in your array are
to be rendered at the same size, whereas pcolormesh/pcolor associates
elements of the data array with rectangular elements whose size may
vary over the rectangular grid.
pcolormesh plots a matrix as cells, and take as argument the x and y coordinates of the cells, which allows you to draw each cell in a different size.
I assume the X and Y of your example data are meant to be the size of the cells. So I converted them in coordinates with:
xSize=[1,4,9]
ySize=[2,4,8]
x=np.append(0,np.cumsum(xSize)) # gives [ 0 1 5 13]
y=np.append(0,np.cumsum(ySize)) # gives [ 0 2 6 15]
Then if you want a similar behavior as imshow, you need to revert the y axis.
c=np.array([[1,2,2],[2,3,2],[3,2,3]])
plt.pcolormesh(x,-y,c)
Which gives us:

PyGObject: How to do a Grid

Ok, I have a ScrolledWindow with inside a Viewport and a Fixed inside of that. I'm using the Builder so I'm not gonna post ALL the code if is not necessary.
I'm using a function that multiplies the given coordinates per 50, so i have a grid with 50 x 50 pixel's squares (the number of squares can variate in the config).
The real question is very simple, how I can put a background of a grid of 50 per 50 pixels? And that should be """infinite""". Preferly the lines should be of 1 px.
Note: I'm not using a grid because I need only to put Images or Icons
I used that. Mainport is the fixed element, and wres and hres are the number of squares
for i in range(self.wres):
image = gtk.Image.new_from_file("resources/Back.png")
self.mainport.put(image, i*50, 0)
for z in range(self.hres):
image2 = gtk.Image.new_from_file("resources/Back.png")
self.mainport.put(image2, i*50, z*50)

isosurface tracking in high dimensions

how to trace isosurface on a higher dimensional space efficiently
You have a scalar cost function in N dimensions,
f(y0, y1, .., yN) ∊ ℝ, y ∊ ℝ
but sampled only in a regular rectangular grid,
yk = Ψk + ψk xk, constants Ψk ∊ ℝ and ψk ∊ ℝ, and grid coordinates xk ∊ ℕ
and the problem is to locate the isosurface(s) i,
f(y0, y1, .., yN) = Ci
The direct approach would be to just loop over each cell in the grid, and check if the current isosurface intersects the current cell, and if so, describe the part of the isosurface within the current cell. (Marching Cubes is one approach to describing how the isosurface intersects each grid cell.)
The restriction here is to use a neighborhood based search instead of examining every single cell.
OP had a previous question specifically for the 3D case, to which I posted a link to example code, grid.h and grid.c (at Pastebin.com, because they were too long to include inline).
That implementation is completely different to OP's slicing method. Mine is a direct, simple walk over the grid cells intersecting the current isosurface. It caches the grid samples, and uses a separate map (one char per grid cell) to keep track which grid cells have been cached, walked, and/or pushed to a stack to be walked later. This approach is easily extended to more than three dimensions. Although the code is written for exactly three dimensions, the approach itself is not specific to three dimensions at all; all you need to do is to adjust the data structures to accommodate any (sensible) number of dimensions.
The isosurface walk itself is trivial. You start from any grid cell the isosurface intersects, then examine all 2N nearest neighbor cells to see if the isosurface intersects those too. In practice, you use a stack of grid cell locations to be examined, and a map of grid cell flags to avoid re-examining already examined grid cells.
Because the number of grid point samples per grid cell is 2N, my example code is not optimal: a lot of nearby grid points end up being evaluated to see if the neighboring grid cells do intersect the isosurface. (Instead of examining only the grid points delimiting the isosurface, grid points belonging to any grid cells surrounding the isosurface are examined.) This extra work grows exponentially as N increases.
A better approach would be to consider each of the 2N possible (N-1)-faces separately, to avoid examining cells the isosurface does not intersect at all.
In an N-dimensional regular rectangular grid, each cell is an N-dimensional cuboid, defined by the 2N grid points at the vertices (corners). The N-cuboid cells have N(N-1) two-dimensional faces, and 2N (N-1)-dimensional faces.
To examine each (N-1)-face, you need to examine the cost function at the 2N-1 grid points defining that (N-1)-face. If the cost function at those points spans the isosurface value, then the isosurface intersects the (N-1)-face, and the isosurface intersects the next grid cell in that direction also.
There are two (N-1)-faces perpendicular to each axis. If the isosurface intersects the (N-1)-face closer to negative infinity, then the isosurface intersects the next grid cell along that axis towards negative infinity too. Similarly, if the isosurface intersects the (N-1)-face closer to positive infinity, then it also intersects the next grid cell along that axis towards positive infinity too. Thus, the (N-1)-faces are perfect for deciding which neighboring cells should be examined or not. This is true because the (N-1)-face is exactly the set of grid points the two cells share.
I'm very hesitant to provide example C code, because the example code of the same approach for the 3D case does not seem to have helped anyone thus far. I fear a longer explanation with 2- and 3-dimensional example images for illustration would be needed to describe the approach in easily understandable terms; and without a firm grasp of the logic, any example code would just look like gobbledygook.
You are better using a library for 2 dimension you can try the conrec algorithm from Prof. Paul Bourke. It's similar to a marching cube.

List of CALayer and float positions

I need to create a control with a behaviour similar to UISegmentControl that shows a list of sublayers into a super layer.
The number of these sublayers can change and I have to calculate their positions.
Here the problem... if I take a superlayer with a width equal to 31 and I want to place 4 sublayers in it, I should create 4 sublayers each wide 31/4 = 7.75. So the first layer has origin.x = 0, the second has origin.x = 7.75 the third 15.5 and the last 23.25.
Obviously these positions are not valid and they'll produce blurry layers... I can't find a way to round this value being able to fill the whole superlayer and maintaining integer values for width and origin.x, someone has a solution?
You can calculate all the origins using the exact values, and then round them off to the nearest integral. Then calculate all the widths by simply subtracting the two adjacent origins (or subtracting the total width of your parent from the origin of the final sublayer). This will produce layers that completely cover your parent, with integral coordinates, but some layers will be 1 pixel wider than others.