Inheritance: MonoBehaviour
示例#1
0
        /// <summary>
        /// Deserialize a treenode
        /// </summary>
        /// <param name="treeXML"></param>
        /// <returns></returns>
        private TreeNode DeSerializeTree(XmlElement treeXML)
        {
            Stump innerStump = new Stump();

            innerStump.Dim  = int.Parse(treeXML.Attributes["Dim"].Value);
            innerStump.Thr  = double.Parse(treeXML.Attributes["Thr"].Value);
            innerStump.Sign = double.Parse(treeXML.Attributes["Sign"].Value);

            TreeNode treeNode = new TreeNode();

            treeNode.InnerStump = innerStump;
            foreach (XmlNode node in treeXML.ChildNodes)
            {
                switch (node.Name)
                {
                case "Left":
                    treeNode.Left        = DeSerializeTree((XmlElement)node);
                    treeNode.Left.Parent = treeNode;
                    break;

                case "Right":
                    treeNode.Right        = DeSerializeTree((XmlElement)node);
                    treeNode.Right.Parent = treeNode;
                    break;
                }
            }
            return(treeNode);
        }
示例#2
0
        /// <summary>
        /// find a optimal stump in sub dataset
        /// </summary>
        /// <param name="sortdata">sorted X[][dim]</param>
        /// <param name="flag">flag of the sub dataset</param>
        /// <param name="datasetFlags">flags of the whole dataset</param>
        /// <param name="weight">current weight</param>
        /// <returns>the stump of min Pm</returns>
        Stump OptimalOneNode(SortedData sortdata, int flag, int[] datasetFlags, double[] weight)
        {
            int          N = sortdata.N, MaxDim = sortdata.MaxDim;
            List <Stump> stumpArray = new List <Stump>(MaxDim);

            double[] sortedWeight = new double[sortdata.N];
            object   _lock        = new object();

            for (int dim = 1; dim <= MaxDim; dim++)
            //Parallel.For(1, MaxDim + 1, dim =>
            {
                SortedNode[] sortedDim = sortdata[dim];
                for (int n = 0; n < N; n++)
                {
                    sortedWeight[n] = weight[sortedDim[n].N];
                }
                Stump stump = OptimalOneDim(sortedDim, flag, datasetFlags, sortedWeight, N, dim);
                stumpArray.Add(stump);
            }//);
            stumpArray.Sort();
            Stump beststump = null;

            do
            {
                beststump = stumpArray[0];
                stumpArray.RemoveAt(0);
            } while (beststump == null && stumpArray.Count != 0);
            return(beststump);
        }
示例#3
0
 public SubGraph(int numFeatures, int numIterations)
 {
     Splits = new Stump[numFeatures][];
     for (int i = 0; i < numFeatures; ++i)
     {
         Splits[i] = new Stump[numIterations];
         for (int j = 0; j < numIterations; j++)
         {
             Splits[i][j] = new Stump(0, 0, 0);
         }
     }
 }
示例#4
0
        public List <BinarySplitter> Train(List <BinaryClassificationSample> samples)
        {
            List <BinarySplitter> result = new List <BinarySplitter>();
            var resamples = samples;

            while (resamples.Count > 0)
            {
                BinarySplitter bs = Stump.GetBestSpliter(resamples);
                result.Add(bs);
                //Resample out wrong points
                resamples = resamples.Where(c => (c.X[bs.SplitedXIndex] == 0 && c.Y == bs.YGiven1) ||
                                            (c.X[bs.SplitedXIndex] == 1 && c.Y != bs.YGiven1)).ToList();
            }
            return(result);
        }
示例#5
0
        /// <summary>
        ///     Creates a Stump from a contract.
        /// </summary>
        /// <param name="contract">The <see cref="StumpContract"/> used to create the Stump.</param>
        /// <returns>
        ///     A <see cref="Stump"/> created from the specified <paramref name="contract"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="contract"/> is <c>null</c>.</exception>
        public static Stump CreateStumpFromContract(StumpContract contract)
        {
            contract = contract ?? throw new ArgumentNullException(nameof(contract));

            var stump = new Stump(contract.StumpId);

            foreach (var r in contract.Rules)
            {
                var rule = ContractBindings.CreateRuleFromContract(r);
                stump.AddRule(rule);
            }

            stump.Responses.Add(contract.Response);

            return(stump);
        }
