Discrete Optimization course notes (2) - constraint programming

Table of contents

1. Constraint Programming

2. Constraint propagation

Case1: Send More Money

3. Element constraint

Case2: Magic Series

Case3: Stable Marriages

4. Global Constraints

Case4: 8 queens

Case5: Jiugongge

 5. Symmetry breaking

Case6: Balanced Incomplete Block Designs (BIBDs)

Case7: Scene Allocation

6. Redundant constraints    

Case2: Magic Series (adding redundant constraints)

Case8: Car Sequencing problem

Case4: 8 queens (dual model)

7. Implement global constraints

Case9: Binary Knapsack(dynamic program)

Case10: Alldifferent(matching algorithm)

8. Constrained search strategy

Case11: Euler Knight(First-fail principle)

Case4: 8 queens (Variable/Value Labeling) 

Case12: The ESDD Deployment Problem(not) 

Case13: The perfect square problem(Value/variable labeling)

Case14: The magic square problem(Domain splitting)

Case8: Car Sequencing problem(Domain splitting)

Case7: Scene allocation(symmetry breaking during search)

Case14: The magic square problem(Randomization and restarts)

9. Graph Coloring Programming Homework


1. Constraint Programming

(1) Constrained programming

  • Use constraints to reduce the set of values ​​for each variable
  • remove values ​​that cannot occur in any solution
  • Exact solution instead of heuristic solution: enough time to find satisfactory and optimal solutions
  • Key point: How to use constraints to narrow the search space and exclude values ​​that cannot appear in any solution

(2) Classic problems

  • 8 Queens: Place 8 chess pieces on an 8*8 chessboard, requiring that every two pieces cannot be in the same row, column or diagonal
  • Map coloring: To color a map, it is required that adjacent countries cannot have the same color

The four-color theorem: Any map that uses only four colors can make countries with a common border different colors 

(3) The role of constraints

  • Check whether there is a feasible solution in the variable domain (Feasibility checking)
  • If there is a feasible solution, constraints can determine whether certain values ​​in the variable domain are feasible solutions (Pruning)

Domain: The domain of a variable is the set of possible values, D(x)={1,2,3} 

2. Constraint propagation

(1) Constraint propagation process

Case1: Send More Money

Take Send More Money as an example to illustrate the constraint propagation process, find the number corresponding to each letter according to the following formula, and introduce 4 auxiliary variables to represent the carry. The model is as follows, the letters take the value 0-9, the carry takes the value 0-1, each letter represents a different number, S and M are the first, cannot be 0, each column has a calculation equation

 (1) The first four constraints: the left picture is the search space, and value[M]=carry[4]=1 is obtained according to the first four constraints

 (2)约束5:carry[3] + value[S] + value[M] = value[O] + 10 * carry[4]

  • carry[3] is 0~1, value[S] is 2~9, value[M]=1, the equation on the left is 3~12
  • carry[4]=1, value[O] is 0 and 2~9, the equation on the right is 10~19
  • Take the intersection of the left and right sides as [10,11]
  • 10 <= carry[3] + value[S] + 1 <= 11,因此8 <= value[S] <= 10
  • 10 <= value[O] + 10 * carry[4] <= 11, so 0 <= value[O] <= 1, ie value[O]=0
  • Update the search space, the values ​​of the letters cannot be connected

 (3)约束6:carry[2] + value[E] + value[O] = value[N] + 10 * carry[3]

  • value[O] = 0,0 <= carry[2] <= 1,2 <= value[E] <= 9,2 <= 左边 <= 10
  • 2 <= value[N] + 10 * carry[3] <= 10, so -7 <= 10 * carry[3] <= 8, ie carry[3] = 0

 (4) Go back to constraint 5: carry[3] + value[S] + value[M] = value[O] + 10 * carry[4]

  •  carry[3] = 0, value[M] = carry[4] = 1, value[O] = 0, so value[S] = 9
  • The values ​​obtained from the latter constraints can then be applied to the previous constraints, narrowing the search space again 

(2) General form

 For integer linear constraints, there is the following general form

3. Element constraint

index an array or matrix with an expression containing variables

Case2: Magic Series

