Dominion Strategy Forum

Please login or register.

Login with username, password and session length
Pages: 1 [2]  All

Author Topic: How do I even start to solve this math problem?  (Read 11362 times)

0 Members and 1 Guest are viewing this topic.

blueblimp

  • Margrave
  • *****
  • Offline Offline
  • Posts: 2849
  • Respect: +1560
    • View Profile
Re: How do I even start to solve this math problem?
« Reply #25 on: January 01, 2015, 05:18:59 pm »
0

This problem seems a little awkward to formulate as a integer program, though, isn't it? Enforcing restriction B (that the sums of subsets of size at most 3 must be unique) requires a hefty amount of "not equal"s, and doesn't formulating "not equal"s typically use big-M constraints, which hurt the efficiency of the IP solver? I don't use IP often so please correct me if I'm wrong.
Logged

blueblimp

  • Margrave
  • *****
  • Offline Offline
  • Posts: 2849
  • Respect: +1560
    • View Profile
Re: How do I even start to solve this math problem?
« Reply #26 on: January 01, 2015, 05:50:58 pm »
0

Calculating exactly how many not-equal constraints are needed:

Presumably we'd handle the distinctness of the integers themselves by putting the variables in order, say x_1 < x_2 < x_3 < x_4 < x_5 < x_6 < x_7 < x_8. That'd be expressed by constraints like x_1 + 1 <= x_2, etc.

Restriction A is x_8 <= x_1 + x_2, but because restriction B requires x_8 =/= x_1 + x_2, we can write that as x_8 + 1 <= x_1 + x_2. This handles all subsets of size 1.

All that's left is to express uniqueness of the sums of subsets of sizes 2 and 3. I don't have a better idea here than doing pairwise distinctness, such as x_2 + x_4 =/= x_1 + x_2 + x_3. When formulated in an IP using big-M, that example would become two inequalities plus an additional variable controlling which inequality is enforced.

The number of non-equality constraints needed is (1/2) * (8 choose 2) * (6 choose 2) + (8 choose 3) * (5 choose 2) + (1/2) * (8 choose 3) * (5 choose 3) = 1050. (The "1/2"s are there because subsets of equal size can be picked two ways.)
Logged

GeoLib

  • Jester
  • *****
  • Offline Offline
  • Posts: 965
  • Respect: +1265
    • View Profile
Re: How do I even start to solve this math problem?
« Reply #27 on: January 01, 2015, 08:30:14 pm »
+1

