public static void Main(string[] args) { DateTime start = DateTime.Now; string str = sr.ReadLine(); int tt = int.Parse(str); for (int t = 1; t <= tt; t++) { sw.WriteLine("Case #{0}:", t); str = sr.ReadLine().Trim(); int lines = int.Parse(str); DTree root = CreateDTree(lines); str = sr.ReadLine(); int num = int.Parse(str); for (int i = 0; i < num; i++) { Animal ani = GetAnimal(); ani.p = 1.0; Calcu(ani, root); sw.WriteLine(ani.p.ToString("0.0000000")); } } sw.Close(); DateTime end = DateTime.Now; Console.WriteLine(end - start); }
public Form2() { InitializeComponent(); data = new Matrix <float>(trainCount, featureCount); response = new Matrix <float>(trainCount, 1); dtree = new DTree(); int[,] ls = { { 4, 100, 100, 4 } }; Matrix <int> layerSize = new Matrix <int>(ls); svm = new SVM(); histogram = new Image <Bgr, byte>(4 * trainCount, 256); }
private DTreeNode FindNode(int id) { DTree tree = employeeSelect.Tree; const string tableName = "Инвентаризация.dbo.vwДолжности"; const string idField = "КодДолжности"; const string leftField = "L"; const string rightField = "R"; DTreeNode nd; using (DataTable dt = new DataTable()) { string sql = " DECLARE @L int, @R int\n" + " SELECT @L=" + leftField + ",@R=" + rightField + " FROM " + tableName + " WHERE " + idField + "=" + id + "\n" + " SELECT " + idField + " FROM " + tableName + " WHERE " + leftField + "<= ISNULL(@L,-1) AND " + rightField + ">= ISNULL(@R,-1) " + " ORDER BY " + leftField; using (SqlDataAdapter da = new SqlDataAdapter(sql, employeeSelect.ConnectionString)) da.Fill(dt); if (dt.Rows.Count == 0) { return(null); } TreeNodeCollection nds = tree.Nodes; nd = null; DTreeNode pnd = null; for (int i = 0; i < dt.Rows.Count; i++) { nd = tree.FindNodeIn(nds, (int)dt.Rows[i][idField]); if (nd == null) { tree.RefreshSubNodes(pnd); nd = tree.FindNodeIn(nds, (int)dt.Rows[i][idField]); if (nd == null) { return(null); } } nds = nd.Nodes; pnd = nd; } } return(nd); }
private static void Calcu(Animal ani, DTree tree) { ani.p *= tree.p; if (string.IsNullOrEmpty(tree.name)) { return; } if (ani.features.Contains(tree.name)) { Calcu(ani, tree.TrueTree); } else { Calcu(ani, tree.FalseTree); } }
public void TestDTreesMushroom() { Matrix<float> data, response; ReadMushroomData(out data, out response); //Use the first 80% of data as training sample int trainingSampleCount = (int)(data.Rows * 0.8); Matrix<Byte> varType = new Matrix<byte>(data.Cols + 1, 1); varType.SetValue((byte)MlEnum.VAR_TYPE.CATEGORICAL); //the data is categorical Matrix<byte> sampleIdx = new Matrix<byte>(data.Rows, 1); using (Matrix<byte> sampleRows = sampleIdx.GetRows(0, trainingSampleCount, 1)) sampleRows.SetValue(255); float[] priors = new float[] {1, 0.5f}; GCHandle priorsHandle = GCHandle.Alloc(priors, GCHandleType.Pinned); MCvDTreeParams param = new MCvDTreeParams(); param.maxDepth = 8; param.minSampleCount = 10; param.regressionAccuracy = 0; param.useSurrogates = true; param.maxCategories = 15; param.cvFolds = 10; param.use1seRule = true; param.truncatePrunedTree = true; param.priors = priorsHandle.AddrOfPinnedObject(); using (DTree dtree = new DTree()) { bool success = dtree.Train( data, Emgu.CV.ML.MlEnum.DATA_LAYOUT_TYPE.ROW_SAMPLE, response, null, sampleIdx, varType, null, param); if (!success) return; double trainDataCorrectRatio = 0; double testDataCorrectRatio = 0; for (int i = 0; i < data.Rows; i++) { using (Matrix<float> sample = data.GetRow(i)) { double r = dtree.Predict(sample, null, false).value; r = Math.Abs(r - response[i, 0]); if (r < 1.0e-5) { if (i < trainingSampleCount) trainDataCorrectRatio++; else testDataCorrectRatio++; } } } trainDataCorrectRatio /= trainingSampleCount; testDataCorrectRatio /= (data.Rows - trainingSampleCount); Trace.WriteLine(String.Format("Prediction accuracy for training data :{0}%", trainDataCorrectRatio*100)); Trace.WriteLine(String.Format("Prediction accuracy for test data :{0}%", testDataCorrectRatio*100)); } priorsHandle.Free(); }
public void TestDTreesMushroom() { Matrix <float> data, response; ReadMushroomData(out data, out response); //Use the first 80% of data as training sample int trainingSampleCount = (int)(data.Rows * 0.8); Matrix <Byte> varType = new Matrix <byte>(data.Cols + 1, 1); varType.SetValue((byte)MlEnum.VarType.Categorical); //the data is categorical Matrix <byte> sampleIdx = new Matrix <byte>(data.Rows, 1); using (Matrix <byte> sampleRows = sampleIdx.GetRows(0, trainingSampleCount, 1)) sampleRows.SetValue(255); float[] priors = new float[] { 1, 0.5f }; GCHandle priorsHandle = GCHandle.Alloc(priors, GCHandleType.Pinned); MCvDTreeParams param = new MCvDTreeParams(); param.maxDepth = 8; param.minSampleCount = 10; param.regressionAccuracy = 0; param.useSurrogates = true; param.maxCategories = 15; param.cvFolds = 10; param.use1seRule = true; param.truncatePrunedTree = true; param.priors = priorsHandle.AddrOfPinnedObject(); using (DTree dtree = new DTree()) { bool success = dtree.Train( data, Emgu.CV.ML.MlEnum.DataLayoutType.RowSample, response, null, sampleIdx, varType, null, param); if (!success) { return; } double trainDataCorrectRatio = 0; double testDataCorrectRatio = 0; for (int i = 0; i < data.Rows; i++) { using (Matrix <float> sample = data.GetRow(i)) { double r = dtree.Predict(sample, null, false).value; r = Math.Abs(r - response[i, 0]); if (r < 1.0e-5) { if (i < trainingSampleCount) { trainDataCorrectRatio++; } else { testDataCorrectRatio++; } } } } trainDataCorrectRatio /= trainingSampleCount; testDataCorrectRatio /= (data.Rows - trainingSampleCount); EmguAssert.WriteLine(String.Format("Prediction accuracy for training data :{0}%", trainDataCorrectRatio * 100)); EmguAssert.WriteLine(String.Format("Prediction accuracy for test data :{0}%", testDataCorrectRatio * 100)); } priorsHandle.Free(); }
void Start() { methodLibrary = gameObject.GetComponent<AIBehavior> (); dPath = new DTree<BranchLogic> (new BranchLogic(methodLibrary.MaxWander)); coreTreeSetup (); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// /* Make and instance of the tree based ont he AI type assigned, if no type matches give it the default mob AI. */ //////////////////////////////////////////////////////////////////////////////////////////////////////////////// void Start() { methodLibrary = gameObject.GetComponent<AIBehavior> (); if(behaviorType == "mob"){ dPath = new DTree<BranchLogic> (new BranchLogic(methodLibrary.MaxWander)); coreTreeSetup (); } else if(behaviorType == "boss"){ dPath = new DTree<BranchLogic> (new BranchLogic(methodLibrary.MaxWander)); coreTreeSetup (); } else{ dPath = new DTree<BranchLogic> (new BranchLogic(methodLibrary.MaxWander)); coreTreeSetup (); } }
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(EmployeeSelect)); this.groupUnits = new System.Windows.Forms.GroupBox(); this.tree = new Trees.DTree(); this.imageList = new System.Windows.Forms.ImageList(this.components); this.splitter1 = new System.Windows.Forms.Splitter(); this.panel1 = new System.Windows.Forms.Panel(); this.panel3 = new System.Windows.Forms.Panel(); this.phoneList1 = new PhoneList(); this.groupEmployees = new System.Windows.Forms.GroupBox(); this.list = new System.Windows.Forms.ListView(); this.photo = new System.Windows.Forms.PictureBox(); this.checkFree = new System.Windows.Forms.CheckBox(); this.checkPlaces = new System.Windows.Forms.CheckBox(); this.checkGuests = new System.Windows.Forms.CheckBox(); this.checkInsiders = new System.Windows.Forms.CheckBox(); this.panel2 = new System.Windows.Forms.Panel(); this.empSearchBlock1 = new EmpSearchBlock(); this.groupUnits.SuspendLayout(); this.panel1.SuspendLayout(); this.panel3.SuspendLayout(); this.groupEmployees.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.photo)).BeginInit(); this.panel2.SuspendLayout(); this.SuspendLayout(); // // groupUnits // this.groupUnits.Controls.Add(this.tree); resources.ApplyResources(this.groupUnits, "groupUnits"); this.groupUnits.FlatStyle = System.Windows.Forms.FlatStyle.System; this.groupUnits.Name = "groupUnits"; this.groupUnits.TabStop = false; // // tree // this.tree.AllowAdd = false; this.tree.AllowDelete = false; this.tree.AllowEdit = false; this.tree.AllowFind = false; this.tree.AllowMove = false; this.tree.ColorInsert = System.Drawing.Color.SkyBlue; this.tree.ColorMove = System.Drawing.Color.CornflowerBlue; this.tree.ConnectionString = null; resources.ApplyResources(this.tree, "tree"); this.tree.FullLoad = true; this.tree.HideSelection = false; this.tree.IDField = null; this.tree.ImageList = this.imageList; this.tree.ItemHeight = 16; this.tree.Name = "tree"; this.tree.NodeType = typeof(DTreeNode); this.tree.SelectedNode = null; this.tree.TableName = null; this.tree.TextField = null; this.tree.FillNode += new FillNodeEventHandler(this.tree_FillNode); this.tree.RefreshNodeEvent += new RefreshNodeDelegate(this.tree_RefreshNodeEvent); this.tree.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tree_AfterSelect); // // imageList // this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream"))); this.imageList.TransparentColor = System.Drawing.SystemColors.Control; this.imageList.Images.SetKeyName(0, ""); this.imageList.Images.SetKeyName(1, ""); this.imageList.Images.SetKeyName(2, ""); // // splitter1 // resources.ApplyResources(this.splitter1, "splitter1"); this.splitter1.Name = "splitter1"; this.splitter1.TabStop = false; // // panel1 // this.panel1.Controls.Add(this.panel3); this.panel1.Controls.Add(this.panel2); resources.ApplyResources(this.panel1, "panel1"); this.panel1.Name = "panel1"; // // panel3 // this.panel3.Controls.Add(this.phoneList1); this.panel3.Controls.Add(this.groupEmployees); this.panel3.Controls.Add(this.photo); this.panel3.Controls.Add(this.checkFree); this.panel3.Controls.Add(this.checkPlaces); this.panel3.Controls.Add(this.checkGuests); this.panel3.Controls.Add(this.checkInsiders); resources.ApplyResources(this.panel3, "panel3"); this.panel3.Name = "panel3"; // // phoneList1 // resources.ApplyResources(this.phoneList1, "phoneList1"); this.phoneList1.Name = "phoneList1"; // // groupEmployees // resources.ApplyResources(this.groupEmployees, "groupEmployees"); this.groupEmployees.Controls.Add(this.list); this.groupEmployees.FlatStyle = System.Windows.Forms.FlatStyle.System; this.groupEmployees.Name = "groupEmployees"; this.groupEmployees.TabStop = false; // // list // resources.ApplyResources(this.list, "list"); this.list.FullRowSelect = true; this.list.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None; this.list.HideSelection = false; this.list.Name = "list"; this.list.ShowItemToolTips = true; this.list.UseCompatibleStateImageBehavior = false; this.list.View = System.Windows.Forms.View.Details; this.list.SelectedIndexChanged += new System.EventHandler(this.list_SelectedIndexChanged); // // photo // resources.ApplyResources(this.photo, "photo"); this.photo.Name = "photo"; this.photo.TabStop = false; // // checkFree // resources.ApplyResources(this.checkFree, "checkFree"); this.checkFree.Name = "checkFree"; this.checkFree.CheckedChanged += new System.EventHandler(this.checkFree_CheckedChanged); this.checkFree.VisibleChanged += new System.EventHandler(this.checkFree_VisibleChanged); // // checkPlaces // resources.ApplyResources(this.checkPlaces, "checkPlaces"); this.checkPlaces.Name = "checkPlaces"; this.checkPlaces.CheckedChanged += new System.EventHandler(this.checkPlaces_CheckedChanged); // // checkGuests // resources.ApplyResources(this.checkGuests, "checkGuests"); this.checkGuests.Name = "checkGuests"; this.checkGuests.CheckedChanged += new System.EventHandler(this.checkGuests_CheckedChanged); this.checkGuests.VisibleChanged += new System.EventHandler(this.checkGuests_VisibleChanged); // // checkInsiders // resources.ApplyResources(this.checkInsiders, "checkInsiders"); this.checkInsiders.Name = "checkInsiders"; this.checkInsiders.CheckedChanged += new System.EventHandler(this.checkInsiders_CheckedChanged); // // panel2 // this.panel2.Controls.Add(this.empSearchBlock1); resources.ApplyResources(this.panel2, "panel2"); this.panel2.Name = "panel2"; // // empSearchBlock1 // resources.ApplyResources(this.empSearchBlock1, "empSearchBlock1"); this.empSearchBlock1.BackColor = System.Drawing.SystemColors.Control; this.empSearchBlock1.EmpText = ""; this.empSearchBlock1.FindEmployeeID = 0; this.empSearchBlock1.LabelText = "Найти сотрудника :"; this.empSearchBlock1.Name = "empSearchBlock1"; this.empSearchBlock1.PlaceID = 0; this.empSearchBlock1.Status = 0; this.empSearchBlock1.FindEmployeeEvent += new FindEmployee(this.empSearchBlock1_FindEmployeeEvent); // // EmployeeSelect // this.Controls.Add(this.groupUnits); this.Controls.Add(this.splitter1); this.Controls.Add(this.panel1); this.DoubleBuffered = true; this.Name = "EmployeeSelect"; resources.ApplyResources(this, "$this"); this.Load += new System.EventHandler(this.EmployeeSelect_Load); this.SizeChanged += new System.EventHandler(this.EmployeeSelect_SizeChanged); this.groupUnits.ResumeLayout(false); this.panel1.ResumeLayout(false); this.panel3.ResumeLayout(false); this.groupEmployees.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.photo)).EndInit(); this.panel2.ResumeLayout(false); this.ResumeLayout(false); }
void Start() { methodLibrary = gameObject.GetComponent <AIBehavior> (); dPath = new DTree <BranchLogic> (new BranchLogic(methodLibrary.MaxWander)); coreTreeSetup(); }
public DTree(string treestr) { //Console.WriteLine("parse p"); double tp = 0.0; double t = 1.0; int i = 1; bool flag = true; while (treestr[i] <= '9' && treestr[i] >= '0' || treestr[i] == '.') { if (treestr[i] == '.') { flag = false; ++i; continue; } if (flag) { tp *= 10.0; tp += treestr[i] - '0'; } else { t /= 10.0; tp += t * (treestr[i] - '0'); } ++i; } p = tp; //Console.WriteLine("parse name and subtree"); int len = 0; while (treestr[i + len] <= 'z' && treestr[i + len] >= 'a') { ++len; } if (len == 0) { name = null; TrueTree = null; FalseTree = null; } else { name = treestr.Substring(i, len); // parse TrueTree i += len; len = 1; int cnt = 1; while (cnt != 0) { ++len; if (treestr[i + len] == '(') { ++cnt; } else if (treestr[i + len] == ')') { --cnt; } } ++len; TrueTree = new DTree(treestr.Substring(i, len)); // parse FalseTree i += len; len = 1; cnt = 1; while (cnt != 0) { ++len; if (treestr[i + len] == '(') { ++cnt; } else if (treestr[i + len] == ')') { --cnt; } } ++len; FalseTree = new DTree(treestr.Substring(i, len)); } }