Friday 8 June 2012

Task Levels - WINNER

The winning code is shown below: 


There is a $100 prize for simplifying the code as well as implementing the points mentioned below.


Feedback and improvements:
The winning code passed all tests and actually gave better results on two of them. Well done!


All the tests and code can be found here.


The code correctly checks whether there is indeed a solution with the following code:


There is actually more to it. We also need to check that the rates for each tasks also balance out. For example if a producing task has a rate per hour of 10,000 and lasts one hour, and there is a consuming task with a rate per hour of -1,000 and lasts 10 hours. The quantities balance out, however if the MIN and MAX levels are respectively 0 and 2,000, the levels will still be breached.

Possible improvements would be:

- Include a check for the rates.
- Use Queues. One for producing tasks and another for consuming tasks if they need to be moved forward, then dequeue them in the correct order.
- Use an algorithm, which will get the first few tasks which balance out. (i.e. Get the firsts tasks such that 
ABS(SUM of producing tasks + SUM of consuming tasks + initialValue) < MAX level - MIN level. 
Once we have these tasks we need to check their rates will also balance out. If it does then we are guaranteed a solution for these tasks. We can then find their new starting times if there was a breach. Once the new start times are found, select some more tasks in the original collection such that 
ABS(SUM of producing tasks + SUM of consuming tasks + initialValue) < MAX level - MIN level, 
and repeat the process until the whole collection is processed.)
- Remove the need for a "private static" variable (which in this code is private static int MAX_ITER = 1000) (There shouldn't be a need for this as the problem is deterministic.)

Possible new methods which could be implemented are:

- GetBalancedTaskCollection(IEnumerable<ITask> taskCollection, double initialValue) : IEnumerable<ITask>
- AreRatesCompatible(IEnumerable<ITask> taskCollection): bool

Even if the rates for the first few tasks are not compatible, there still could be a solution to the problem. You will have to get the next few tasks that balance out, and then add them to the first subset of tasks, and then check AreRatesCompatible() again, until you have reached the end of the original task collection, at which point you can be certain there is no solution to the problem.

No comments:

Post a Comment