There is a fast method for optimizing an objective function subject to a systems of inequality constraints (the simplex algorithm), but doing it with integers is NP-hard (i.e. it isn't likely that MATLAB will be able to solve it for you quickly).
It is still good to remember that NP-hard does not mean slow for small problems. State-of-the-art integer programming solvers (CPLEX and others) would solve problems this small probably immediately, and more generally a lot of NP-hard problems can be solved efficiently for surprisingly large number of variables unless you start explicitly constructing the hardest possible instances. Consequently, one shouldn't automatically assume NP-hard problems to be too slow to compute. Too often people say "this is TSP, so there's no hope" whereas in practice TSPs of modest size are easy with standard solvers and even problems with tens of thousands of nodes can be solved exactly (this is naturally slow).

You're right, of course. With this particular problem though, as blueblimp demonstrated there are a lot of constraints. My guess is that trying to do something in MATLAB wouldn't be fast. I may very well be wrong.
Logged
"All advice is awful"
 —Count Grishnakh

Ratsia

  • Moneylender
  • ****
  • Offline Offline
  • Posts: 168
  • Respect: +113
    • View Profile
Re: How do I even start to solve this math problem?
« Reply #28 on: January 02, 2015, 08:53:09 am »
+1

You're right, of course. With this particular problem though, as blueblimp demonstrated there are a lot of constraints. My guess is that trying to do something in MATLAB wouldn't be fast. I may very well be wrong.
This particular problem indeed is not the most suitable one for integer programming, but I would not call 1050 constraints a very large number. I'm definitely not expert enough to say anything about how well generic solvers solve problems with very few variables but some orders of magnitude more constraints, but it does not sound too difficult. That said, the only reason I replied was the thing you agreed with. :)

Matlab wouldn't be the right tool anyway.
Logged

GeoLib

  • Jester
  • *****
  • Offline Offline
  • Posts: 965
  • Respect: +1265
    • View Profile
Re: How do I even start to solve this math problem?
« Reply #29 on: January 02, 2015, 04:46:34 pm »
+1

You're right, of course. With this particular problem though, as blueblimp demonstrated there are a lot of constraints. My guess is that trying to do something in MATLAB wouldn't be fast. I may very well be wrong.
This particular problem indeed is not the most suitable one for integer programming, but I would not call 1050 constraints a very large number. I'm definitely not expert enough to say anything about how well generic solvers solve problems with very few variables but some orders of magnitude more constraints, but it does not sound too difficult. That said, the only reason I replied was the thing you agreed with. :)

Matlab wouldn't be the right tool anyway.

Yeah, my main point was that it sounded like Stephen thought this could be solved by something like linear programming. I got too excited adding in the NP-hard part there.
Logged
"All advice is awful"
 —Count Grishnakh

blueblimp

  • Margrave
  • *****
  • Offline Offline
  • Posts: 2849
  • Respect: +1560
    • View Profile
Re: How do I even start to solve this math problem?
« Reply #30 on: January 02, 2015, 06:31:29 pm »
+1

I forgot about an IP formulation technique that can work here to avoid the big-M constraints. The idea is to have a boolean variable for each of the cross product {possible sums} x {subsets of size 2 or 3}. You have to cap the possible sums at some maximum value, but that's okay since we're looking for small examples. An example such variable would be e_{83, {x_2, x_4}}.

You'd have constraints like this to enforce that every sum only gets used once:
e_{83, {x_1, x_2}} + e_{83, {x_1, x_3}} + ... + e_{83, {x_6, x_7, x_8}} <= 1

Constraints like this set the sum of each subset:
x_2 + x_4 = 1 * e_{1, {x_2, x_4}} + 2 * e_{2, {x_2, x_4}} + ... + 300 * e_{300, {x_2, x_4}}

Constraints like this ensure that each subset has exactly one sum:
e_{1, {x_2, x_4}} + e_{2, {x_2, x_4}} + ... + e_{300, {x_2, x_4}} = 1

The number of variables is quite large, but I think it should be easier for an IP solver than a big-M formulation because the LP relaxation is nicer. I don't have access to an IP solver license currently so I can't really test this out. As far as I know, free IP solvers are not very good, so trying it out there would not be very representative.
Logged

blueblimp

  • Margrave
  • *****
  • Offline Offline
  • Posts: 2849
  • Respect: +1560
    • View Profile
Re: How do I even start to solve this math problem?
« Reply #31 on: January 03, 2015, 07:41:41 pm »
0

Alright, I generated the formulation that I described in the previous post and ran it through SCIP, which claims to be the best free IP solver out there. After 8 hours 23 minutes, it pulled through to find a feasible solution:
Code: [Select]
x1                                                 37 (obj:1)
x2                                                 72 (obj:1)
x3                                                 83 (obj:1)
x4                                                 94 (obj:1)
x5                                                 97 (obj:1)
x6                                                 99 (obj:1)
x7                                                100 (obj:1)
x8                                                101 (obj:1)


SCIP> display solution

objective value:                                  683
e_109_12                                            1 (obj:0)
e_120_13                                            1 (obj:0)
e_131_14                                            1 (obj:0)
e_134_15                                            1 (obj:0)
e_136_16                                            1 (obj:0)
e_137_17                                            1 (obj:0)
e_138_18                                            1 (obj:0)
e_155_23                                            1 (obj:0)
e_166_24                                            1 (obj:0)
e_169_25                                            1 (obj:0)
e_171_26                                            1 (obj:0)
e_172_27                                            1 (obj:0)
e_173_28                                            1 (obj:0)
e_177_34                                            1 (obj:0)
e_180_35                                            1 (obj:0)
e_182_36                                            1 (obj:0)
e_183_37                                            1 (obj:0)
e_184_38                                            1 (obj:0)
e_191_45                                            1 (obj:0)
e_192_123                                           1 (obj:0)
e_193_46                                            1 (obj:0)
e_194_47                                            1 (obj:0)
e_195_48                                            1 (obj:0)
e_196_56                                            1 (obj:0)
e_197_57                                            1 (obj:0)
e_198_58                                            1 (obj:0)
e_199_67                                            1 (obj:0)
e_200_68                                            1 (obj:0)
e_201_78                                            1 (obj:0)
e_203_124                                           1 (obj:0)
e_206_125                                           1 (obj:0)
e_208_126                                           1 (obj:0)
e_209_127                                           1 (obj:0)
e_210_128                                           1 (obj:0)
e_214_134                                           1 (obj:0)
e_217_135                                           1 (obj:0)
e_219_136                                           1 (obj:0)
e_220_137                                           1 (obj:0)
e_221_138                                           1 (obj:0)
e_228_145                                           1 (obj:0)
e_230_146                                           1 (obj:0)
e_231_147                                           1 (obj:0)
e_232_148                                           1 (obj:0)
e_233_156                                           1 (obj:0)
e_234_157                                           1 (obj:0)
e_235_158                                           1 (obj:0)
e_236_167                                           1 (obj:0)
e_237_168                                           1 (obj:0)
e_238_178                                           1 (obj:0)
e_249_234                                           1 (obj:0)
e_252_235                                           1 (obj:0)
e_254_236                                           1 (obj:0)
e_255_237                                           1 (obj:0)
e_256_238                                           1 (obj:0)
e_263_245                                           1 (obj:0)
e_265_246                                           1 (obj:0)
e_266_247                                           1 (obj:0)
e_267_248                                           1 (obj:0)
e_268_256                                           1 (obj:0)
e_269_257                                           1 (obj:0)
e_270_258                                           1 (obj:0)
e_271_267                                           1 (obj:0)
e_272_268                                           1 (obj:0)
e_273_278                                           1 (obj:0)
e_274_345                                           1 (obj:0)
e_276_346                                           1 (obj:0)
e_277_347                                           1 (obj:0)
e_278_348                                           1 (obj:0)
e_279_356                                           1 (obj:0)
e_280_357                                           1 (obj:0)
e_281_358                                           1 (obj:0)
e_282_367                                           1 (obj:0)
e_283_368                                           1 (obj:0)
e_284_378                                           1 (obj:0)
e_290_456                                           1 (obj:0)
e_291_457                                           1 (obj:0)
e_292_458                                           1 (obj:0)
e_293_467                                           1 (obj:0)
e_294_468                                           1 (obj:0)
e_295_478                                           1 (obj:0)
e_296_567                                           1 (obj:0)
e_297_568                                           1 (obj:0)
e_298_578                                           1 (obj:0)
e_300_678                                           1 (obj:0)
x1                                                 37 (obj:1)
x2                                                 72 (obj:1)
x3                                                 83 (obj:1)
x4                                                 94 (obj:1)
x5                                                 97 (obj:1)
x6                                                 99 (obj:1)
x7                                                100 (obj:1)
x8                                                101 (obj:1)
Compare to the 12 seconds that the hand-coded search took. I assume the IP solving time could be brought down by manual tweaking the branching strategy, etc., since the hand-coded search is basically just branching on the x variables and pruning by checking for duplicated set sums, and maybe the IP solver is smart enough to apply the same pruning. At that point though, it's not any easier than coding by hand. (Actually, already the code to generate the problem file for SCIP is nearly as long as the code to straight-up solve the problem.)

I think the sweet spot for IP is more when finding feasible solutions is easy but finding an optimal solution is hard.
« Last Edit: January 03, 2015, 07:47:48 pm by blueblimp »
Logged

GeoLib

  • Jester
  • *****
  • Offline Offline
  • Posts: 965
  • Respect: +1265
    • View Profile
Re: How do I even start to solve this math problem?
« Reply #32 on: January 03, 2015, 08:09:20 pm »
0

Alright, I generated the formulation that I described in the previous post and ran it through SCIP, which claims to be the best free IP solver out there. After 8 hours 23 minutes, it pulled through to find a feasible solution:
Code: [Select]
x1                                                 37 (obj:1)
x2                                                 72 (obj:1)
x3                                                 83 (obj:1)
x4                                                 94 (obj:1)
x5                                                 97 (obj:1)
x6                                                 99 (obj:1)
x7                                                100 (obj:1)
x8                                                101 (obj:1)


SCIP> display solution

objective value:                                  683
e_109_12                                            1 (obj:0)
e_120_13                                            1 (obj:0)
e_131_14                                            1 (obj:0)
e_134_15                                            1 (obj:0)
e_136_16                                            1 (obj:0)
e_137_17                                            1 (obj:0)
e_138_18                                            1 (obj:0)
e_155_23                                            1 (obj:0)
e_166_24                                            1 (obj:0)
e_169_25                                            1 (obj:0)
e_171_26                                            1 (obj:0)
e_172_27                                            1 (obj:0)
e_173_28                                            1 (obj:0)
e_177_34                                            1 (obj:0)
e_180_35                                            1 (obj:0)
e_182_36                                            1 (obj:0)
e_183_37                                            1 (obj:0)
e_184_38                                            1 (obj:0)
e_191_45                                            1 (obj:0)
e_192_123                                           1 (obj:0)
e_193_46                                            1 (obj:0)
e_194_47                                            1 (obj:0)
e_195_48                                            1 (obj:0)
e_196_56                                            1 (obj:0)
e_197_57                                            1 (obj:0)
e_198_58                                            1 (obj:0)
e_199_67                                            1 (obj:0)
e_200_68                                            1 (obj:0)
e_201_78                                            1 (obj:0)
e_203_124                                           1 (obj:0)
e_206_125                                           1 (obj:0)
e_208_126                                           1 (obj:0)
e_209_127                                           1 (obj:0)
e_210_128                                           1 (obj:0)
e_214_134                                           1 (obj:0)
e_217_135                                           1 (obj:0)
e_219_136                                           1 (obj:0)
e_220_137                                           1 (obj:0)
e_221_138                                           1 (obj:0)
e_228_145                                           1 (obj:0)
e_230_146                                           1 (obj:0)
e_231_147                                           1 (obj:0)
e_232_148                                           1 (obj:0)
e_233_156                                           1 (obj:0)
e_234_157                                           1 (obj:0)
e_235_158                                           1 (obj:0)
e_236_167                                           1 (obj:0)
e_237_168                                           1 (obj:0)
e_238_178                                           1 (obj:0)
e_249_234                                           1 (obj:0)
e_252_235                                           1 (obj:0)
e_254_236                                           1 (obj:0)
e_255_237                                           1 (obj:0)
e_256_238                                           1 (obj:0)
e_263_245                                           1 (obj:0)
e_265_246                                           1 (obj:0)
e_266_247                                           1 (obj:0)
e_267_248                                           1 (obj:0)
e_268_256                                           1 (obj:0)
e_269_257                                           1 (obj:0)
e_270_258                                           1 (obj:0)
e_271_267                                           1 (obj:0)
e_272_268                                           1 (obj:0)
e_273_278                                           1 (obj:0)
e_274_345                                           1 (obj:0)
e_276_346                                           1 (obj:0)
e_277_347                                           1 (obj:0)
e_278_348                                           1 (obj:0)
e_279_356                                           1 (obj:0)
e_280_357                                           1 (obj:0)
e_281_358                                           1 (obj:0)
e_282_367                                           1 (obj:0)
e_283_368                                           1 (obj:0)
e_284_378                                           1 (obj:0)
e_290_456                                           1 (obj:0)
e_291_457                                           1 (obj:0)
e_292_458                                           1 (obj:0)
e_293_467                                           1 (obj:0)
e_294_468                                           1 (obj:0)
e_295_478                                           1 (obj:0)
e_296_567                                           1 (obj:0)
e_297_568                                           1 (obj:0)
e_298_578                                           1 (obj:0)
e_300_678                                           1 (obj:0)
x1                                                 37 (obj:1)
x2                                                 72 (obj:1)
x3                                                 83 (obj:1)
x4                                                 94 (obj:1)
x5                                                 97 (obj:1)
x6                                                 99 (obj:1)
x7                                                100 (obj:1)
x8                                                101 (obj:1)
Compare to the 12 seconds that the hand-coded search took. I assume the IP solving time could be brought down by manual tweaking the branching strategy, etc., since the hand-coded search is basically just branching on the x variables and pruning by checking for duplicated set sums, and maybe the IP solver is smart enough to apply the same pruning. At that point though, it's not any easier than coding by hand. (Actually, already the code to generate the problem file for SCIP is nearly as long as the code to straight-up solve the problem.)

I think the sweet spot for IP is more when finding feasible solutions is easy but finding an optimal solution is hard.

And this is worse than the one you gave before. That sum was 528.
Logged
"All advice is awful"
 —Count Grishnakh

blueblimp

  • Margrave
  • *****
  • Offline Offline
  • Posts: 2849
  • Respect: +1560
    • View Profile
Re: How do I even start to solve this math problem?
« Reply #33 on: January 03, 2015, 08:50:42 pm »
0

Yeah, I should have mentioned that this run was trying to minimize the sum of the answer, instead of lexicographic minimization like before, because it would have made the objective function more awkward to do lexicographic minimization. I killed it rather than letting it run long enough to find an optimal solution, because ~100min after finding a feasible solution, it hadn't found any better one.
Logged
Pages: 1 [2]  All
 

Page created in 0.05 seconds with 22 queries.