If in a sequence S=(S0,S1,S2...Sn), S(i) represents the number of times i appears in S, then S is called a Magic Series. The following is a Magic Series, S[0]=2, indicating that 0 appears twice.

Reification: Turn the constraint into a 0/1 variable. If the constraint is established, the variable takes the value 1, otherwise it takes the value 0. For each k, introduce 0/1 variable b[i], if series[i] = k, then b[i] = 1, otherwise b[i] = 0, series[k] is the sum of all values ​​of b.

Case3: Stable Marriages

Suppose there are four boys named George, Hugh, Will, and Clive, and four girls named Julia, Halle, Angelina, and Keira. Each girl (boy) has a preference level for all boys (girls). For example, for Halle, the rankings of boys are Clive, George, Will, Hugh; for Hugh, the rankings of girls are Angelina, Halle, Clive, George. When is a marriage stable? Take Hugh and Angelina as an example. If Angelina likes George more than Hugh, then George must like his spouse more than Angelina. If Hugh likes Julia more than Angelina, then Julia must like his spouse more than Hugh. Those who meet the above two conditions Marriage is stable. Modeled as follows

  • wrank[Men,Women] represents the ranking of Women for Men; mrank[Women,Men] represents the ranking of Men for Women
  • Decision variable: wife[Men] represents Men's wife, husband[Women] represents Women's husband
  • Constraints (consistent): For each man, the husband corresponding to his wife is himself, ie husband[wife[m]] = m; similarly for every woman, the wife corresponding to his husband is herself, ie wife [husband[w]] = w
  • Constraints (stable): For each m, if m prefers w to his wife (wrank[m,w] < wrank[w,wife[m]]), then for w, she prefers m to Likes her husband (mrank[w,husband[w]] < mrank[w,m]). The same is true for each w.

4. Global Constraints

A global constraint is a constraint that captures the relationship between a non-fixed number of variables

Case4: 8 queens

 For example, if all variables are required to have different values, it can be expressed by the global constraint alldifferent. In the 8 queens problem, the original model is shown on the left as shown in the figure below. A value of 1 is placed in each column, and the row is uncertain. The rows of all elements are different row[i] ≠ row[j], and the upper diagonal cannot be the same row[i] + i ≠ row[j] + j, the lower diagonal cannot be the same row[i] - i ≠ row[j] - j. Transform into a global constraint as shown on the right of the figure below, and use one alldifferent to represent the original three constraints.

 (1) Then why do you need global constraints? 

  • Global constraints enable faster detection of feasible solutions

 From the perspective of global constraints: it is required that x1, x2, and x3 cannot be the same, and there are only values ​​of 1 and 2 in the domain, so there is no feasible solution. From the perspective of element constraints: it contains three constraints x1 ≠ x2, x2 ≠ x3, x3 ≠ x1, and a satisfactory solution can be found for each constraint, so it will be judged that there is a feasible solution. That is, a global constraint and three element constraints express the same meaning, but the final result is different. The setting of constraints in constraint programming is very important.

  •  Global constraints allow more pruning of the search space

 From the perspective of global constraints: x3 ≠ 2 can be obtained. From the perspective of element constraints: for each constraint, the search space cannot be narrowed.

Case5: Jiugongge

The Jiugongge model is as follows: the first constraint represents that each row is not repeated, the second constraint represents that each column is not repeated, and the third constraint represents that 3*3 box elements are not repeated.

 According to the constraint conditions, the first determined position can be selected and put 2 (non-repetitive), and finally only two positions can be selected arbitrarily to complete the entire Jiugongge.

(2) The simplest global constraint (table constraint) 

 5. Symmetry breaking

Many problems exhibit symmetry, and it is useless to search the symmetric part of the solution space. Symmetry includes value symmetry and variable symmetry, which can be solved using symmetry breaking

Case6: Balanced Incomplete Block Designs (BIBDs)

Balanced Incomplete Block Designs (BIBDs)

  • Input: (v,b,r,k,l)
  • Output: a 0/1 matrix with v rows and b columns, each row of the matrix has r 1s, each column has k 1s, and the inner product of the matrix is ​​l, that is, the inner product value of any two row vectors is l

