Ejemplo n.º 1
0
        /// <summary>
        /// Generate a standard tree which can be used for testing
        /// </summary>
        public static TaskGraph GenerateTestGraph()
        {
            TaskGraph DAGgraph = new TaskGraph();

            List <TaskNode> nodes = new List <TaskNode>();

            nodes.Add(new TaskNode(1, 2000));

            nodes.Add(new TaskNode(2, 2000));
            nodes.Add(new TaskNode(3, 2000));
            nodes.Add(new TaskNode(4, 1500));
            nodes.Add(new TaskNode(5, 500));
            nodes.Add(new TaskNode(6, 500));

            nodes.Add(new TaskNode(7, 1000));
            nodes.Add(new TaskNode(8, 1500));
            nodes.Add(new TaskNode(9, 2000));

            nodes.Add(new TaskNode(10, 2000));

            // Edges from node 1
            for (int i = 0; i < 5; i++)
            {
                DAGgraph.CreateEdge(nodes[0], nodes[i + 1]);
                //nodes[i + 1].AddDependency(nodes[0]);
            }

            // Edges from node 2
            DAGgraph.CreateEdge(nodes[1], nodes[7]);
            DAGgraph.CreateEdge(nodes[1], nodes[8]);


            // Edges from node 3
            DAGgraph.CreateEdge(nodes[2], nodes[6]);

            // Edges from node 4
            DAGgraph.CreateEdge(nodes[3], nodes[7]);
            DAGgraph.CreateEdge(nodes[3], nodes[8]);

            // Edges from node 5
            DAGgraph.CreateEdge(nodes[4], nodes[8]);

            // Edges from node 6
            DAGgraph.CreateEdge(nodes[5], nodes[7]);

            // Edges from node 7
            DAGgraph.CreateEdge(nodes[6], nodes[9]);

            // Edges from node 8
            DAGgraph.CreateEdge(nodes[7], nodes[9]);

            // Edges from node 8
            DAGgraph.CreateEdge(nodes[8], nodes[9]);

            DAGgraph.AddNodes(nodes);
            return(DAGgraph);
        }
