Hi!
I am trying to make a scheduling tool in VBA. I am new to programming but have succeeded in building a VBA scheduling tool which currently works for any number of tasks. Unfortunately, when I try to schedule any more than 7 or 8 tasks, my code is too slow.
The scheduler works by trying to minimise an overall 'result'. Each task initially has an ideal start time and a duration. The 'result' is equal to a function accuracy (how close the tasks are to their ideal start time), plus a function of overlap (how many hours any two tasks are overlapping).
Currently, the scheduler works by iterating each task by 6 hours withing a +- 48 hour period from its 'ideal start time'. The scheduler is based on a recursive function which nests a variable number of for loops (1 for each task) and solves for the 'result' at each iteration, storing the 'result' if it is the smallest.
I think the recursive function which nests for loops and tests for every possible answer is why the code is so slow. I cannot think of a way to find a minimum solution without testing every iteration though. Can somebody please point me in the right direction with how I could achieve a similar result without testing each iteration (using nested for loops)? For interest sake, I have had the code output the result at each iteration for 5 tasks. The resulting graph is below:
As you can see, the minimum solution in this scenario is 0.75.
Because there are many local maxima and minima are there any good numerical methods or solvers which would help me? Would the excel solver work in this scenario? I have never used it before but am happy to look into it.
Thanks in advance for any help!
If it helps, the recursive function I have created is shown below:
I am trying to make a scheduling tool in VBA. I am new to programming but have succeeded in building a VBA scheduling tool which currently works for any number of tasks. Unfortunately, when I try to schedule any more than 7 or 8 tasks, my code is too slow.
The scheduler works by trying to minimise an overall 'result'. Each task initially has an ideal start time and a duration. The 'result' is equal to a function accuracy (how close the tasks are to their ideal start time), plus a function of overlap (how many hours any two tasks are overlapping).
Currently, the scheduler works by iterating each task by 6 hours withing a +- 48 hour period from its 'ideal start time'. The scheduler is based on a recursive function which nests a variable number of for loops (1 for each task) and solves for the 'result' at each iteration, storing the 'result' if it is the smallest.
I think the recursive function which nests for loops and tests for every possible answer is why the code is so slow. I cannot think of a way to find a minimum solution without testing every iteration though. Can somebody please point me in the right direction with how I could achieve a similar result without testing each iteration (using nested for loops)? For interest sake, I have had the code output the result at each iteration for 5 tasks. The resulting graph is below:
As you can see, the minimum solution in this scenario is 0.75.
Because there are many local maxima and minima are there any good numerical methods or solvers which would help me? Would the excel solver work in this scenario? I have never used it before but am happy to look into it.
Thanks in advance for any help!
If it helps, the recursive function I have created is shown below:
Code:
Sub Recursive(b, c, depth, Min_Start, Max_Start, Accuracy, Overlap, minresult, result, result1, Count_Services)
' This is the recursive function which iterates through every result
If b > depth Then
'Calculate f_accuracy
f_accruacy = 0
For x = 1 To Count_Services
f_accuracy = f_accuracy + Accuracy(c(x), x)
Next x
'Calculate f_overlap
Stepup = 0
f_overlap = 0
z = 1
For y = 1 To Count_Services - 1
For g = 2 + Stepup To Count_Services
f_overlap = f_overlap + Overlap(z, c(y), c(g))
z = z + 1
Next g
Stepup = Stepup + 1
Next y
result = f_accuracy + f_overlap
'Find min result
If result < result1 Then
For i = 1 To Count_Services
minresult(i) = c(i)
Next i
result1 = result
End If
Exit Sub
End If
For d = Min_Start(b) To Max_Start(b)
c(b) = d
b = b + 1
Recursive b, c, depth, Min_Start, Max_Start, Accuracy, Overlap, minresult, result, result1, Count_Services
b = b - 1
Next d
End Sub
Last edited: