简体   繁体   中英

Google Or-Tools manipulating variables before solver

I am new to OR-Tools and I am trying to solve an optimisation problem with 12 variables (masochism, I know).

Each variable represents how much time each person (out of 2) should work on a particular device model (out of 2) each day (out of 3). New devices arrive each day and there are many devices in the backlog that arrived several days ago. Each device needs to be processed within 2 days of arrival.

Basically, I want to maximise the proportion of devices that get processed within 2 days of arrival out of the total number of devices (some of them take more than 2 days because there are not enough people and time).

I have a table like this that shows which device model arrives when (day -2 means it arrived the day before yesterday, day 0 is today):

    item    arrival day
0   ITEM 1  -2
1   ITEM 1  -2
2   ITEM 1  -2
3   ITEM 1  -2
18  ITEM 1  2
17  ITEM 1  1
16  ITEM 1  1
14  ITEM 1  0
...

My goal is to work out how many days each device takes to be processed, so I want to add a column to this table that shows on which day that item gets processed, then another column showing how many days that took ('age'), then another showing if it took more than 2 days:

    item    arrival day  processed date  age  success (within 2 days)?
0   ITEM 1  -2               0            2           1
1   ITEM 1  -2               0            2           1
2   ITEM 1  -2               1            3           0
3   ITEM 1  -2               2            4           0
18  ITEM 1  2                2            0           1
17  ITEM 1  1                2            1           1
16  ITEM 1  1                2            1           1
14  ITEM 1  0                1            1           1

In this example there are 6 devices that were processed within 2 days out of 8 total, so the success rate is 6/8. I want to maximise this success rate.

I don't know how to create the objective function for this in OR-Tools. I tried to recreate this table as a dataframe and then have the objective function as the sum of the 1's in the last column, but the processed date column depends on the variables in the optimisation model, so I can't create this table before the model is solved.

I tried this (where a10, b10, a11, b11, a12, b12 are my variables and s1a, s1b are constants). What doesn't work:

# define variables
a10 = model.NewIntVar(0, 8, 'a10')
a11 = model.NewIntVar(0, 8, 'a11')
a12 = model.NewIntVar(0, 8, 'a12')
b10 = model.NewIntVar(0, 8, 'b10')
b11 = model.NewIntVar(0, 8, 'b11')
b12 = model.NewIntVar(0, 8, 'b12')

# to create the dataframe with `item` and `arrival day` columns
total_demand = [backlog, incoming]
total_demand = pd.concat(total_demand, ignore_index=True)
total_demand = total_demand.sort_values('item')


# then an attempt to create the `processed date` column
item1_repair_day = []
count = 0

for i in range(0, (a10*s1a + b10*s1b + a11*s1a + b11*s1b + a12*s1a + b12*s1b)):
    if a10*s1a + b10*s1b > count:
        item1_repair_day.append(0)
        count += 1
    else:
        if a11*s1a + b11*s1b > count:
            item1_repair_day.append(1)
            count += 1
        else:
            if a12*s1a + b12*s1b > count:
                item1_repair_day.append(2)
                count += 1
            else:
                print("No item 1's were processed on Day 0, Day 1, and Day 2")

total_demand['repair day'] = item1_repair_day

I could then work out the age and success columns from there and get the objective function, but the code above doesn't work.

What I want as my objective function

objective_function = total_demand['success'].sum()

model.Maximize(objective_function)

solver = cp_model.CpSolver()
status = solver.Solve(model)

this makes no sense:

for i in range(0, (a10*s1a + b10*s1b + a11*s1a + b11*s1b + a12*s1a + b12*s1b)):

a10 is a variable. a10 * s1a is a linear expression. It does not evaluate as a number for the range expression.

You should look at basic CP-SAT example to understand how to access the value assigned to the variables after the solve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM