Employee Scheduling Constraints Issue - python

I am currently trying to develop an employee scheduling tool in order to reduce the daily workload. I am using pyomo for setting up the model but unfortunately stuck on one of the constraint setting.
Here is the simplified background:
4 shifts are available for assignation - RDO (Regular Day Off), M (Morning), D (Day) and N (Night). All of them are 8-hrs shift
Every employee will get 1 RDO per week and constant RDO is preferred (say staff A better to have Monday as day off constantly but this can be violate)
Same working shift (M / D / N) is preferred for every staff week by week (the constraint that I stuck on)
a. Example 1 (RDO at Monday): The shift of Tuesday to Sunday should be / have better to be the same
b. Example 2 (RDO at Thursday): The shift of Mon to Wed should be same as the last working day of prior week, while the shift of Fri to Sun this week also need to be same but not limit to be which shift
Since the RDO day (Mon - Sun) is different among employees, the constraint of point 3 also require to be changed people by people conditionally (say if RDO == "Mon" then do A ; else if RDO == "Tue" then do B), I have no idea how can it be reflected on the constraint as IF / ELSE statement cant really work on solver.
Appreciate if you can give me some hints or direction. Thanks very much!

The constraints you are trying to create could be moderately complicated, and are very dependent on how you set up the problem, how many time periods you look at in the model, etc. etc. and are probably beyond the scope of 1 answer. Are you taking an LP course in school? If so, you might want to bounce your framework off of your instructor for ideas.
That aside, you might want to tackle the ROD by assigning each person a cost table based on their preferences and then putting in a small penalty in the objective based on their "costs" to influence the solver to give them their "pick" -- assumes the "picks" are relatively well distributed and not everybody wants Friday off, etc.
You could probably do the same with the shifts, essentially making a parameter that is indexed by [employee, shift] with "costs" and using that in the obj in a creative way. This would be the easiest solution... others get into counting variables, big-M, etc.

You could in this case use a switch case. Use the ROD as input and the cases are the days of the week. In the cases you can than to the rest of the planing.
Here is a good reverence:
https://pythongeeks.org/switch-in-python/

Related

Production Planning based on the Raw Material Arrival

I am a non-programmer here but would like to learn to solve 1 problem at a time.
I want to make a production plan based on the availability of raw materials where % of each raw material (RM) decides the product type.
Example -
Product_1: RM_1 - 50%, RM_2 - 30%, RM_3 - 20%.............1st Choice
Product_2: RM_1 - 50%, RM_4 - 10%, RM_5 - 40%.............2nd Choice
Product_3: RM_2 - 40%, RM_3 - 20%, RM_5 - 30%.............3rd Choice
Number of products are fixed i.e., 4
Number of Raw materials are fixed i.e., 8
I have 2 production lines with different capacity (line_1 - 10 Tons per Day, line_2 - 15 Tons per Day).
I intend to produce Product_1 for the whole year but the main constraint is that the Raw materials of choice (RM_1, RM_2 and RM_3) may not be readily available all the time. All the raw materials have to be imported. They are shipped 70-80 times a year. Hence until the inventory is replenished, I have to switch to Product_2 or Product_3 depending on the ratio of raw materials available to keep my plant running.
So, I am looking for a production planner that can consider all these constraints and formulate a day wise production plan.
If someone would be kind enough to walk me through the steps that I have to perform, I would be grateful.
Thanks.
I tried to do this in excel but there are too many constraints for formulation without programing.
I have read about you good folks while googling the solution, hence I am here.
thanks.

Python PuLP Optimization - How to improve performance?

I'm writing a script to help me schedule shifts for a team. I have the model and the script to solve it, but when I try to run it, it takes too long. I think it might be an issue with the size of the problem I'm trying to solve. Before getting into the details, here are some characteristics of the problem:
Team has 13 members.
Schedule shifts for a month: 18 operating hours/day, 7 days/week, for 4 weeks.
I want to schedule non-fixed shifts: members can be on shift during any of the 18 operating hours. They can even be on for a couple hours, off for another hour, and then on again, etc. This is subject to some constraints.
Sets of the problem:
m: each of the members
h: operating hours (0 to 17)
d: days of the week (0 to 6)
w: week of the month (0 to 4)
Variables:
X[h,h,d,w]: Binary. 1 if member m starts a shift on hour h,d,w. 0 otherwise.
Y[h,h,d,w]: Binary. 1 if member m is on shift on hour h,d,w. 0 otherwise.
W[m,d,w]: Binary. 1 if member m had any shift on day d,w. 0 otherwise.
G[m,w]: Binary. 1 if member m had the weekend off during week w. 0 otherwise.
The problem has 20 constraints, 13 which are "real constraints" of the problem and 7 which are relations between the variables, for the model to work.
When I run it, I get this message:
At line 2 NAME MODEL
At line 3 ROWS
At line 54964 COLUMNS
At line 295123 RHS
At line 350083 BOUNDS
At line 369067 ENDATA
Problem MODEL has 54959 rows, 18983 columns and 201097 elements
Coin0008I MODEL read with 0 errors
I left it running overnight and didn't even get a solution. Then I tried changing all variables to continuous variables and it took ~25 seconds to find the optimal solution.
I don't know if I'm having issues because of the size of the problem, or if it's because I'm using only binary variables. Or a combination of that.
Are there any general tips or best practices that could be used to improve performance on a model? Or is it always related to the specific model I'm trying to solve?
The long duration solve is almost certainly due to the number of binary variables in your model, and it may be degenerate, meaning that there are many solutions that produce the same value for the objective, so it is just thrashing around trying all of the (relatively equal) combinations.
The fact that it does solve when you relax the integer constraint is good and good troubleshooting. Assuming the model is constructed properly, here are a couple things to try:
Solve for 1 week at a time (4 separate solves). It isn't clear what the linkage is from week-to-week from your description, but that would reduce the model to 1/4 of its size.
Change your time-blocks to more than 1 hour. If you used 3 hour blocks, your problem would again reduce to 1/3 of its size by your description. You would only need to reduce the set H to {1,2,3,4,5,6} and then do the mental math to align that with the actual hours. Or you could do 2 hour blocks.
You should also tinker with the optimality gap. Sometimes the difference between 95% Optimal and 100% is days/weeks/years of solver time. Have you tried a .02, .05, 0.10, or 0.25 relative gap? You may still get the optimal answer, but you forgo the guarantee of optimality.

