#include "Network.h"
float max_satisfaction = 0;
void bandwidth_assignment(void)
{
struct Node *node = NULL;
struct Link *next = NULL;
int i;
double load;
for (i=0;i<network.num_nodes;i++)
{
node = &(network.nodes[i]);
next = node->links;
while (next!=NULL)
{
if (next->wired == 1)
{
next->assigned_bw = WIRED_BW;
next = next->next_link;
continue;
}
if (next->freq == -1)
load = 0;
else
load = estimate_interference_for_edge(next->freq, next);
if (load < EPSILON)
next->assigned_bw = 0;
else
next->assigned_bw = (BW_PER_CHNL * next->expected_load) / load;
next = next->next_link;
}
}
}
int find_path_from_tree(int src, int dest, int routeid, int length)
{
struct Link * next;
struct Link * co_edge;
if (src == dest)
return 1;
//printf("Using find_path_from_tree\n");
// go through all marked edges coming out of src
next = network.nodes[src].links;
while (next!=NULL)
{
if ((next->flag == 1) && (next->flag2 == 0))
{
//if (next->freq == -1)
//printf("Using new edge %d->%d\n", next->main_node, next->other_node);
//else
//printf("Using old edge %d->%d\n", next->main_node, next->other_node);
routes[routeid][length] = next;
routes[routeid][length]->flag2 = 1;
co_edge = find_reverse_edge(routes[routeid][length]);
co_edge->flag2=1;
if (find_path_from_tree(next->other_node, dest, routeid, length+1))
return 1;
routes[routeid][length]->flag2 = 0;
co_edge->flag2=0;
routes[routeid][length] = NULL;
}
next = next->next_link;
}
return 0;
}
// Runs Djikstra's Algo, write the shortest path into the routes array
int shortest_path(int src, int dest, float comm, int routeid)
{
int best_hop_count;
struct Link * best_edge;
struct Link * co_edge;
struct Link *next = NULL;
int i;
initialize_edge_attribute(0);
for(i=0;i<network.num_nodes;i++)
network.nodes[i].flag = 0;
// mark src as visited with hop-count = 1
network.nodes[src].flag = 1;
// keep visiting nodes until dest is reached
for(;;)
{
// for this iteration, nothing found so far..
best_edge= NULL;
best_hop_count = MAX_NODES + 1;
// go thru all marked node
for(i=0;i<network.num_nodes;i++)
{
// unmarked!
if (network.nodes[i].flag == 0)
continue;
// visit all edges of this marked node
next = (network.nodes[i]).links;
while (next!=NULL)
{
// look at this edge only if it is is unmarked & it leads to an unmarked node
if ((next->flag == 0) && (network.nodes[next->other_node].flag == 0) && (next->residual_capacity >= comm))
{
// better than best so far ?
if ((network.nodes[i].flag + 1) < best_hop_count)
{
best_hop_count = network.nodes[i].flag + 1;
best_edge = next;
}
}
next = next->next_link;
}
}
if (best_edge == NULL) // oops!
return 0;
// mark the newly found best-edge and it's corresponding node
best_edge->flag = 1;
network.nodes[best_edge->other_node].flag = best_hop_count;
// unnecessary
co_edge = find_reverse_edge(best_edge);
co_edge->flag = best_edge->flag;
// if the newly marked node is destination, then you're done!
if (best_edge->other_node == dest)
break;
}
initialize_edge_attribute(4);
// Now put the path into Routes[routeid]
find_path_from_tree(src, dest, routeid, 0);
return 1;
}
int cached_min_hop[MAX_NODES][MAX_NODES];
// Runs Djikstra's Algo, find min-hop-path-length
int min_hop_plen(int src, int dest)
{
int best_hop_count;
struct Link * best_edge;
struct Link * co_edge;
struct Link *next = NULL;
int i;
if (cached_min_hop[src][dest] != -1)
{
//printf(">>>> returning cached [%d,%d] %d\n", src, dest, cached_min_hop[src][dest]);
return cached_min_hop[src][dest];
}
initialize_edge_attribute(5);
for (i=0;i<network.num_nodes;i++)
network.nodes[i].flag3 = 0;
// mark src as visited with hop-count = 1
network.nodes[src].flag3 = 1;
// keep visiting nodes until dest is reached
for(;;)
{
// for this iteration, nothing found so far..
best_edge= NULL;
best_hop_count = MAX_NODES + 1;
// go thru all marked node
for(i=0;i<network.num_nodes;i++)
{
// unmarked!
if (network.nodes[i].flag3 == 0)
continue;
// visit all edges of this marked node
next = (network.nodes[i]).links;
while (next!=NULL)
{
// look at this edge only if it is is unmarked & it leads to an unmarked node
if ((next->flag3 == 0) && (network.nodes[next->other_node].flag3 == 0))
{
// better than best so far ?
if ((network.nodes[i].flag3 + 1) < best_hop_count)
{
best_hop_count = network.nodes[i].flag3 + 1;
best_edge = next;
}
}
next = next->next_link;
}
}
if (best_edge == NULL) // oops!
return -1;
// mark the newly found best-edge and it's corresponding node
best_edge->flag3 = 1;
network.nodes[best_edge->other_node].flag3 = best_hop_count;
// unnecessary
co_edge = find_reverse_edge(best_edge);
co_edge->flag3 = best_edge->flag3;
// if the newly marked node is destination, then you're done!
if (best_edge->other_node == dest)
break;
}
cached_min_hop[src][dest] = network.nodes[dest].flag3 - 1;
cached_min_hop[dest][src] = network.nodes[dest].flag3 - 1;
//printf(">>>> returning new [%d,%d] %d\n", src, dest, cached_min_hop[src][dest]);
return network.nodes[dest].flag3 - 1;
}
struct flow
{
int src;
int dst;
float bw;
float adjustment;
int rtable_pos;
};
struct sorter
{
int len;
int index;
};
float routes_assgn_shortest_path_in_order(int redo_bw)
{
int i,j,k, rid;
int tmp;
struct Link * co_edge;
int exists;
float total_routed;
float min_residual;
int src;
int dst;
struct flow off_flow[MAX_NODES*MAX_NODES];
struct Link * old_routes[MAX_NODES][MAX_NODES];
struct sorter allocator[MAX_NODES];
struct sorter *sorted[MAX_NODES];
struct sorter *swapper;
int num_flows;
struct Link *next;
float total_satisfied = 0;
int flow_count = 0;
int num_off_flow;
int x;
total_routed = 0;
init_adjustments();
initialize_edge_attribute(3);
for(i=0;i<overall_flow_count;i++)
profile[i].count=0;
num_flows = 0;
for(i=0;i<MAX_NODES;i++)
{
if (routes[i][0]!=NULL)
{
sorted[i] = &(allocator[i]);
sorted[i]->index = i;
num_flows++;
}
else break;;
for (j=0; j<MAX_NODES; j++)
{
if (routes[i][j]!=NULL)
sorted[i]->len = j+1;
else
break;
}
}
for(i=0;i<num_flows;i++)
{
for(j=0;j<num_flows-1;j++)
{
if (sorted[j]->len > sorted[j+1]->len)
{
swapper = sorted[j+1];
sorted[j+1] = sorted[j];
sorted[j] = swapper;