Ejemplo n.º 2
0
        public static TaskGraph CreateGraphFromFile()
        {
            string[]  lines       = System.IO.File.ReadAllLines(@"rand3500.stg");
            TaskGraph loadedGraph = new TaskGraph();

            Dictionary <TaskNode, IEnumerable <string> > dependencies = new Dictionary <TaskNode, IEnumerable <string> >();

            for (int i = 1; i < lines.Count(); i++)
            {
                var line = lines[i];

                if (line.StartsWith("#"))
                {
                    break;
                }

                var lineItems = line.Split(null as char[], StringSplitOptions.RemoveEmptyEntries);

                int ID     = 0;
                int weight = 0;
                if (!int.TryParse(lineItems[0], out ID))
                {
                    Console.WriteLine("Wrong graph format, ID cannot be parsed");
                }
                if (!int.TryParse(lineItems[1], out weight))
                {
                    Console.WriteLine("Wrong graph format, Weight cannot be parsed");
                }

                var predecessors = lineItems.Skip(3).ToArray();
                //foreach (var item in predecessors) {
                //    Console.WriteLine(item);
                //}
                var newTaskNode = new TaskNode(ID, weight * 10);

                dependencies[newTaskNode] = predecessors;
                loadedGraph.AddNode(newTaskNode);
            }

            foreach (var taskDependencies in dependencies)
            {
                foreach (var dependency in taskDependencies.Value)
                {
                    int predecessor = 0;
                    if (!int.TryParse(dependency, out predecessor))
                    {
                        Console.WriteLine("Wrong graph format, Predecessor cannot be parsed");
                    }
                    loadedGraph.CreateEdge(predecessor, taskDependencies.Key.ID);
                }
            }


            return(loadedGraph);
        }
        /// <summary>
        ///
        /// </summary>
        private void HandleInput(string consoleCmd)
        {
            string[] cmdArgs = consoleCmd.Split();

            switch (cmdArgs[0])
            {
            case "rand":
                Console.WriteLine("Randomizing a new graph....");
                activeGraph = TaskGraph.GenerateRandomWeightedDAG();
                //activeGraph.PrintImage();
                break;

            case "cores":
                break;

            case "load":
                activeGraph = TaskGraph.CreateGraphFromFile();
                //activeGraph.PrintImage();
                break;

            case "print":
                Console.WriteLine("Printing graph....");
                //activeGraph.PrintImage();
                activeGraph.PrintTree();
                break;

            case "run":
                if (activeGraph == null)
                {
                    Console.WriteLine("No graph present, neeed to load or generate one!!");
                    return;
                }
                if (cmdArgs.Length == 2)
                {
                    ExecuteAlgorithm(cmdArgs[1]);
                }
                else
                {
                    ExecuteAlgorithm();
                }
                break;

            case "help":
                Console.WriteLine("Available commands:");
                Console.WriteLine("\trand");
                Console.WriteLine("\trun --algorithm");
                break;

            default:
                Console.WriteLine("\"" + cmdArgs[0] + "\" command does not exist");
                break;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// A function to randomly generate a weighted DAG tree.
        /// Ranks represent different levels in the graph,
        /// edges can only be drawn from lower levels to higher.
        /// This will prevent cycles.
        ///
        /// </summary>
        public static TaskGraph GenerateRandomWeightedDAG()
        {
            TaskGraph       DAGgraph        = new TaskGraph();
            Random          randomGenerator = new Random();
            List <TaskNode> newNodes        = new List <TaskNode>();
            List <TaskNode> oldNodes        = new List <TaskNode>();

            int numberOfRanks = randomGenerator.Next(Settings.minRanks, Settings.maxRanks);
            //int numberOfRanks = maxRanks;
            int numberOfNewNodes;

            for (int i = 0; i < numberOfRanks; i++)
            {
                newNodes.Clear();

                /*
                 * Generate new nodes
                 */
                if (i == 0)
                {
                    numberOfNewNodes = 1;
                }
                else
                {
                    numberOfNewNodes = randomGenerator.Next(Settings.minWidth, Settings.maxWidth);
                }
                for (int n = 0; n < numberOfNewNodes; n++)
                {
                    newNodes.Add(new TaskNode(DAGgraph.Nodes.Count + 1 + n, randomGenerator.Next(Settings.minSimTime, Settings.maxSimTime)));
                }

                /*
                 * Generate edges from old nodes to new
                 */
                foreach (var oldNode in DAGgraph.Nodes)
                {
                    foreach (var newNode in newNodes)
                    {
                        if (randomGenerator.Next(100) < Settings.changeOfEdge)
                        {
                            DAGgraph.CreateEdge(oldNode, newNode);
                        }
                    }
                }

                // Add new nodes to tree
                //oldNodes = newNodes.ToList();
                DAGgraph.AddNodes(newNodes);
            }

            return(DAGgraph);
        }
Ejemplo n.º 5
0
        public void Serialize(TaskGraph tree, string xmlPath = "tree.dgml")
        {
            Graph graph = new Graph(tree.Nodes, tree.Edges);

            XmlRootAttribute root = new XmlRootAttribute("DirectedGraph");

            root.Namespace = "http://schemas.microsoft.com/vs/2009/dgml";
            XmlSerializer     serializer = new XmlSerializer(typeof(Graph), root);
            XmlWriterSettings settings   = new XmlWriterSettings();

            settings.Indent = true;
            XmlWriter xmlWriter = XmlWriter.Create(xmlPath, settings);

            serializer.Serialize(xmlWriter, graph);
        }
 public GraphSimulator()
 {
     activeGraph = null;
     savedGraphs = new List <TaskGraph>();
 }