The following figure shows the BIBDs matrix, a 7*7 matrix, with 3 1s in each row and 3 1s in each column, and the inner product of every two rows is 1. The BIBDs matrix is ​​not unique, so swapping any two rows or columns satisfies the conditions. That is, there are many solutions. Lexicographic ordering is introduced for this purpose, a = 0 1 1 0 0 1 0, b = 1 0 1 0 1 0 0, then a <= b, otherwise if a = 1 1 1 0 0 1 0, then a >= b. Resize all rows and all columns to get the right image.

Case7: Scene Allocation

There is a group of actors Actor, a series of scenes Scenes, shooting days Days, fee[Actor] represents the daily shooting fee of each actor, each scene needs some actors, and a maximum of k scenes can be shot every day. The goal is to schedule each scene to be shot on days that minimize the total cost. In the following model, shoot[Scenes] represents the decision variable, appears[Scenes] represents the actors that need to appear in each scene, which[a in Actor] = setof(i in Scenes) member(a,appears[i]) calculates each Scenes where actors need to be involved. The objective function fee[a] * or(s in which[a]) (shoot[s]=d) calculates the shooting fees of all actors. Constraints atmost(all(i in Days) 5,Days,shoot) limit shooting k scenes per day.

The symmetry in this problem is that if all the scenes of the first day are exchanged with all the scenes of the second day, the target value of the solution is the same, that is, there is no difference between days and days. In order to eliminate the symmetry of values, it can be assumed that scene 1 can only be arranged on the first day, and scene 2 can be arranged on days 1 and 2. In this way, each scene only considers the days that have been used plus the new day.

6. Redundant constraints    

 Redundant constraints are constraints that can be omitted from a linear constraint system without changing the feasible region, capturing properties about the solution that the model does not exhibit.

  • Semantic redundancy: no solution can be ruled out
  • Computationally significant: reducing the search space

Case2: Magic Series (adding redundant constraints)

Take the Magic Series in the previous article as an example. This problem has some hidden constraints that can be mined. The value of the variable represents the number of times this number appears in the sequence, so the value is bounded. For example, it is impossible to have a series[4] = 17, sum(i in D) series[i] = n indicates that the sum of the number of occurrences of all numbers should be equal to the sequence length. If series[2] = 3, it means that 2 has appeared 3 times, and the value of 3 positions is 2, that is, 3 numbers have appeared 2 times, classified according to the number of occurrences, and the constraint can also be added as sum(i in D) i * series[i] = n. These constraints cannot affect the feasible region, but can increase the search efficiency.

  • Express properties of solutions; facilitate propagation of other constraints
  • Provides a more global perspective; incorporates constraints that already exist; improves communication

Combining existing constraints is also called Surrogate constraints

  

Case8: Car Sequencing problem

There are some cars that need to be produced on an assembly line (assembly line), and each type of car is customized with some optional options (Options) such as air conditioners, sunroofs, etc. The assembly line is moving and assembly takes time, so each production unit has a capacity constraint, for example, among 5 vehicles arriving in succession, only two vehicles can be assembled to the sunroof at most. The Car Sequencing problem is to determine the order of cars on the assembly line to satisfy the demand constraints for each set of car options and the capacity constraints for each production unit.

The model is as follows: Slots represents the position of each car, Configs represents the type of car, Options represents assembly options, demand[Configs] represents the demand of each type of car, nbCars represents the total demand, lb[Options] and ub[Options] are The upper and lower bounds of capacity constraints, requires[Options,Config] is the option to be assembled for each type of vehicle.

(1) Decision variables

  • lline[Slots]: the type of car placed in each slot
  • setup[Options,Slots]: 0/1 variable, if slot[s] has a car that requires option o

(2) Constraints

  • forall(c in Configs) sum(s in Slots) (line[s] = c) = demand[c]: car demand constraints
  • forall(s in Slots,o in Options) setup[o,s] = requires[o,line[s]]: define setup variables
  • forall(o in Options, s in 1..nbCars-ub[o]+1) sum(j in s..s+ub[o]-1) setup[o,s] <= lb[o]: capacity Constraints, for each option of each slot, each ub[Options] car assembly of the option can only have at most lb[Options]