Is there a library or suggested tactic for shift planning with hours and breaks?

I’m trying to think through a sort of extra credit project- optimizing our schedule.
Givens:
“demand” numbers that go down to the 1/2 hour. These tell us the ideal number of people we’d have on at any given time;
8 hour shift, plus an hour lunch break > 2 hours from the start and end of the shift (9 hours from start to finish);
Breaks: 2x 30 minute breaks in the middle of the shift;
For simplicity, can assume an employee would have the same schedule every day.
Desired result:
Dictionary or data frame with the best-case distribution of start times, breaks, lunches across an input number of employees such that the difference between staffed and demanded labor is minimized.
I have pretty basic python, so my first guess was to just come up with all of the possible shift permutations (points at which one could take breaks or lunches), and then ask python to select x (x=number of employees available) at random a lot of times, and then tell me which one best allocates the labor. That seems a bit cumbersome and silly, but my limitations are such that I can’t see beyond such a solution.
I have tried to look for libraries or tools that help with this, but the question here- how to distribute start times and breaks within a shift- doesn’t seem to be widely discussed. I’m open to hearing that this is several years off for me, but...
Appreciate anyone’s guidance!

Items movement daily collection database design system issue

I am doing a very simple database in mysql to track movement of items. The current paper form looks like this:
Date totalFromPreviousDay NewToday LeftToday RemainAtEndOfDay
1.1.2017 5 5 2 8 (5+5-2)
2.1.2017 8 3 0 11 ( 8+ 3 -0)
3.1.2017 11 0 5 6 (11+0-5)
And so forth. In my table, I want to make totalFromPreviousDay and RemainAtEndOfDay calculated fields which I show in my front end only. That is mainly cos we tend to erase on the paper due to errors. I want them to be reflected based on changes to the other two fields. As such, I did my table like this:
id
date
NewToday
LeftToday
Now the problem I am facing is, I want to select any date and be able to say "there were 5 items at the start of the day or from previous day, then 5 were added, 0 left and the day ended with 10 items"
So far, I can't really think of a way going about it. Theoretically, I want to try something like this: if the requested day is Feb. 1, 2017, start at 0 cos that's the day we started collecting data. If not, loop thru the records at 0 and doing the math until the requested date is found.
But that is obviously inefficient cos i have to start form first date until the last every time.
Is my approach ok or I should include the columns in the table? If the first, what would be the way to do it in python/mysql?
I think you have to step back a little bit and define the business needs first (it is worthwhile to talk somebody, who worked with stocks before) because these determine your table structure.
A system always tracks the current level of stocks and the movement. It is a business decision how often you save your historical stock level and this influences how you store the data.
You may save the current stock level along with all transactions. In this case you would store the stock level in the transactions table. You do not even have to sum up a transactions per day because the last transaction per day will have the daily closing stock level anyway.
You may choose to save the historic stock levels regularly (on a daily / weekly / monthly, etc. basis). In this case you will have a separate historic stock levels table with stock id, stock name (name may change over the time, so may be a good idea to save it), date and the level. If you would like to know the historic stock level for any point of time that falls between your saved points, then you need to take the latest saved stock level before the period you are looking for, and sum up all transactions to the saved period.

Data modeling/forecasting in Python?

How do I go about this problem I've thought up? I don't even know if it's possible in Python, but anyway. Basically, I want to give Python some data to look for patterns in the data and then display the most likely result. I thought pocket money would make a good example:
A gets $7 a week, a dollar a day, he spends $5 at the weekend and his balance on Monday is $2 + $1 ($7 a week 1 a day). This continues for three weeks. What will A get on the forth week?
Balance in account:
week1 = 1,2,3,4,5,6,7
week2 = 3,4,5,6,7,8,9
week3 = 5,6,7,8,9,10,11
week4 = ??????????
Now, instead of basic math I was wondering if it was possible to create a model, one that looks for patterns and then creates the data using the existing data and the patterns. So the script should be able to see that A gets $7 a week and he loses $5 a week. Is this possible?
Is the model possible to be flexible as in if I give it other data of the same nature, will it be able to find patterns?
(I'm using Python 3.2.)
What you're describing is classified as a regression problem. Armed with this term, you should be able to find lots of information online and in books.
To point you in the right direction, I'd suggest investigating "linear regression" and perhaps moving on to a more complex regression model like the "random forests regressor".
The "scikit-learn" Python package has a number of different regression models and the documentation is excellent.

Categories

Resources