0

I'm using or-tools to solve a CP problem. The solver gives a feasible solution as below . It means:

 X1=345, X2=437, ..., X6=1150, X7=716, X8=788...etc.

random_list=[345, 437, 801, 981, 1068, 1150, 716, 788, 860, 982, 1069, 1151, 717, 789, 861, 944, 1026, 1152, 718, 790, 862, 984, 1071, 1153, 719, 791, 863, 985, 1072, 1154, 354, 436, 622, 1073, 1155]

The adjacent elements' gap ranged from -880 to 364.

Actually I would like obtain the sorted variables as below and set the constraints to the gap of the adjacent elements. The gap should be ranged 10 to 50 for example. So is it feasible to set the constrains like this?

order_list=[345, 354, 436, 437, 622, 716, 717, 718, 719, 788, 789, 790, 791, 801, 860, 861, 862, 863, 944, 981, 982, 984, 985, 1026, 1068, 1069, 1071, 1072, 1073, 1150, 1151, 1152, 1153, 1154, 1155]

X1=345, X30=354, X31=436, X2=437,.... # the variables are not in order as definition. 

The adjacent elements' gap ranged from 1 to 185.

Thank you.

MindHacks
  • 931
  • 2
  • 7
  • 9
  • If you are only interested in solutions which are feasible only if those gaps are holding, you should explicitly encode this like: `x2 - x1 >= MIN_GAP` and `x2 - x1 <= MAX_GAP`. This will be `(n-1) * 2` constraints then. – sascha Jul 09 '21 at 10:33
  • 1
    I think you could also make a circuit and add the contraints with onlyenforceif – Stradivari Jul 09 '21 at 11:38
  • I have tried your suggestion and it works. Thank you sascha. – MindHacks Jul 12 '21 at 08:59

1 Answers1

1

Following stradivari's suggestion,

I would use a circuit constraint.

Start from a dummy node. Add one arc from the dummy node to each var. Add one arc between xi and xj iff xj is greater than xi and with the right gap. Add one arc from each node to the dummy node. Search for a feasible solution.

If you want to compute the rank, use the initial arc to set the rank of the reached var to 0. Use each arc to set the rank of head as the rank of tail + 1.

Laurent Perron
  • 8,594
  • 1
  • 8
  • 22
  • Hi Laurent, is there any code example about how to add a circuit constrain? I have searched on Internet, but can't find a good one. – MindHacks Jul 12 '21 at 09:06
  • Is it convenient to you if you can provide the main code as you suggested? I'm sorry for being new to circuit constrain. Many thanks. – MindHacks Jul 12 '21 at 09:15
  • https://github.com/google/or-tools/blob/stable/examples/python/jobshop_ft06_distance_sat.py – Laurent Perron Jul 12 '21 at 15:22