Exemple #1
0
        public NodeBase CreateNode(NodeType nodeType, Vector2 mousePos)
        {
            if (curGraph != null)
            {
                NodeBase curNode = null;
                switch (nodeType)
                {
                case NodeType.Text:
                    curNode          = new TextNode();
                    curNode.nodeName = "New Text";
                    break;

                case NodeType.Dialog:
                    curNode          = new DialogNode();
                    curNode.nodeName = "New Dialog";
                    break;

                case NodeType.Branching:
                    curNode          = new BranchingNode();
                    curNode.nodeName = "Branching Dialog";
                    break;

                case NodeType.Start:
                    curNode          = new StartNode();
                    curNode.nodeName = "New Start";
                    break;

                case NodeType.End:
                    curNode          = new EndNode();
                    curNode.nodeName = "New End";
                    break;

                case NodeType.Bool:
                    curNode          = new BoolNode();
                    curNode.nodeName = "Bool Node";
                    break;

                default:
                    break;
                }

                if (curNode != null)
                {
                    curNode.parentGraph = curGraph;
                    curGraph.nodes.Add(curNode);

                    curNode.InitNode();
                    curNode.nodeRect.x = mousePos.x;
                    curNode.nodeRect.y = mousePos.y;
                }
                return(curNode);
            }
            return(null);
        }
        /// <summary>
        /// Creates root node.
        /// </summary>
        /// <param name="costs">Cost matrix.</param>
        /// <returns>Root node.</returns>
        private static BranchingNode createRoot(double[,] costs)
        {
            var root = new BranchingNode {
                Row = -1, Column = -1, Bound = 0, Parent = null
            };

            for (int r = 0; r < costs.GetLength(0); r++)
            {
                var rowMin = Double.MaxValue;
                for (int c = 0; c < costs.GetLength(1); c++)
                {
                    rowMin = System.Math.Min(rowMin, costs[r, c]);
                }

                root.Bound += rowMin;
            }

            return(root);
        }
        /// <summary>
        /// Makes node by using the specified parent node and the child index (column in the cost matrix).
        /// </summary>
        /// <param name="costs">Cost matrix.</param>
        /// <param name="parent">Parent node.</param>
        /// <param name="column">Child index (column in cost matrix).</param>
        /// <returns>Node.</returns>
        static BranchingNode makeNode(double[,] costs, BranchingNode parent, int column)
        {
            var node = new BranchingNode {
                Row = parent.Row + 1, Column = column, Bound = parent.GetPathCost(costs), Parent = parent
            };
            var isColumnTaken = parent.GetTakenColumns(costs.ColumnCount());

            if (node.Row == costs.RowCount() || isColumnTaken[column])
            {
                return(null); //invalid node
            }
            node.Bound           += costs[node.Row, node.Column];
            isColumnTaken[column] = true;

            for (int r = node.Row + 1; r < costs.RowCount(); r++)
            {
                var min = findMinInRowOmmitingTaken(costs, r, isColumnTaken);
                node.Bound += min;
            }

            return(node);
        }
        /// <summary>
        /// Creates immediate child nodes for the specified node.
        /// </summary>
        /// <param name="costs">Cost matrix.</param>
        /// <param name="node">Node that needs to be branched.</param>
        /// <returns>List of feasible immediate child nodes.</returns>
        private static List <BranchingNode> branchAndSelectMinCostNodes(double[,] costs, BranchingNode node)
        {
            const double EPSILON = 1e-3;

            var immediateChildren = new List <BranchingNode>();
            var minChildCost      = Double.MaxValue;

            //make children and the min cost
            for (int c = 0; c < costs.ColumnCount(); c++)
            {
                var child = makeNode(costs, node, c);
                if (child == null)
                {
                    continue;
                }

                immediateChildren.Add(child);
                minChildCost = System.Math.Min(minChildCost, child.Bound);
            }

            //add children which have the min cost
            var candidates = new List <BranchingNode>();

            foreach (var child in immediateChildren)
            {
                if (System.Math.Abs(child.Bound - minChildCost) < EPSILON)
                {
                    candidates.Add(child);
                }
            }

            return(candidates);
        }
        /// <summary>
        /// Creates root node.
        /// </summary>
        /// <param name="costs">Cost matrix.</param>
        /// <returns>Root node.</returns>
        private static BranchingNode createRoot(double[,] costs)
        {
            var root = new BranchingNode { Row = -1, Column = -1, Bound = 0, Parent = null };

            for (int r = 0; r < costs.GetLength(0); r++)
            {
                var rowMin = Double.MaxValue;
                for (int c = 0; c < costs.GetLength(1); c++)
                {
                    rowMin = System.Math.Min(rowMin, costs[r, c]);
                }

                root.Bound += rowMin;
            }

            return root;
        }
        /// <summary>
        /// Makes node by using the specified parent node and the child index (column in the cost matrix).
        /// </summary>
        /// <param name="costs">Cost matrix.</param>
        /// <param name="parent">Parent node.</param>
        /// <param name="column">Child index (column in cost matrix).</param>
        /// <returns>Node.</returns>
        static BranchingNode makeNode(double[,] costs, BranchingNode parent, int column)
        {
            var node = new BranchingNode { Row = parent.Row + 1, Column = column, Bound = parent.GetPathCost(costs), Parent = parent };
            var isColumnTaken = parent.GetTakenColumns(costs.ColumnCount());

            if (node.Row == costs.RowCount() || isColumnTaken[column])
                return null; //invalid node

            node.Bound += costs[node.Row, node.Column];
            isColumnTaken[column] = true;

            for (int r = node.Row + 1; r < costs.RowCount(); r++)
            {
                var min = findMinInRowOmmitingTaken(costs, r, isColumnTaken);
                node.Bound += min;
            }

            return node;
        }
        /// <summary>
        /// Creates immediate child nodes for the specified node.
        /// </summary>
        /// <param name="costs">Cost matrix.</param>
        /// <param name="node">Node that needs to be branched.</param>
        /// <returns>List of feasible immediate child nodes.</returns>
        private static List<BranchingNode> branchAndSelectMinCostNodes(double[,] costs, BranchingNode node)
        {
            const double EPSILON = 1e-3;

            var immediateChildren = new List<BranchingNode>();
            var minChildCost = Double.MaxValue;

            //make children and the min cost
            for (int c = 0; c < costs.ColumnCount(); c++)
            {
                var child = makeNode(costs, node, c);
                if (child == null) continue;

                immediateChildren.Add(child);
                minChildCost = System.Math.Min(minChildCost, child.Bound);            
            }

            //add children which have the min cost
            var candidates = new List<BranchingNode>();
            foreach (var child in immediateChildren)
            {
                if (System.Math.Abs(child.Bound - minChildCost) < EPSILON)
                    candidates.Add(child);
            }

            return candidates;
        }