# For Loop and Math

## Recommended Posts

I am trying to create a schedule for my team, and having some issues with getting my head wrapped around the Math and dealing with the loops in the right way.

The conditions of the schedule is it should not matter how many hours the shift is, but working with 10 hour shifts right now. There is a number of agents, lets say 12 for now, but could increase or decrease based on schedule needs.

The other conditions of the schedule are that every hour has to have at least two agents (minus the lunch hour), and no agent can work over 40 hours in the week (4 day schedule of 10 hour shifts, there is going to be an overlap for the hour lunch, and that is the only time that 2 agents are not needed, so the agent is at work 44 hours a week, 4 hours being lunch). The lunch hour is not important, as it can be taken any time during the 11 hours.

Here is my unsuccessful attempt at trying to just get the basic math down.

There are very little comments, but the I think the code can be followed. Please ask if anything does not make sense.

```; schedule

;168 hours in a week
;11 hour 10 hour shifts
; 15.27 total 10/11 hours shift hours * 2, as every hour has to be covered by 2 agents = 32 10 hour shifts,

;
; each agent must work 44 hours
; there has to be 2 agents eevey hour
;

#include-once
#include <array.au3>
Global \$aTime[24 / .25 + 1][8] ;broken down to 15 minute increments
Global \$iIncrements = 0 ; to keep track of each 15 minutes
Global \$aAgents[14] ; how many agents, not sure if this is to little or two much

; create unique agents
For \$x = 1 To UBound(\$aAgents) - 1
\$aAgents[\$x] = 'AGENT ' & \$x
Next

;_ArrayDisplay(\$aAgents)

; fill in the time slots
For \$x = 1 To UBound(\$aTime) - 1
\$aTime[\$x][0] = \$iIncrements
\$iIncrements += .25
Next

; days of the week
For \$x = 1 To UBound(\$aTime, 2) - 1
\$aTime[0][\$x] = \$x
Next

;_ArrayDisplay(\$aTime)

; try and set each time period with an agent
Global \$iCount = 1, \$ixTemp = 0
For \$x = 1 To UBound(\$aTime) - 1 Step 22 ; this is how much time is between first agent starting and next agent

For \$y = 1 To UBound(\$aTime, 2) - 1 Step 1
\$aTime[\$x][\$y] = \$aAgents[\$iCount]
\$iCount += 1 ; different agents
\$ixTemp = \$x
\$x += 22
If \$x >= UBound(\$aTime) - 1 Then \$x = \$ixTemp
;If UBound(\$aTime)
If \$iCount = UBound(\$aAgents) - 1 Then \$iCount = 1

;ConsoleWrite(\$x & ' ' & \$y & ' ' & \$z & @CRLF)
Next
Next

_ArrayDisplay(\$aTime)```

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

What about using the Date.au3? You could use the DateAdd function to fill up the schedule with your agents based on your shift lengths.

##### Share on other sites

nitekram,

How about something like this as a start...

```#include <array.au3>

local \$aSCHD[20][24], \$iAgents = 11, \$iShiftLen = 11, \$iOffset = 0, \$iStagger = 24/(\$iAgents/2)
ConsoleWrite(\$iStagger & @CRLF)

for \$1 = 0 to 23
\$aSCHD[0][\$1] = stringformat('%02s',\$1)
Next

for \$1 = 1 to \$iAgents step 2
if \$iShiftLen + \$iOffset > 24 then \$iOffset = 24 - \$iShiftLen
for \$2 = \$iOffset to \$iShiftLen + \$iOffset - 1
\$aSCHD[\$1][\$2] = \$1
\$aSCHD[\$1+1][\$2] = \$1+1
Next
\$iOffset += \$iStagger
next

_arraydisplay(\$aSCHD)```

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

##### Share on other sites

@kylomas

I tried your example, but it does not fit the criteria. I am working with some of the ideas in your script though...thanks for your help

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

Such as it is...it was 4:30AM...

Good Luck,

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

##### Share on other sites

I like algorithm puzzles But I don't understand the question... If you were going to continue this question, maybe it's an idea to manually craft an example of what you would like your output to be?

Roses are FF0000, violets are 0000FF... All my base are belong to you.

##### Share on other sites

The idea is a schedule maker...I am just trying to get the math down for the first part, that should allow me to continue putting other criteria in place. But the math part is getting me.

