Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
for I = 1 to restart_times do
initialize (labeling)
for j-1 to NC_times do
NC Labeling
If j mod2 = 1then
HC (Labeling)
End if
End for
End for
Algorithm of NS Code
class LinkDelay;
private:
Packet *wf2q_deque(void);
void wf2q_enque(Packet *pkt);
int active(int flowid); /* judge the flow is active or not */
void leave(int flowid); /* when an empty and non-leading session
leave,modify other active session's lag */
double sumw(void);
/* sum of the weights of the active session */
int cansend(int flowid); /* determine whether packet of flow flowid can
be send */
protected:
/* flow structure */
struct flowState {
PacketQueue q_; /* packet queue associated to the corresponding flow
*/
int qmaxSize_; /* maximum queue size (in bytes) */
int qcrtSize_; /* current queue size (in bytes) */
double weight_; /* Weight of the flow */
double S_; /* Starting time of flow , not checked for
wraparound*/
double F_; /* Ending time of flow, not checked for wraparound
*/
double lag_; /* identify the difference between real sys and
preference sys */
int rate_; /*uniform error rate*/
RandomVariable *ranvar_; /* the underlying random variate generator*/
} fs_[MAXFLOWS];
CIFQ::CIFQ()
{
/* initialize flow's structure */
for (int i = 0; i < MAXFLOWS; ++i) {
fs_[i].qmaxSize_ = DEF_QUEUE_SIZE;
fs_[i].qcrtSize_ = 0;
fs_[i].weight_ = DEF_FLOW_WEIGHT;
fs_[i].S_ = 0.0;
fs_[i].F_ = 0.0;
fs_[i].lag_ = 0.0;
//fs_[i].send_ = 1;
fs_[i].ranvar_ =NULL;
}
V = 0.0;
link_ = NULL;
}
/*
* entry points from OTcL to set per flow state variables
* - $q set-queue-size flow_id flow_queue_size
* - $q set-flow-weight flow_id flow_weight
* - $q ranvar flowid [new RandomVariable/Uniform] rate
*
* NOTE: $q represents the discipline queue variable in OTcl.
*/
/*
* identify the flow state
*
*/
int CIFQ::active(int flowid)
{
if
(fs_[flowid].qcrtSize_||(!fs_[flowid].qcrtSize_&&fs_[flowid].lag_<0.0))
return 1;
else
return 0;
}
/*
* when an empty and non-leading session leave,modify other active
session's lag
*
*/
void CIFQ::leave(int flowid)
{
printf("ok");
int m;
for (m=0;m<MAXFLOWS;m++)
{
if (active(m))
{ fs_[m].lag_+=fs_[flowid].lag_*fs_[flowid].weight_/sumw();
if (!fs_[m].qcrtSize_&&fs_[m].lag_>=0.0)
leave(m);
}
}
}
/*
*calculate the sum of the weights of the active session,return the sum
*
*/
double CIFQ::sumw(void)
{
double sum=0.0;
int n;
for (n=0;n<MAXFLOWS;n++)
{
if (active(n))
sum+=fs_[n].weight_;
}
return sum;
}
/*
*Determine wether a flow can send data packet
*/
int CIFQ::cansend(int flowid)
{
// if no random var is specified, assume uniform random variable
double u = fs_[flowid].ranvar_ ? fs_[flowid].ranvar_->value() :
Random::uniform();
return (u >= fs_[flowid].rate_);
}
/*
* Receive a new packet.
*
*
*/
void CIFQ::enque(Packet *pkt)
{
wf2q_enque(pkt);
}
drop(pkt);
} else {
if (!fs_[flowId].qcrtSize_) {
/* If queue for the flow is empty, calculate start and finish times
*/
V = max(minS, V);
}
fs_[flowId].q_.enque(pkt);
fs_[flowId].qcrtSize_ += pktSize;
}
}
/*
* Dequeue the packet.
*/
Packet* CIFQ::deque()
{
Packet *pkt = NULL, *nextPkt;
Packet *pktj=NULL, *nextPktj;
int i,j;
int pktSize;
int pktjSize;
double minF = MAXDOUBLE;
int flow = -1;
int maxlagflow= -1;
double W = 0.0;
double maxlag;
/* look for the candidate flow with the earliest finish time */
for (i = 0; i< MAXFLOWS; i++){
if (!active(i))
continue;
if (fs_[i].S_ <= V)
if (fs_[i].F_ < minF){
flow = i;
minF = fs_[i].F_;
}
}
if (fs_[flow].lag_>=0.0&&cansend(flow))
{
pkt = fs_[flow].q_.deque();
pktSize = ((hdr_cmn*)hdr_cmn::access(pkt))->size();
fs_[flow].qcrtSize_ -= pktSize;
/* Set the start and the finish times of the remaining packets in the
* queue */
nextPkt = fs_[flow].q_.head();
if (nextPkt) {
fs_[flow].S_ = fs_[flow].F_;
fs_[flow].F_ = fs_[flow].S_ +
((((hdr_cmn*)hdr_cmn::access(nextPkt))->size())/fs_[flow].weight_);
/* the weight parameter better not be 0 */
}
if (!fs_[maxlagflow].qcrtSize_&&fs_[maxlagflow].lag_>+0)
leave(maxlagflow);
return(pktj);
}
else/*there is no active session ready to send*/
{
/* Set the start and the finish times of the session i */
fs_[flow].S_ = fs_[flow].F_;
fs_[flow].F_ = fs_[flow].S_ + (DELTA/fs_[flow].weight_);
/* the weight parameter better not be 0 */
Force-based algorithms
algorithm BreadthFirstSearch
input:
C, E, s, t
output:
M[t] (Capacity of path found)
P (Parent table)
P := array(1..n)
for u in 1..n
P[u] := -1
P[s] := -2 (make sure source is not rediscovered)
M := array(1..n) (Capacity of found path to node)
M[s] := ∞
Q := queue()
Q.push(s)
while Q.size() > 0
u := Q.pop()
for v in E[u]
(If there is available capacity, and v is not seen before in search)
if C[u,v] - F[u,v] > 0 and P[v] = -1
P[v] := u
M[v] := min(M[u], C[u,v] - F[u,v])
if v ≠ t
Q.push(v)
else
return M[t], P
return 0, P
Bellman-Ford algorithm
procedure BellmanFord(list vertices, list edges, vertex source)
// This implementation takes in a graph, represented as lists of vertices
// and edges, and modifies the vertices so that their distance and
// predecessor attributes store the shortest paths.
Designing Errors
Designing errors are the errors which occurred in the designing
phase of the project development. These errors included
1. Error in E-R diagram design: Some aspects of E-R diagram
design were missed during the design phase. These errors
cropped up when this E-R Diagram was actually implemented as
the database in Access 2003.
2. Error in module designing: There were some inherent errors in
dividing the entire project into user modules. The modules had
to be redesigned once the group started working on the
modules.
Coding Errors
The major part of errors encountered during the project
designing belonged to this phase of project development. This phase
encountered a vast array of errors ranging from simple coding
mistakes to big logical blunders. The errors can be listed as
1. Error in Database connectivity: This error was frequently
encountered when the code was migrated from one terminal to
another. The root cause of this error was the difference in server
names at the respective terminals.
2. Error in the implementation of website security: This was
realized when the values were posted between different forms
and they were visible in the query string. This was resolved by
encrypting this secretive information and by avoiding passing
these values wherever possible.
3. Error in the usage of web controls: In some portions of our
code there was error in selection of the type of control i.e.
dropdown list, textboxes, data grids, etc. We used ASP controls
where web controls had to be used.
4. Error in usage of application variables: Error was
encountered when we tried to create application variables and
embed them in our functionality.
5. Error in keeping track of user sessions: We faced errors
initially while trying to keep track of the different users visiting
the website. This error was dealt with by creating a session table
and session variables and using them as and when required.
6. Error in date range validation: Since this part of the code
involved intensive usage of nested “if” and “while” loops it
became tedious to work with this part of the code to achieve the
desired end results.
7. Error in implementing session logout: We encountered a
problem in which even after a user logged out he could access
the sensitive content of the website.
5.3
Algo complete
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
namespace VisualIntelligentScissors
{
/// <summary>
/// Implements a generalized Dijkstra's algorithm to calculate
/// both minimum distance and minimum path.
/// </summary>
/// <remarks>
/// For this algorithm, all nodes should be provided, and handled
/// in the delegate methods, including the start and finish nodes.
/// </remarks>
public class Dijkstra
{
/// <summary>
/// An optional delegate that can help optimize the algorithm
/// by showing it a subset of nodes to consider. Very useful
/// for limited connectivity graphs. (like pixels on a screen!)
/// </summary>
/// <param name="startingNode">
/// The node that is being traveled away FROM.
/// </param>
/// <returns>
/// An array of nodes that might be reached from the
/// <paramref name="startingNode"/>.
/// </returns>
public delegate IEnumerable<int> NearbyNodesHint(int startingNode);
/// <summary>
/// Determines the cost of moving from a given node to another given node.
/// </summary>
/// <param name="start">
/// The node being moved away from.
/// </param>
/// <param name="finish">
/// The node that may be moved to.
/// </param>
/// <returns>
/// The cost of the transition from <paramref name="start"/> to
/// <paramref name="finish"/>, or <see cref="Int32.MaxValue"/>
/// if the transition is impossible (i.e. there is no edge between
/// the two nodes).
/// </returns>
public delegate int InternodeTraversalCost(int start, int finish);
/// <summary>
/// Creates an instance of the <see cref="Dijkstra"/> class.
/// </summary>
/// <param name="totalNodeCount">
/// The total number of nodes in the graph.
/// </param>
/// <param name="traversalCost">
/// The delegate that can provide the cost of a transition between
/// any two nodes.
/// </param>
/// <param name="hint">
/// An optional delegate that can provide a small subset of nodes
/// that a given node may be connected to.
/// </param>
public Dijkstra(int totalNodeCount, InternodeTraversalCost traversalCost,
NearbyNodesHint hint)
{
if (totalNodeCount < 3) throw new ArgumentOutOfRangeException("totalNodeCount",
totalNodeCount, "Expected a minimum of 3.");
if (traversalCost == null) throw new ArgumentNullException("traversalCost");
Hint = hint;
TraversalCost = traversalCost;
TotalNodeCount = totalNodeCount;
}
/// <summary>
/// The minimum path array, where each array element index corresponds
/// to a node designation, and the array element value is a pointer to
/// the node that should be used to travel to this one.
/// </summary>
public readonly int[] MinimumPath;
/// <summary>
/// The minimum distance from the starting node to the given node.
/// </summary>
public readonly int[] MinimumDistance;
}
/// <summary>
/// Performs the Dijkstra algorithm on the data provided when the
/// <see cref="Dijkstra"/> object was instantiated.
/// </summary>
/// <param name="start">
/// The node to use as a starting location.
/// </param>
/// <returns>
/// A struct containing both the minimum distance and minimum path
/// to every node from the given <paramref name="start"/> node.
/// </returns>
public virtual Results Perform(int start)
{
// Initialize the distance to every node from the starting node.
int[] d = GetStartingTraversalCost(start);
// Initialize best path to every node as from the starting node.
int[] p = GetStartingBestPath(start);
ICollection<int> c = GetChoices();
//Debug.WriteLine("Step v C D P");
//Debug.WriteLine(string.Format("init - {{{0}}} [{1}] [{2}]",
// ArrayToString<int>(",", c), ArrayToString<int>(",", d), ArrayToString<int>(",", p)));
//int step = 0;
/// <summary>
/// Uses the Dijkstra algorithhm to find the minimum path
/// from one node to another.
/// </summary>
/// <param name="start">
/// The node to use as a starting location.
/// </param>
/// <param name="finish">
/// The node to use as a finishing location.
/// </param>
/// <returns>
/// A struct containing both the minimum distance and minimum path
/// to every node from the given <paramref name="start"/> node.
/// </returns>
public virtual int[] GetMinimumPath(int start, int finish)
{
Results results = Perform(start);
return GetMinimumPath(start, finish, results.MinimumPath);
}
/// <summary>
/// Finds an array of nodes that provide the shortest path
/// from one given node to another.
/// </summary>
/// <param name="start">
/// The starting node.
/// </param>
/// <param name="finish">
/// The finishing node.
/// </param>
/// <param name="shortestPath">
/// The P array of the completed algorithm.
/// </param>
/// <returns>
/// The list of nodes that provide the one step at a time path
/// from <paramref name="start"/> to <paramref name="finish"/> nodes.
/// </returns>
protected virtual int[] GetMinimumPath(int start, int finish, int[] shortestPath)
{
Stack<int> path = new Stack<int>();
do
{
path.Push(finish);
finish = shortestPath[finish]; // step back one step toward the start point
}
while (finish != start);
return path.ToArray();
}
/// <summary>
/// Initializes the P array for the algorithm.
/// </summary>
/// <param name="startingNode">
/// The node that has been designated the starting node for the entire algorithm.
/// </param>
/// <returns>
/// The new P array.
/// </returns>
/// <remarks>
/// A fresh P array will set every single node's source node to be
/// the starting node, including the starting node itself.
/// </remarks>
protected virtual int[] GetStartingBestPath(int startingNode)
{
int[] p = new int[TotalNodeCount];
for (int i = 0; i < p.Length; i++)
p[i] = startingNode;
return p;
}
/// <summary>
/// Finds the yet-unconsidered node that has the least cost to reach.
/// </summary>
/// <param name="d">
/// The cost of reaching any node.
/// </param>
/// <param name="c">
/// The nodes that are still available for picking.
/// </param>
/// <returns>
/// The node that is closest (has the shortest special path).
/// </returns>
protected virtual int FindMinimizingDinC(int[] d, ICollection<int> c)
{
int bestIndex = -1;
foreach (int ci in c)
if (bestIndex == -1 || d[ci] < d[bestIndex])
bestIndex = ci;
return bestIndex;
}
/// <summary>
/// Initializes an collection of all nodes not yet considered.
/// </summary>
/// <returns>
/// The initialized collection.
/// </returns>
protected virtual ICollection<int> GetChoices()
{
ICollection<int> choices = new List<int>(TotalNodeCount);
for (int i = 0; i < TotalNodeCount; i++)
choices.Add(i);
return choices;
}
/// <summary>
/// Initializes the D array for the start of the algorithm.
/// </summary>
/// <param name="start">
/// The starting node.
/// </param>
/// <returns>
/// The contents of the new D array.
/// </returns>
/// <remarks>
/// The traversal cost for every node will be set to impossible
/// (int.MaxValue) unless a connecting edge is found between the
/// <paramref name="start"/>ing node and the node in question.
/// </remarks>
protected virtual int[] GetStartingTraversalCost(int start)
{
int[] subset = new int[TotalNodeCount];
for (int i = 0; i < subset.Length; i++)
subset[i] = int.MaxValue; // all are unreachable
subset[start] = 0; // zero cost from start to start
foreach (int nearby in Hint(start))
subset[nearby] = TraversalCost(start, nearby);
return subset;
}
/// <summary>
/// Joins the elements of an array into a string, using
/// a given separator.
/// </summary>
/// <typeparam name="T">The type of element in the array.</typeparam>
/// <param name="separator">The seperator to insert between each element.</param>
/// <param name="array">The array.</param>
/// <returns>The resulting string.</returns>
/// <remarks>
/// This is very much like <see cref="string.Join"/>, except
/// that it works on arrays of non-strings.
/// </remarks>
protected string ArrayToString<T>(string separator, IEnumerable<int> array)
{
StringBuilder sb = new StringBuilder();
foreach (int t in array)
sb.AppendFormat("{0}{1}", t < int.MaxValue ? t + 1 : t, separator);
sb.Length -= separator.Length;
return sb.ToString();
}
}
}