2
votes

I am using Gekko to solve an optimization models for job assignment, so one of the decision variable (Xijt)(i=workers, j=jobs, t=time periods) is binary whether the worker is selected to the job(x=1) or not (x=0). I got solution (The value of the objective function). However, for the variables value when use - Print(str (x)) - I got all values of x as shown in the attached picture but I want to get only the selected workers where xijt =1. Same issue with the second variables (Yijt) continues number indicates number of hours for worker i to perform job j at time t. When use - Print(str (Y)) - I got all values of Y but I want to get the Y values for only selected workers where xijt =1. Any idea about resolving that please. click here for output screenshot and for code

    model = GEKKO()
model.options.SOLVER = 1

x = {}

for i in I:
    for j in J:
        for t in T:
            x[i, j, t] = model.Var(lb=0.00, ub=1.00001,integer=True, name="x %s %s %s" % (i, j, t))
y = {}

for i in I:
    for j in J:
        for t in T:
            y[i, j, t] = model.Var(integer=False, name="y %s %s %s" % (i, j, t))

# model.update()

for l in L:
    for i in I:
        for j in J:
            for t in T:
                model.Equation(x[i, j, t] * (q[i][l] - s[j][l]) >= 0)

for j in J:
    for i in I:
        for t in T:

            model.Equation(x[i, j, t] * (lc[j][t] * y[i, j, t] *  (sum(q[i][l] / s[j][l] for l in L) / len(L))) <= uc[j][t])
            model.Equation(x[i, j, t] * (lz[j][t] * y[i, j, t] * (sum(q[i][l] / s[j][l] for l in L) / len(L))) <= uz[j][t])

for j in J:
    for t in T:
        model.Equation(sum(x[i, j, t] for i in I) == 1)

for i in I:
    for t in T:
        model.Equation(sum(x[i, j, t] for j in J) == 1)

for i in I:
    for t in T:
        model.Equation(sum(y[i, j, t] for j in J) <= 8)

objt = sum(sum(sum((x[i, j, t] * v[j] * lc[j][t]  * y[i, j, t] * (sum(q[i][l] / s[j][l] for l in L) / len(L))) - (
        x[i, j, t] * lz[j][t] * y[i, j, t] *  (sum(q[i][l] / s[j][l] for l in L) / len(L))) for j in J) for i in I) for t in
               T)

model.Obj(-objt)
model.solve(disp=True, debug=False)
print(str (x))
print(str (y))
1
Please take the effort to paste the code here - user15801675
Done, code attached now - Abdullah

1 Answers

0
votes

Use the .value[0] property of the x variable. This conditionally retrieves the y values if x is greater than a threshold value such as 0.9:

for i in I:
    for j in J:
        for t in T:
            if x[i, j, t].value[0]>=0.9:
                print(y[i, j, t])

There is better solution performance by using the Gekko m.sum() versus the Python sum() function. This shouldn't change the result but it may be much faster.

The array function is also available if you'd like to define the variables this way:

x = m.Array(m.Var,(I,J,T),integer=True,lb=0,ub=1.00001)
y = m.Array(m.Var,(I,J,T),integer=False)

It doesn't name the variables but this is not required in Gekko.