Skip to content

Algorithm

Nathan Hur edited this page Aug 28, 2017 · 18 revisions

We decided to use the A* algorithm to produce optimal solutions for task graphs up to 20 nodes. It is a best first state space search algorithm that reduces the state space search by our implemented cost function and clipping techniques.

Description and details of the algorithm used and its implementation.


Heuristics

The A* algorithm relies on priority queues to determine which partial solutions to visit next in the total state space of partial solutions. In determining the priority for each partial solution, heuristics are used. Heuristics are essentially underestimates of the current partial solution - that is, given the current partial solution, all partial or full solutions that can be generated from that current partial solution is guaranteed to have a total cost that is greater than or equal to that underestimate.

There are several cost functions the algorithm uses to determine a partial solution's underestimate: idle time, bottom level, data ready time, and parent's underestimate. Each heuristic has the potential to outperform its peers based on the type of the input graph, thus the algorithm incorporates all heuristics in an attempt to get the best of all worlds. Naturally, the best underestimate is the highest underestimate - if one cost function guarantees the cost function to be at least 5 seconds, and another cost function guarantees 8 seconds, then the second cost function implies the first, making the first redundant. Thus, the algorithm's candidate heuristic is Max(all heuristics).

Bottom Level

The bottom level heuristic of each node calculates the longest path from the selected node. The bottom level is analogous to the critical path - that is, the cost determining path to the bottom of the graph. It assumes the ideal scenario that all paths to other leaves can be scheduled in the other processor. Thus, all full schedules that can be generated from a partial schedule must be at least as great as the maximum time to get to a leaf. The calculation only takes into consideration all the weights of the nodes in the path, ignoring the edge weights.

The bottom level is calculated once and once only, prior to the generation of the partial solutions. This is because the bottom level for each node is specific to that node, and remains static throughout the generation of partial solutions. Thus, after calculation, it is cached in a hashmap and is referenced throughout the partial schedule generation in O(1) time.

Implementation

The bottom level cache is generated by using a queue, with the leaves of the graph being queued first. The bottom level for a node is calculated by SUM(MAX(Bottom level of all children)+self.weight). Because the leaves have no children, their bottom level weights are their weights themselves. Every time a node's bottom level is added to the cache, it checks that node's parents and sees if all of that parent's children's bottom level is already in the cache. If it is true, then it adds the parent to the queue. In this way, the queue moves up the tree towards the root/s.

Idle Time

The idle time heuristic takes into account the amount of time being wasted by a processor in a partial solution, and uses this to calculate an underestimate. In an ideal scenario, with no idle time, each task is distributed equally across each processor. That is, the total schedule cost in this ideal scenario is the sum of all the weights of the tasks divided by the number of processors available. However, the idle time represents time that can never be gained back, thus it adds to the total cost required for an ideal scenario. The idle time heuristic assumes this total idle time can be distributed equally across the processors, thus it is also divided by the total number of processors,

Implementation

The idle time heuristic has two components: the total current idle time, and the total weight. The total weight is a constant: the sum of all weights in the graph divided by the number of processors available. Thus, this is calculated prior to the generation of partial solutions and stored as a field. The total current idle time, however, changes based on the current partial solution. Luckily, since idle time is lost time that can never be gained back, it will never decrease when more nodes are added to a partial solution. Thus, idle time can be updated incrementally with each addition of a node, using the idle time of its parent partial solution, in O(1) time. The idle time is stored as a protected field of a partial solution, and is referenced by child partial solutions to generate their own idle times.

During incremental idle time updates, our solution is to find the start time of the node we are about to add to our new partial solution, and note the processor in which we are adding the node to. Using this processor, we find the latest finish time for that processor prior to adding our new node, and then use this value to subtract from our new node's start time. If the new node we are about to add is the first node for that processor, then the incremental idle time is just the start time of that new node.

Data Ready Time

The data ready time heuristic is closely related to the bottom level heuristic. Whereas the first two heuristics attempt to make best guesses on the cost function based on the current schedule, the data ready time seeks to peek into the future by one additional node. The data ready time asks that, given the ability to add a free node in the earliest time possible, what is the maximum bottom level across all those free nodes? Thus, the heuristic is the sum of the maximum bottom level across all free nodes and the start time of the free node that gave rise to that maximum bottom level.

Implementation

The data ready time takes in the candidate partial solution, then obtains all free nodes that can be added from that partial solution. For each free node, the algorithm finds the earliest time possible that can be placed for that node, then sums this value with that free node's bottom level (using the cache), which is the heuristic. As it loops through each free node, the algorithm takes note of the maximum heuristic out of all free nodes, then returns this value as the partial solution's Data Ready Time Heuristic.

Parent's Underestimate

The parent's underestimate acts as a guarding heuristic against the other heuristics. This guarding property arises as the parent's underestimate guarantees the children's underestimates to be at least as large as the parent's underestimate.


Pruning

The ultimate drawback of the A* algorithm's approach is memory requirements. As the algorithm goes down the tree of the partial solution space, the priority queue that stores the partial solutions exponentially increases, exploding to infeasible numbers for wide and balanced trees. Although heuristics aid in reducing the total number of partial solutions explored, pruning is a much more direct approach to cutting off whole branches. While heuristics keep partial solutions in the queue, however unpromising they may be, pruning will reject certain partial solutions altogether.