I want to create a week or two week schedule, where there are only two employees working at any given hour, and at all hours there should be two emplyoees 24/7 operation (forget about the lunch hour, for now). - also a criteria that I will change, once the math is correct, may want to have 3 or 4 per hour

Each person should only be allowed to work a total of 40 hours - also a criteria that I will change, once the math is correct.

Each person has to work an 11 hour shift (counting the lunch) - also a criteria that I will change, once the math is correct.

There should be at least 10 hours between shifts that a single employee will be allowed to work, meaning the same emplyoee will not work two shifts in a row - also a criteria that I will change, once the math is correct.

There should be a minimum of 8 employees, as with my math there are a total of 168 hours in a week, multiply that by 2, as there will be two employees, divide that number by 11, as that is how many hours each person should work (for now), that will give the amount of shifts for that week, I got 30 shifts, divide that number by 4 (the number of shifts each person will work to get 40 hours) and the answer is 7.63... I round that number up to 8, as there is no half person yet lol

If it helps, I got this so far, inregard to the hours for each shift...

```#include<array.au3>

Global \$aShifts[34]
Global \$iTime = '00'
ConsoleWrite(\$iTime & @CRLF)
For \$x = 1 To UBound(\$aShifts) - 1
\$aShifts[\$x] = 'Shift ' & \$x & ' From ' & ' ' & \$iTime & ' - '

If \$iTime >= 24 Then ;Or \$iTime = '0000' Then
ConsoleWrite(\$iTime & @CRLF)

\$iTime = Mod(\$iTime, 24)
Else
\$iTime += 11
EndIf

;subtract

If \$iTime > 24 Then \$iTime = \$iTime - 24

\$aShifts[\$x]  &= \$iTime
ConsoleWrite(\$iTime & @CRLF)

Next

_ArrayDisplay(\$aShifts)```

I think that sums it up. Let me know if I can help lol

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

What I don't get is: why the 11 hour shifts? That doesn't fit well with the rest of the criteria. Unless shifts overlap, I don't see how you can avoid a tired and grumpy workforce losing two hours of sleep every 24 hours with back-to-back shifts.

Edited by czardas
##### Share on other sites

There should be no back to back shifts, as there would be a break of at least 10 hours between the first shift for employee 1 and the next time employee 1 works. This is to allow different types of scheduling. Say the person would work the first 4 days of the week and off for the last 3 days...that person would need to have a break between shifts. What there will be, is different start times for every shift a person would work. In option 1 below, they would start 4 hours ealier each of the 4 days, and with option 2 they would start 2 hours ealier for each shift. Of course I want other options, but I want the computer to do the work, not my head, as my head hurts trying to get this to work out lol

Manually I came up with these two types of shifts. The first option would allow 33 hours to pass before the next time they work, and the option two would be 11 hours. Again, my math might be wrong? The below times are military times 0 = 12am, 24 also equals 12am

```START   END             opption 1       opption 2       option 3
0   11      MON     1       1
11  22      MON     2       2
22  9       MON/TUES        3       1   11
9   20      TUES        4       2
20  7       TUES/WED        1   33  1
7   18      WED     2       2
18  5       WED/THURS       3       1
5   16      THURS       4       2
16  3       THURS/FRI       1       3
3   14      FRI     2       4
14  1       FRI/SAT     3       3
1   12      SAT     4       4
12  23      SAT     1       3
23  10      SAT/SUN     2       4
10  21      SUN     3       3
21  8       SUN/MON     4       4

8   19      MON             3
19  6       MON/Tues                4
6   17      TUES                3
17  4       TUES/WED                4
4   15      WED             3
15  2       WED/THURS               4
2   13      THURS               3
13  24      THURS               4
0   11      FRI             2
11  22      FRI             1
22  9       FRI/SAT             2
9   20      SAT             1
20  7       SAT/SUN             2
7   18      SUN             1
18  5       SUN/MON             2
5   16      MON             1
16  3       MON/TUES                3
3   14      TUES                4
14  1       TUES/WED                3
1   12      WED             4
12  23      WED             3
23  10      WED/THURS               4
10  21      THURS               3
21  8       THURS/FRI               4
8   19      FRI             1
19  6       FRI/SAT             2
6   17      SAT             1
17  4       SAT/SUN             2
4   15      SUN             1
15  2       SUN/MON             2
2   13      MON             1
13  24      MON             2```