(3) Redundancy constraints

  • forall(o in Options, i in 1..demand[o]) sum(s in 1..nbCars-i*ub[o]) setup[o,s] >= demand[o] - i*lb[o] ]: Assume that there is an option capacity constraint of 2/3, a total of 12 vehicles need to be produced, and the last 3 slots can be equipped with a maximum of 2 vehicles, which means that there must be at least 10 positions in the front to complete the option. The same idea is from the back push forward.

Take the following as an example to illustrate the process: two tables slots and setup assume that a class1 is arranged in slot1 first, and the demand of class1 is 1, so the first row and first column of the slots table are filled with ❌, and the capacity constraint of option3 is 1/3. The capacity constraint of option1 is 1/2, so option1 and option3 cannot be processed in the second column of the setup table, class5 and class6 cannot be placed in the second column of the slots table, option3 cannot be processed in the third column of the setup table, and option3 cannot be placed in the third column of the slots table class5. Assuming that class2 is arranged in slot2, and the slots table is updated, according to the redundancy constraints, at least 6 of the 10 positions of options2 in the setup table need to be arranged, so the remaining content can be released.

Case4: 8 queens (dual model)

There are many ways to build a model for a problem. For example, the decision variables are different. The two models may have complementary advantages. It is difficult to choose which one. Some constraints are easy to express in this model, but others are easier to express in another model. , the significance of the dual model is to express a problem with multiple models and connect them with constraints. In the queen problem, each column can be regarded as a decision variable, and each row can also be regarded as a decision variable. Find the row-column relationship by (row[c] = r) <=> (col[r] = c).

7. Implement global constraints

Case9: Binary Knapsack(dynamic program)

For the global constraints of the knapsack problem, dynamic programming tables can be used to find feasible solutions and pruning

  • Forward Phase: Start from x1, if you choose x1, use a solid line, otherwise use a dashed line, and traverse all variables
  • Backward Phase: The global constraint requires a value of 10-12. After removing some lines and points, x4 is all solid lines, so x4=1

Case10: Alldifferent(matching algorithm)

There are 6 variables, and each variable has a set of values. It is required to find solutions with different values ​​of all variables, which can be converted into a bipartite graph. As shown in the figure below, the vertex x of the bipartite graph is a variable, and the vertex v is a value. Each edge corresponds to the value of each variable.

(1) Feasibility: Find the maximum matching of the bipartite graph. If the maximum matching (maximum matching) size is the number of variables, there is a feasible solution

Alternating path: A path formed by alternating matching and non-matching edges

Augmenting path: a staggered path whose starting points are non-matching points

How to find the maximum matching: Start with any matching and improve this matching until there is no room for improvement. How to improve the matching: Turn the bipartite graph into a directed graph and find the augmenting path. As shown in the figure below, the initial match points from bottom to top, and the remaining sides point from top to bottom. The augmented path starts from an unmatched point x and ends at another bit-matched point v.

  • Select the unmatched point x2, find the intersection road x2-3-x5-4, change the direction of the side, and match the new x2
  • Select the unmatched point x1, find the intersection path x1-2-x4-4-x5-5, change the direction of the edge, find the maximum matching

You can use depth-first or best-first to search the augmented path, and the time complexity is O(|X|+|E|) 

 (2) Prune: If the edge (x,v) does not appear in any maximum matching, v is removed from the domain D(x) of x. There are two methods.

Naive approach: Force the edge (x,v) to stay in the match, that is, remove all other edges (x,w), and search for the maximum match. If the maximum match is smaller than the number of variables, it means that the value v should be taken from D(x) remove. This method is relatively inefficient.

Basic property (Berge, 1970): If given a maximum matching M, an edge belongs to even interleaving paths or even interlacing circles starting from unmatched points, then this edge belongs to some maximum matchings (not all). Therefore, if an edge does not meet these two conditions, it means that this edge does not belong to any maximum matching, and the corresponding value can be removed.

  • Given a matching M, turn the bipartite graph into a directed graph, but in the opposite direction, with matching edges going from top to bottom and non-matching edges going from bottom to top
  •  Search for even alternating path starting from a free vertex from an unmatched point :P
  • Search for even alternating cycles: C
  • Remove all edges not in M,P,C

 ​​​​​​​​As shown in the figure below, after prune, x4 cannot take the value 2, and x5 cannot take the value 3,4. The time complexity is O((|X| + |V|) * |E|)

