# Using single decimal for continuous variables

回答済み-
Basically, I am looking for solutions within gurobi, rather then in python.

0 -
Hi Shesha,

can the solver be told to use single decimal

No, you would have to achieve the desired behavior through modelling constructs (i.e. with additional variables and constraints).

I wasn't able to follow your example so I'm not entirely sure I understand what you are wanting to do but see my answer to a previous post here, where I show how to use variables and constraints to round up, down, or to the nearest 0.5:

https://support.gurobi.com/hc/en-us/community/posts/15340137148305/comments/15348392711441

If you want to round to 1 decimal place you can multiply the numbers by 10 so that you are rounding to the nearest integer, e.g. to round x to nearest, create a continuous variable x_nearest, and integer variable aux_int, and the following constraints

10 x - 0.5 + epsilon <= 10 x_nearest x_nearest - 0.5 <= 10 x

10 x_nearest = aux_int # ensures x_nearest takes discrete values with 1 d.p.Lastly, could you perhaps avoid all of this by reformulating so that your array sums to the number of people instead of 1?

- Riley

0 -
Thanks Riley for your response. I am sorry for not being clear.

Let me explain in little detail :)

I have set of variables that provides the percentage of people in the room, say 0.08, 0.31, 0.61 These model variables are used in other constraints so values of other model variables depend on these values.

Now, as we can see 0.08 * 17 + 0.31 * 17 + 0.61 * 17 is indeed 17 and 0.08 + 0.31 + 0.61 is 1 too. Therefore, solver will give me feasible solution in both the cases.

Now the predicament is, I need to put people in respective rooms.

round (0.08 * 17) + round (0.31 * 17) + round (0.61 * 17) = 16

floor (0.08 * 17) + floor (0.31 * 17) + floor (0.61 * 17) = 16

ceil (0.08 * 17) + ceil (0.31 * 17) + ceil (0.61 * 17) = 19

If I do some dance in python after the solver has solved to make it 17, like ceil on the minimum and floor for others then I can get 17. (ceil (0.08 * 17) + floor (0.31 * 17) + floor (0.61 * 17))

But since these values (0.08, 0.31, 0.61 ) are used for other constraints calculation, just by modifying like above messes other calculation, such weight distribution etc.

0 -
Hi Shesha,

Thanks for the detail, I understand your problem, but don't understand how restricting numbers to 1 decimal place will solve it. You have the same problem with the numbers 0.2, 0.2, 0.6 for example.

I would think more about this:

Lastly, could you perhaps avoid all of this by reformulating so that your array sums to the number of people instead of 1?

Or, in a similar theme, if the numbers in your array are a, b, c, then add integer variables A, B, C and the constraints

A + B + C = 17

A = 17 a

B = 17 b

C = 17 cso that a, b, c will take values that work. If you do this you can continue to use a, b, c in your model but I'd remove the constraint a + b + c = 1 since this is implied by these constraints.

- Riley

0 -
I can definitely model to have a constraint A + B + C = 17. But a, b, c can still be 0.08, 0.31, 0.61 and in that case, A, B, C will not be integers, right ? Will it be guaranteed that A, B, C be integers if I have a constraint A + B + C = 17.

I was thinking of the following solution, will it work ? Can model FLOOR either.

CEIL[i][j] =opt_mod.addVar(name="v0", vtype=gp.GRB.INTEGER)opt_mod.addConstr( CEIL[i][j] >= PERCENTAGE[i][j] * NPEOPLE, name="v1")opt_mod.addConstr( CEIL[i][j] -1<= (PERCENTAGE[i][j] * NPEOPLE) - EPSILON, name="v2")EPSILON is some small number = 1e-3.The above constraint is in addition to Sigma of percentages being equal to 1.0 -
Hi Shesha,

Will it be guaranteed that A, B, C be integers if I have a constraint A + B + C = 17.

No this does not guarantee A, B, C are integer. Defining A, B, C to be integer variables guarantees they are integer. Then a, b, c can only take values n/17 for n = 0, 1, 2, ..., 17.

- Riley

0 -
Thank you very much.

0

サインインしてコメントを残してください。

## コメント

7件のコメント