edit, formatting took a toll, my example would only be for 4 employees - half of the people needed, the first option, would be 1, 2, 3, 4 and so on, and the next option would be 1, 2, 1, 2, 1, 2, 1, 2, 3, 4, 3, 4, 3, 4, 3, 4 - there might be other options, but I do no think there will be enough time between shifts to allow a person to still get 40 hours?

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

I meant back-to-back shifts between different employees. eg. 2 employees:

```employee1...   0 - 11       22 - 9      20 - 5

employee2...         11 - 22      9 - 20      5 - 16```

It does not fit into a 24 hour cycle, like 3 x 8 hour shifts.

It's hard to interpret with the formatting. Try changing the code box from AutoIt to text. There's a drop down menu allowing different options for posting code. Also try using spaces instead of tab.

Edited by czardas
##### Share on other sites

Right, that could happen, but there is 11 hours between those two shifts...not a lot of down time, I know. But the other end is nice, because you are done with your work week in about 3.33 days, as compared to working all week. Just trying to come up with different ideas for a 8, 10, and 12 hour work week - the ten hour is hard, as there is always those 4 hours that have to be accounted for. There will be an option though, not all the shift will be exactly like those hours. Maybe interweiving the hours, so that there are still two people on a shift (those hours are abritory, it was just being used to get an idea of the hours), but the shifts are not all the same? I might have confused myself with that last part, but the idea is still 2 people covering an hour, they would not have to start at the same time, just make sure there are 2 covering the hours.

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

This is less than trivial. The main problem is that not all the parameters are so clearly defined. How long must a person be on night shifts before switching to daytime? Do all workers work flexi-hours, or just a fraction of the workers? Are shifts allowed to overlap, so that there are a surplus of workers, for short periods of time? Do all workers have to work the same number of hours?

Edited by czardas
##### Share on other sites

I beleive the shifts would last about 2 months, before there would be a change. There are no flexi-hours. All employees need to work 40 hours per week. Yes the shifts can overlap, I would think about a 1/2 overlap on either the front or back or both, so if both it would be an hour overlap. As for the number of hours, yes, everyone is full time and expected to work 40 hours a week. There might be some conditions where that is not the case, ie someone calls out sick, but that is not going to be the norm. Main condition would be, at least 2 per hour all day, and no more than 40 hours per week (yet that might change in the future), and the minum time between shifts will be 10 hours (that might also change).

Let me know if there is any other questions. Believe me, it is a lot easier doing it by hand, than creating a routine to run all possible types of schedules.

Thanks for all and anyones help.

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

A general hour scheduling script is actually quite tricky and would require some thought. To ensure 24 hour coverage and 40 hours guaranteed work for each employee, might mean some shorter and some longer shifts. Changing any one of the parameters will effect one or more of the others and there may also be several best solutions. It's an interesting problem to solve, on a small scale at least, but complicated to say the least. Sorry I can't provide you with a working solution.

Edited by czardas
##### Share on other sites

Okay, well I will plug away and see if I can even come close to what I want.

Thanks for everyones help

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

I have been thinking about this, but I know I would struggle to find a solution. I know there are similar problems in maths which have solutions. What I was trying to say earlier was that sometimes you will have to compromise one or more parameters to get it to all fit in the schedule. The question is what compromise and only then can you figure out the maths that can do that.

Edited by czardas
##### Share on other sites

Yeah, it does not look fun, but maybe I will learn something. lol

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

##### Share on other sites

it is a lot easier doing it by hand

So you have a method for doing it by hand?  Can you list the steps involved?  That might help us to better understand the problem and layout a general algorithm.

##### Share on other sites

This is where I would begin my investigation.

```; Hours to cover
echo (24 * 7 * 2) ;= 336

; eight people working 40 hours leaves a deficit of 16 hours
echo (8 * 40 + 16) ;= 336

; nine people working 38 hours gives a surplus of 6 hours
echo (9 * 38 - 6) ;= 336

; three people working 38 hours and six people working 37 hours is one of several possible scenarios.
echo (3 * 38 + 6 * 37) ;= 336```

After this stage things get a bit more complicated.

Edited by czardas
##### Share on other sites

So you have a method for doing it by hand?  Can you list the steps involved?  That might help us to better understand the problem and layout a general algorithm.

The only thing I did was start with the list of hours, and then add one agent to the list, then the next agent. The post >LINK, is where I showed an excel spread sheet of the manual way.

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go."

"Everybody catches up with everyone, eventually"

"As you teach others, you are really teaching yourself."

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace \$ghGDIPDll with \$__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

# Ternary operator

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...