示例#6
0
        /// <summary>
        ///     Defines the entry point of the application.
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        public static void Main(string[] args)
        {
            ConsoleHelper.ApplicationBanner("Hello World API");

            // Create a new Stumps Server
            var server = new StumpsServer();

            // An open port will be chosen automatically unless specified
            // server.ListensOnPort = 9100;

            // Create a new Stump for the server
            var stump = new Stump("HelloWorldStump");

            // Add two rules that stumps out HTTP GET requests for the url /HeloWorld.htm
            stump.AddRule(new HttpMethodRule("GET"));
            stump.AddRule(new UrlRule("/HelloWorld.htm"));

            // Create a response for the rule
            var response = new BasicHttpResponse();

            response.Headers["Content-Type"] = "text/html;charset=UTF-8";
            response.AppendToBody(
                "<html><header><title>Stumps Hello World</title></header><body><p>Hello From Stumps</p></body></html>");

            // Add the response to the stump
            stump.Responses = new StumpResponseFactory();
            stump.Responses.Add(response);

            // Add the stump to the server
            server.AddStump(stump);

            // Show the requests that are incomming
            server.RequestProcessed += (o, e) => ConsoleHelper.ShowHttpResponse(server, e);

            // Start the server and wait!
            server.Start();

            // Show the URL to the user
            Console.WriteLine("Browse to http://localhost:{0}/HelloWorld.htm", server.ListeningPort);
            Console.WriteLine();

            // Wait to exit
            ConsoleHelper.WaitForExit();

            server.Shutdown();
            server.Dispose();
        }
示例#7
0
        public double Train(TrainData data, double[] weight)
        {
            SortedData   sortdata = data as SortedData;
            int          N = sortdata.N, MaxDim = sortdata.MaxDim;
            List <Stump> stumpArray = new List <Stump>(MaxDim);

            double[] sortedWeight = new double[sortdata.N];
            for (int dim = 1; dim <= MaxDim; dim++)
            {
                SortedNode[] sortedDim = sortdata[dim];
                //sort the weight as sortedDim
                for (int n = 0; n < N; n++)
                {
                    sortedWeight[n] = weight[sortedDim[n].N];
                }
                //find the best Stump in a dim.
                Stump stump = OptimalOneDim(sortedDim, sortedWeight, N, dim);
                stumpArray.Add(stump);
            }
            stumpArray.Sort();
            _stump = stumpArray[0];
            return(stumpArray[0].Pm);
        }
示例#8
0
        public double Train(TrainData data, double[] weight)
        {
            SortedData sortdata = data as SortedData;
            int        N = sortdata.N, MaxDim = sortdata.MaxDim;

            int[] datasetFlags = new int[sortdata.N];
            int   flag         = 0;

            Stump stump = OptimalOneNode(sortdata, flag, datasetFlags, weight);

            if (stump == null)
            {
                throw new Exception(Messege.CouldNotClassify);
            }

            TreeNode treeNode = new TreeNode();

            treeNode.InnerStump = stump;
            treeNode.Parent     = null;
            treeNode.Flag       = flag;
            treeNode.Delta      = 0.5 - treeNode.InnerStump.Pm;

            List <TreeNode> priorityQueue = new List <TreeNode>();

            priorityQueue.Add(treeNode);
            double Pm = stump.Pm;

            for (int splitIndex = 0; splitIndex < _maxSplit; splitIndex++)
            {
                do
                {
                    treeNode = priorityQueue[0];
                    priorityQueue.RemoveAt(0);
                } while (treeNode == null && priorityQueue.Count != 0);

                if (treeNode == null)
                {
                    break;
                }

                if (treeNode.Parent == null)
                {
                    _treeRoot = treeNode;
                }
                else
                {
                    if (treeNode.Flag % 2 != 0)
                    {
                        treeNode.Parent.Left = treeNode;
                    }
                    else
                    {
                        treeNode.Parent.Right = treeNode;
                    }
                    Pm = Pm - treeNode.Delta;
                }

                int leftFlag  = ++flag;
                int rightFlag = ++flag;
                CutDataSet(treeNode, sortdata, leftFlag, rightFlag, ref datasetFlags);

                TreeNode leftNode = null, rightNode = null;
                if (treeNode.InnerStump.Pl > double.Epsilon)
                {
                    stump = OptimalOneNode(sortdata, leftFlag, datasetFlags, weight);
                    if (stump != null)
                    {
                        leftNode            = new TreeNode();
                        leftNode.InnerStump = stump;
                        leftNode.Parent     = treeNode;
                        leftNode.Flag       = leftFlag;
                        leftNode.Delta      = treeNode.InnerStump.Pl - stump.Pm;
                    }
                }
                if (treeNode.InnerStump.Pr > double.Epsilon)
                {
                    stump = OptimalOneNode(sortdata, rightFlag, datasetFlags, weight);
                    if (stump != null)
                    {
                        rightNode            = new TreeNode();
                        rightNode.InnerStump = stump;
                        rightNode.Parent     = treeNode;
                        rightNode.Flag       = rightFlag;
                        rightNode.Delta      = treeNode.InnerStump.Pr - stump.Pm;
                    }
                }

                priorityQueue.Add(leftNode);
                priorityQueue.Add(rightNode);
                priorityQueue.Sort();
            }
            return(Pm);
        }