8. Constrained search strategy

After setting the constraints, a search strategy is required to find the solution.

Case11: Euler Knight(First-fail principle)

First-fail principle: Try the easiest part to fail first, don't spend time on the easy part, directly tackle the hardest part. For example, among the 8 queens, the one with the least number of optional positions is given priority, and in the graph coloring problem, the country with the most neighboring countries is given priority. Use Knight to visit all positions on the chessboard exactly once. Knight knight is a chess piece in chess, similar to a horse in Chinese chess, and can only move a three-by-two "day" grid. According to the First-fail principle, start from the point with the fewest jumpable positions.

Case4: 8 queens (Variable/Value Labeling) 

Two steps: select the variable to assign; select the value of that variable

The original process: forall (r in R) iterates all queens; tryall (v in R) explores all values ​​non-deterministically, and only tries to assign one value at a time. If there is no solution after adding the constraint in the next step, return to this step again Assignment; row[r] = v adds a constraint to the constraint space.

After refining the search:

  • variable: forall(r in R) by row[r].getSize() Select the variable with the smallest domain according to the First-fail principle. After each new constraint is added, the domain of each variable will change, and each selection will keep First -fail principle.
  • value: tryall(v in R) by col[v].getSize() Choose a value that removes as much domain as possible from other variables.

Case12: The ESDD Deployment Problem(not) 

 Generalized quadratic assignment problem (generalized quadratic assignment model)

f represents the frequency of communication between components, h represents the distance between machines, x is a decision variable, indicating which machine each component is on, C is a collection of components, and there are two constraints: separation constraints separation constraints, colocation constraints Collocation constraints. Minimize the communication burden by deciding which machine to place each component on.

  • selectMax(i in C:!x[i].bound(),j in C)(f[i,j]): first select the component with the maximum communication frequency
  • tryall(n in N) by (min(l in x[j].memberOf(l)) h[n,l]) : try to arrange to the machine with the smallest distance

Case13: The perfect square problem(Value/variable labeling)

Value/variable labeling has two steps: select the value to be assigned next time; select which variable to assign this value to.

In The perfect square problem, it is necessary to put multiple squares of different sizes into a large square

Decision variables: coordinates x[Square] and y[Square] of the bottom left corner of each square

Restrictions:

  • No coordinates exceed the vertex coordinates of the large square
  • Squares cannot overlap
  • Redundant constraint: For any straight line x=p (y=p), the sum of the side lengths of the squares it passes through is equal to the side lengths of the large square

How to search for a solution using Value/variable labeling: pick an x-coordinate p, iterate over all squares, consider square i, and decide whether to place square i at position p. Repeat the above process for all x and y coordinates.

Case14: The magic square problem(Domain splitting)

Fill in the numbers in the square, each row, each column, and the diagonal and the same. The mathematical model is as shown in the lower left. When searching for the solution to this problem, strategies such as Variable/Value Labeling cannot be used. When the scale becomes larger, how to choose the allocation Variables are hard to choose. Domain splitting selects a variable to divide its domain into several sets

Case8: Car Sequencing problem(Domain splitting)

The previous model is to assign an option to each slot, and now traverse all the options to determine whether the slot accepts the option, starting from the option with the least slack time.

Case7: Scene allocation(symmetry breaking during search)

In the case of Scene allocation, in order to break the symmetry, the available days of each scene are limited. Every time a scene is selected to allocate a shooting day, a specific date is selected from the existing days plus new day according to heuristic rules (choose the date with the smallest shooting cost), so the constraints of symmetry breaking can be dynamically added to During the search process, just like when coloring a graph, every time a point is colored, the fields of other points need to be updated, and the coloring is done dynamically.

Case14: The magic square problem(Randomization and restarts)

For some problems that do not have an obvious search order, you can try a random order first, set the search time, and reset the search if no solution is found within the time limit. The following search process combines Domain splitting with Randomization and restarts

9. Graph Coloring Programming Homework

 Use networkx to solve graph coloring problems, code GitHub - bujibujibiuwang/Discrete-Optimization-Solution: The University of Melbourne Discrete Optimization Course

Guess you like

Origin blog.csdn.net/weixin_45526117/article/details/127637489