开发者

Allocating resources according to rules -- is simulated annealing appropriate?

开发者 https://www.devze.com 2023-02-12 21:22 出处:网络
I would like to design an application that can allocate resources according to rules. I believe simulated annealing would work, but I am not too familiar with it and I was wondering if there were alte

I would like to design an application that can allocate resources according to rules. I believe simulated annealing would work, but I am not too familiar with it and I was wondering if there were alternative algorithms that might be suitable.

For example if I had a grid, and I could color each cell in the grid, I would like to design an algorithm that would find an optimum or close-to-optimum solution for a rule set like the following:

  • 1000x1000 grid
  • Must place 500 red cells, 500 blue cells, and 1000 green cells
  • A red cell must be touching another red cell
  • A blue cell must not be touching another blue cell
  • A green cell may only be placed along the edge
  • Arrangements can be scored based on the mean distance of the colored cells from the upper-left hand corner

Would simulated annealing be appropriate for this problem? I need an algorithm which can compute a solution reliably quickly (seconds to m开发者_如何转开发inutes).


Simulated annealing will get close to the optimal solution pretty fast. However, implementing simulated annealing correctly (which isn't that much code) can be very challenging. Many people (including myself in the past) implement it wrong, think they did it right and presume that the algorithm just isn't that good.

Alternatives algorithms are tabu search, genetic algorithms, simplex, ...

Here's how your constraints would like in Drools Planner (java, open source, ASL):

rule "A red cell must be touching another red cell"
when
   // There is a cell assignment of color red
   $a1: CellAssignment(color = RED, $x1 : x, $y1 : y)
   // There is no other red cell a neighbor of it
   not CellAssignment(color = RED, eval(MyDistanceHelper.distance(x, y, $x1, $y1) == 1))
then
   insertLogical(new IntConstraintOccurrence(
            "A red cell must be touching another red cell", 
            ConstraintType.NEGATIVE_HARD, 1, // weight 1
            $a1));
end

rule "A blue cell must not be touching another blue cell"
when
   // There is a cell assignment of color blue
   $a1: CellAssignment(color = BLUE, $x1 : x, $y1 : y)
   // There is another blue cell a neighbor of it
   $a2: CellAssignment(color = BLUE, eval(MyDistanceHelper.distance(x, y, $x1, $y1) == 1))
then
   insertLogical(new IntConstraintOccurrence(
            "A blue cell must not be touching another blue cell", 
            ConstraintType.NEGATIVE_HARD, 1, // weight 1
            $a1, $a2));
end

...

Now the fun part is: once you have you score rules, you can throw several algorithms (tabu search, simulated annealing, ...) on it (see Benchmarker support) and use the best one in production. More information in the reference manual.


This is basically a constraint satisfaction problem, I would not expect simulated annealing to get anywhere close to optimal in a reasonable amount of time. However, it might get you to a suboptimal solution quite quickly, so you could potentially terminate whenever you are out of time.

That said, if you want to solve this optimally the best way by far would be to use a CSP solver of some sort. I coded this up in IBM CPLEX OPL, which get's compiled into an integer linear program (ILP) and solved by the CPLEX solver. If you are in academia you can get a free copy of CPLEX and if you are not you can do a very similar thing in GLPK. You can also terminate many ILP solvers after a fixed amount of time and get the best solution so far.

Also, there are a number of speed ups you can do for this particular set up. First of all the green nodes can just be removed from the problem, they are always lined up along the top and left edges and if you will always have such a large number of green compared to red/blue it would never make sense to put a blue or red on those edges. These changes allowed the solver, for a 1000x1000 grid, to get within 7% of optimal in under 10 seconds, but is still hasn't found the actual optimal assignment after 15 minutes.

Here is the OPL code for a 100x100 grid with 100 green, 50 red, 50 blue, if you are interested.

using CPLEX;
dvar int grid[0..102][0..102][0..2] in 0..1;
minimize (sum(i in 1..101, j in 1..101, k in 0..2) grid[i][j][k]*(i*i + j*j));

subject to {

// edge conditions so I can always index i-1 and i+1 in all cases
forall(i in 0..102) (sum(j in 0..2) (grid[i][0][j] + grid[i][102][j])) == 0;
forall(i in 0..102) (sum(j in 0..2) (grid[0][i][j] + grid[102][i][j])) == 0;

// only one color per cell
forall(i in 1..101, j in 1..101)
    (sum(k in 0..2) grid[i][j][k]) <= 1;

// 50 red
sum(i in 1..101, j in 1..101) grid[i][j][0] == 50;

// 100 green
sum(i in 1..101, j in 1..101) grid[i][j][1] == 100;

// 50 blue
sum(i in 1..101, j in 1..101) grid[i][j][2] == 50;

// green must be on the edge (not on not-edge)
forall(i in 2..100, j in 2..100)
    grid[i][j][1] == 0;

// red must be next to another red
forall(i in 1..101, j in 1..101)
    (1 - grid[i][j][0]) + grid[i+1][j][0] + grid[i-1][j][0] + grid[i][j+1][0] + grid[i][j-1][0] >= 1;

// blue cannot be next to another blue
forall(i in 1..101, j in 1..101)
    (1-grid[i][j][2]) + (1-grid[i+1][j][2]) >= 1;
forall(i in 1..101, j in 1..101)
    (1-grid[i][j][2]) + (1-grid[i-1][j][2]) >= 1;
forall(i in 1..101, j in 1..101)
    (1-grid[i][j][2]) + (1-grid[i][j+1][2]) >= 1;
forall(i in 1..101, j in 1..101)
    (1-grid[i][j][2]) + (1-grid[i][j-1][2]) >= 1;
}

Here is the optimal placement it solved in about 10 seconds on a 3.05Ghz machine

G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G 
G B R R R B R R R B R B _ B _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
G R B R B R B R B R R _ B _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
G R R B R R R B R B R B _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
G R B R B R B R R R B _ B _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
G B R R R B R B R B _ B _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
G R B R B R R R B _ B _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
G R R B R B R B _ B _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
G R B R R R B _ B _ B _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _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