Duplicates

Processor Normalization In the client's scenario, we assume that each processor is homogeneous with each other. As such, it is inevitable that duplicate partial solutions arise. Two partial solutions are duplicates of one another if one partial solution's processor schedules is a permutation of the other's processor schedules. A processor schedule is the task schedule for one particular processor. Pruning takes advantage of this fact by first checking if a newly generated partial solution has a duplicate that has already been generated before by the algorithm. If it has, then the algorithm will not add the partial solution to the queue. This is because any children that can be generated form this dropped partial solution will have already been or will be explored by the duplicate.

Schedule Horizons Another property of partial solutions that could be used for pruning is the "schedule horizon". This is the combination of the finishing times of each processor and the time that all of their non-present successor nodes can be scheduled on any processor. This combination defines the uniqueness of the possible child partial solutions - in other words, partial solutions with the same schedule horizon are equivalent in the outcome that if one produces a final optimal schedule, so too will the other. Given that obtaining any final optimal solution is the goal of the algorithm, these schedules are equivalent. We can utilize this equivalency to prune all but one of a group of partial solutions with the same schedule horizon. This pruning technique was researched and implemented for the final milestone.

Implementation of processor normalization pruning

A property of duplicates that is crucial for our implementation is that two processor schedules are equal if the ordering of the tasks within the processor is equal between the two processor schedules. This is because tasks are added to a processor at the earliest slot possible. Consequently, the start times and finish times of the tasks are irrelevant as long as the tasks are ordered with respect to one another in the processor. Therefore, it is sufficient to store, for each partial solution, a string array of size #processors, with each element being a string representing a processor's task schedule in order.

Another property of duplicates to be noted is the fact that duplicates are not localized between sibling partial solutions, but can arise across cousins. An example of this is the partial solutions ["a","b"] and ["ac",""]. A child that can be generated from both solutions is ["ac","b"], yet they were generated from different parents. Thus, a partial solution must be checked with all the explored partial solutions, rather than just its siblings.

When a new partial solution is generated, the algorithm checks this new partial solution with every partial solution present in the priority queue for duplicates. A duplicate is found if each element of the string array of one partial solution is found in the string array of the other partial solution. This single check is done as a nested for-loop, costing O(P^2). However, the computation of checking a generated partial solution with every other partial solution in the queue is alarmingly expensive, particularly as the size of the queue explodes. Moreover, it is not enough to check the priority queue for duplicates, as partial solutions that have already been expanded are removed from the queue as part of the A* algorithm. To account for this, a separate list, called _closed, is maintained that keeps all the partial solutions that have been dequeued from the priority queue. Newly generated partial solutions are checked with the elements of the _closed list, further adding to the computation time.

Future implementations seek to reduce the computation of checking newly generated partial solutions for duplicates in the priority queue and closed list. One idea is to check only those partial solutions with the same heuristic cost function. If the priority queue was ordered based on the cost function, it would take O(log(n)) to find the partial solutions with the same cost function as the newly generated partial solution.

Processor normalization improvement

The testing of more complex task graphs confirmed that the current state of our pruning implementation was not efficient enough for the final milestone. Checking only those partial solutions with the same cost underestimate still required linear comparison in the order of thousands before adding a partial solution. Clearly improvement would be found by minimizing the set of possible duplicates. We decided on using a Java TreeSet implementation to store unique partial solutions and provide duplicate checking in guaranteed O(log(n)) time. For the TreeSet to be effective, we had to define an ordering of partial solutions based on multiple properties, so that as the tree was traversed, every property comparison reduced the set of possible duplicates greatly. The ordering we implemented was based on cost, nodes, starting nodes, and finally nodes in each normalized processor.

cost -> total number of nodes -> starting nodes (sorted by node id) -> processor slots

The comparison of the starting nodes and processor slots looped through each list, comparing the IDs (integers) of each node. This reduced our run time of the most difficult client test graph by 88%.

Schedule horizon pruning implementation

To prevent pruning of all equivalent schedules we had to define way to identify partial solutions that should not be pruned within groups of partial solutions having equivalent schedule horizons. We decided on a canonical ordering of nodes on processors - if this order was fulfilled by a partial solution, then it was immune to any equivalency pruning. This order was based on sorting the graph topologically, each node having a topId which identified their index in the topological sort. While generating child partial solutions, if the processor that the free node was added to was not ordered according to topId then the free node slot was swapped with each processor slot above it that had a higher topId. After each swap, communication constraints were reapplied. We then checked to see if the schedule horizon had changed. The first check was to confirm that the finishing time on that processor had not increased. The second check was to confirm that the earliest time of all successors had not increased. It suffices to check only the nodes that were scheduled after the first element of the swap, as none of the prior nodes could have been affected. We also only need to check that there was no increase - a decrease of the schedule horizon still allows us to prune this state because all states are generated by the A* algorithm and this more promising partial solution will be created. This pruning method, while computationally more expensive than our heuristics and processor normalization, greatly reduced the number of states we explored before reaching an optimally solution and thus exponentially decreased our run time.

Clone this wiki locally