private static string[] ColumnNames(bool withLabel, ModelValue prediction) { var columns = new List <string>(); var data = new ModelDataSet(); for (int i = 0; i < data.Features(); i++) { columns.Add(data.Name(i)); } if (withLabel) { switch (prediction) { case ModelValue.Action: columns.Add("Action"); break; case ModelValue.Angle: columns.Add("FaceAngle"); break; case ModelValue.XY: columns.Add("MoveAngle"); break; default: throw new Exception("Unknown value for prediction : " + prediction); } } return(columns.ToArray()); }
public void SetLevel1UnitValueTest() { //test Level 2 Model model = new Model(); ModelValue mv = new ModelValue(); SetModelValueSample(mv); //calculate Level 3 values mv.CalculateLevel4Values(); mv.CalculateLevel3Values(); //create model model.CreateModel(); //GlobalData.GlobalData.globalData.if_assess_modularity = true; //model.TrimModel(); model.CalculateModelWeight(); mv.SetLevel4UnitValue(model.level4_nodes); mv.SetLevel3UnitValue(model.level3_nodes); mv.SetLevel2UnitValue(model.level2_nodes); mv.SetLevel1UnitValue(model.level1_nodes); //verify the value Assert.AreEqual(3.1944350372696791127913786374672, (double)model.level1_nodes[0].value, 0.0001); ////修剪模型的情况 //model.CreateModel(); //GlobalData.GlobalData.globalData.if_assess_modularity = false; //model.TrimModel(); //model.CalculateModelWeight(); //mv.SetLevel4UnitValue(model.level4_nodes); //mv.SetLevel3UnitValue(model.level3_nodes); //mv.SetLevel2UnitValue(model.level2_nodes); //mv.SetLevel1UnitValue(model.level1_nodes); ////验证数值 //Assert.AreEqual(2.8009873001760972650572709221841, (double)model.level1_nodes[0].value, 0.0001); }
public void CalculateLevel3ValuesTest() { //build Model's Dictionary ModelValue mv = new ModelValue(); SetModelValueSample(mv); //calculate Level 3 values mv.CalculateLevel4Values(); mv.CalculateLevel3Values(); //verify calculation results Assert.AreEqual(mv.NDCS_TOTAL / mv.NS, mv.Coupling); Assert.AreEqual(mv.NS / mv.NM, mv.Cohesion); Assert.AreEqual(mv.NIS_TOTAL / (mv.NS * mv.NS - mv.NS), mv.Design_Complexity); Assert.AreEqual((mv.NPI_TOTAL + mv.NII_TOTAL) / mv.NS, mv.System_Size); Assert.AreEqual((mv.NPI_TOTAL * mv.NPI_TOTAL) / (mv.NM * mv.NM), mv.Service_Granularity); Assert.AreEqual((mv.NMP_TOTAL * mv.NMP_TOTAL) / (mv.NPI_TOTAL * mv.NPI_TOTAL), mv.Parameter_Granularity); Assert.AreEqual(mv.WISL_TOTAL / mv.NS, mv.Service_Loopback); Assert.AreEqual(17.28380m / 12.23456m, mv.Coupling); Assert.AreEqual(12.23456m / 23.45678m, mv.Cohesion); Assert.AreEqual(42.71615m / (12.23456m * 12.23456m - 12.23456m), mv.Design_Complexity); Assert.AreEqual((67.28380m + 92.71615m) / 12.23456m, mv.System_Size); Assert.AreEqual((67.28380m * 67.28380m) / (23.45678m * 23.45678m), mv.Service_Granularity); Assert.AreEqual((117.28380m * 117.28380m) / (67.28380m * 67.28380m), mv.Parameter_Granularity); Assert.AreEqual(3 / 12.23456m, mv.Service_Loopback); }
private static string WriteToDisk(IEnumerable <ModelDataSet> data, ModelValue prediction) { // geneate a random path var path = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); using (var writer = File.CreateText(path)) { foreach (var d in data) { for (int i = 0; i < d.Features(); i++) { writer.Write(d.Feature(i)); writer.Write(','); } switch (prediction) { case ModelValue.Action: writer.WriteLine(d.Action); break; case ModelValue.Angle: writer.WriteLine(d.FaceAngle); break; case ModelValue.XY: writer.WriteLine(d.MoveAngle); break; default: throw new Exception("Unknown value for prediction : " + prediction); } } } return(path); }
private static TextLoader GetTextLoader(MLContext context, ModelValue prediction) { var index = 0; return(context.Data.CreateTextLoader( columns: ColumnNames(true /*with label*/, prediction).Select(c => new TextLoader.Column(c, DataKind.Single, index++)).ToArray(), separatorChar: ',', hasHeader: false)); }
private void BindToModelValue(EAV.Model.IModelAttribute attribute, EAV.Model.IModelInstance instance, EAV.Model.IModelValue value, ViewModelAttributeValue viewValue) { if (value == null && viewValue == null) { return; } if (value != null && viewValue == null) { if (value.ObjectState == ObjectState.New) { instance.Values.Remove(value); } else { value.MarkDeleted(); } return; } else if (value == null && viewValue != null) { value = new ModelValue() { Attribute = attribute, Instance = instance, Unit = attribute.VariableUnits.HasValue ? new ModelUnit() { UnitID = NextUnitID } : null }; } if (value.RawValue != viewValue.Value) { value.RawValue = viewValue.Value; } if (attribute.VariableUnits.HasValue) { if (attribute.VariableUnits.Value) { if (value.Unit.ObjectState == ObjectState.New && !String.Equals(value.Unit.DisplayText, viewValue.UnitText, StringComparison.InvariantCulture)) { value.Unit.DisplayText = viewValue.UnitText; } } else { if (value.UnitID != viewValue.UnitID) { value.Unit = attribute.Units.SingleOrDefault(it => it.UnitID == viewValue.UnitID); } } } }
public void Execute(int s, ref int sP, ref int steps) { int lclsteps = 0; sP = s; double r_temp = 0; //steps--; int max = 5; while (sP != SubGoalState) { int[] temp = maxA(sP); int a = temp[1]; s = sP; if (temp[0] == -1) { break;// theres no choice in initiation set from this state } //sP = temp[0]; bool contextChanged = m_agent.executeAction(sP, temp[1], ref sP, ref r_temp, ref steps); if (sP == s) { max--; if (max == 0) { break; } } else { max = 5; } if (contextChanged || temp[0] != sP) { break; } if (m_agent.Model[sP, temp[1]] == null) { ModelValue m = new ModelValue(s, a, sP, r_temp, 1); m_agent.Model[s, a] = m; } else { m_agent.Model[s, a].Update(s, a, sP, r_temp, 1); } //steps++; lclsteps++; if (lclsteps > 100) { break; } } //steps++; }
private static TextLoader GetTextLoader(MLContext context, ModelValue prediction) { var index = 0; return(context.Data.TextReader( new TextLoader.Arguments() { Separator = ",", HasHeader = false, Column = ColumnNames(true /*with label*/, prediction).Select(c => new TextLoader.Column(c, DataKind.R4, index++)).ToArray() })); }
// train public ModelMLNet(IEnumerable <ModelDataSet> data, ModelValue prediction) { Context = new MLContext(); // add data var textLoader = GetTextLoader(Context, prediction); // spill to disk !?!?! since there is no way to load from a collection var pathToData = ""; try { // write data to disk pathToData = WriteToDisk(data, prediction); // read in data IDataView dataView = textLoader.Load(pathToData); InputSchema = dataView.Schema; // configurations var label = ""; switch (prediction) { case ModelValue.Action: label = "Action"; break; case ModelValue.Angle: label = "FaceAngle"; break; case ModelValue.XY: label = "MoveAngle"; break; default: throw new Exception("Unknown value for prediction : " + prediction); } var dataPipeline = Context.Transforms.CopyColumns(outputColumnName: "Label", inputColumnName: label) .Append(Context.Transforms.Concatenate("Features", ColumnNames(false /* do not include the label */, prediction))); // set the training algorithm var trainer = Context.Regression.Trainers.Sdca(labelColumnName: "Label", featureColumnName: "Features"); var trainingPipeline = dataPipeline.Append(trainer); TrainedModel = trainingPipeline.Fit(dataView); } finally { // cleanup if (!string.IsNullOrWhiteSpace(pathToData) && File.Exists(pathToData)) { File.Delete(pathToData); } } }
public override ModelFitness Evaluate(List <ModelDataSet> data, ModelValue prediction) { if (TrainedModel == null) { throw new Exception("Must initialize the model before calling"); } lock (TrainedModel) { #if ML_LEGACY var testData = CollectionDataSource.Create(data); var evaluator = new RegressionEvaluator(); var metrics = evaluator.Evaluate(TrainedModel, testData); return(new ModelFitness() { RMS = metrics.Rms, RSquared = metrics.RSquared }); #else var textLoader = GetTextLoader(Context, prediction); var pathToData = ""; try { // ugh have to spill data to disk for it to work! pathToData = WriteToDisk(data, prediction); IDataView dataView = textLoader.Read(pathToData); var predictions = TrainedModel.Transform(dataView); var metrics = Context.Regression.Evaluate(predictions, label: "Label", score: "Score"); return(new ModelFitness() { RMS = metrics.Rms, RSquared = metrics.RSquared }); } finally { // cleanup if (!string.IsNullOrWhiteSpace(pathToData) && File.Exists(pathToData)) { File.Delete(pathToData); } } #endif } }
public void CalculateLevel4ValuesTest() { //build Model's Dictionary ModelValue mv = new ModelValue(); SetModelValueSample(mv); //calculate Level 4 values mv.CalculateLevel4Values(); //verify calculation results Assert.AreEqual(12.23456m, mv.NS); Assert.AreEqual(17.28380m, mv.NDCS_TOTAL); Assert.AreEqual(23.45678m, mv.NM); Assert.AreEqual(42.71615m, mv.NIS_TOTAL); Assert.AreEqual(67.28380m, mv.NPI_TOTAL); Assert.AreEqual(92.71615m, mv.NII_TOTAL); Assert.AreEqual(117.28380m, mv.NMP_TOTAL); Assert.AreEqual(3, mv.WISL_TOTAL); }
public void SetModelValueSample(ModelValue mv) { //add service name mv.Dic_NO_ServiceName.Add(0, "Service 1"); mv.Dic_NO_ServiceName.Add(1, "Service 2"); mv.Dic_NO_ServiceName.Add(2, "Service 3"); mv.Dic_NO_ServiceName.Add(3, "Service 4"); mv.Dic_NO_ServiceName.Add(4, "Service 5"); //add number mv.NS = 12.23456m; mv.NDCS_NO_VALUE.Add(0, 1.23456m); mv.NDCS_NO_VALUE.Add(1, 2.34567m); mv.NDCS_NO_VALUE.Add(2, 3.45678m); mv.NDCS_NO_VALUE.Add(3, 4.56789m); mv.NDCS_NO_VALUE.Add(4, 5.67890m); mv.NM = 23.45678m; mv.NIS_NO_VALUE.Add(0, 6.78901m); mv.NIS_NO_VALUE.Add(1, 7.89012m); mv.NIS_NO_VALUE.Add(2, 8.90123m); mv.NIS_NO_VALUE.Add(3, 9.01234m); mv.NIS_NO_VALUE.Add(4, 10.12345m); mv.NPI_NO_VALUE.Add(0, 11.23456m); mv.NPI_NO_VALUE.Add(1, 12.34567m); mv.NPI_NO_VALUE.Add(2, 13.45678m); mv.NPI_NO_VALUE.Add(3, 14.56789m); mv.NPI_NO_VALUE.Add(4, 15.67890m); mv.NII_NO_VALUE.Add(0, 16.78901m); mv.NII_NO_VALUE.Add(1, 17.89012m); mv.NII_NO_VALUE.Add(2, 18.90123m); mv.NII_NO_VALUE.Add(3, 19.01234m); mv.NII_NO_VALUE.Add(4, 20.12345m); mv.NMP_NO_VALUE.Add(0, 21.23456m); mv.NMP_NO_VALUE.Add(1, 22.34567m); mv.NMP_NO_VALUE.Add(2, 23.45678m); mv.NMP_NO_VALUE.Add(3, 24.56789m); mv.NMP_NO_VALUE.Add(4, 25.67890m); mv.WISL_NO_VALUE.Add(0, 1); mv.WISL_NO_VALUE.Add(1, 0); mv.WISL_NO_VALUE.Add(2, 1); mv.WISL_NO_VALUE.Add(3, 1); mv.WISL_NO_VALUE.Add(4, 0); }
public void SetLevel2UnitValueTest() { //test Level 2 Model model = new Model(); ModelValue mv = new ModelValue(); SetModelValueSample(mv); //calculate Level 3 values mv.CalculateLevel4Values(); mv.CalculateLevel3Values(); //create model model.CreateModel(); //GlobalData.GlobalData.globalData.if_assess_modularity = true; //model.TrimModel(); model.CalculateModelWeight(); mv.SetLevel4UnitValue(model.level4_nodes); mv.SetLevel3UnitValue(model.level3_nodes); mv.SetLevel2UnitValue(model.level2_nodes); //verify the value Assert.AreEqual(-0.5814090208767396425967497017122, (double)model.level2_nodes[0].value, 0.0001); Assert.AreEqual(4.5493525993415009929712588253279, (double)model.level2_nodes[1].value, 0.0001); Assert.AreEqual(4.435102352522840320814015528183, (double)model.level2_nodes[2].value, 0.0001); Assert.AreEqual(4.37469421809111477997698989807, (double)model.level2_nodes[3].value, 0.0001); ////修剪模型的情况 //model.CreateModel(); //GlobalData.GlobalData.globalData.if_assess_modularity = false; //model.TrimModel(); //model.CalculateModelWeight(); //mv.SetLevel4UnitValue(model.level4_nodes); //mv.SetLevel3UnitValue(model.level3_nodes); //mv.SetLevel2UnitValue(model.level2_nodes); ////验证数值 //Assert.AreEqual(-0.5814090208767396425967497017122, (double)model.level2_nodes[0].value, 0.0001); //Assert.AreEqual(4.5493525993415009929712588253279, (double)model.level2_nodes[1].value, 0.0001); //Assert.AreEqual(4.435102352522840320814015528183, (double)model.level2_nodes[2].value, 0.0001); }
public override ModelFitness Evaluate(List <ModelDataSet> data, ModelValue prediction) { if (TrainedModel == null) { throw new Exception("Must initialize the model before calling"); } lock (TrainedModel) { var textLoader = GetTextLoader(Context, prediction); var pathToData = ""; try { // ugh have to spill data to disk for it to work! pathToData = WriteToDisk(data, prediction); IDataView dataView = textLoader.Load(pathToData); var predictions = TrainedModel.Transform(dataView); var metrics = Context.Regression.Evaluate(predictions, labelColumnName: "Label", scoreColumnName: "Score"); return(new ModelFitness() { RMS = metrics.RootMeanSquaredError, RSquared = metrics.RSquared }); } finally { // cleanup if (!string.IsNullOrWhiteSpace(pathToData) && File.Exists(pathToData)) { File.Delete(pathToData); } } } }
private void SetDpItemValue(IWriteSession session, IDpItem dpItem, ModelValue value) { dpItem.Value = value; session.SaveHierarchy(dpItem); }
public override ModelFitness Evaluate(List <ModelDataSet> data, ModelValue prediction) { if (TrainedModel == null) { throw new Exception("Must initialize the model before calling"); } if (data.Count == 0) { return(default(ModelFitness)); } // values var predictions = new float[data.Count]; var labels = new float[data.Count]; for (int i = 0; i < data.Count; i++) { predictions[i] = Predict(data[i]); switch (prediction) { case ModelValue.Action: labels[i] = (float)data[i].Action; break; case ModelValue.Angle: labels[i] = data[i].FaceAngle; break; case ModelValue.XY: labels[i] = data[i].MoveAngle; break; default: throw new Exception("Unknown prediction type : " + prediction); } } // calculate rms // https://sciencing.com/calculate-rms-5104500.html // rms(a,b,c) = sqrt((a^2 + b^2 + c^2)/3); var rms = 0d; for (int i = 0; i < predictions.Length; i++) { rms = Math.Pow(predictions[i] - labels[i], 2); } rms /= predictions.Length; rms = Math.Sqrt(rms); // calculate r^2 // https://www.sapling.com/5117357/calculate-rsquared // R = (Count (sum of ab) - (sum of a)(sum of b)) / [sqrt((Count(sum a^2) - (sum of a)^2)(Count *(sum of b^2) - (sum of b)^2)] // a == labels // b == prediction var ab = 0d; var a = 0d; var b = 0d; var a2 = 0d; var b2 = 0d; for (int i = 0; i < predictions.Length; i++) { a += labels[i]; b += predictions[i]; ab += (labels[i] * predictions[i]); a2 += Math.Pow(labels[i], 2); b2 += Math.Pow(predictions[i], 2); } var r2 = ((predictions.Length * ab) - (a * b)) / Math.Sqrt(((predictions.Length * a2) - Math.Pow(a, 2)) * (predictions.Length * b2 - Math.Pow(b, 2))); r2 = Math.Pow(r2, 2); return(new ModelFitness() { RMS = rms, RSquared = r2 }); }
public void SetLevel3UnitValueTest() { Model model = new Model(); ModelValue mv = new ModelValue(); SetModelValueSample(mv); //calculate Level 3 values mv.CalculateLevel4Values(); mv.CalculateLevel3Values(); model.CreateModel(); //GlobalData.GlobalData.globalData.if_assess_modularity = true; //model.TrimModel(); model.CalculateModelWeight(); mv.SetLevel4UnitValue(model.level4_nodes); mv.SetLevel3UnitValue(model.level3_nodes); mv.SetLevel3UnitValue(model.level2_nodes); mv.SetLevel3UnitValue(model.level1_nodes); //verify the value of Level 3 Assert.AreEqual(17.28380m / 12.23456m, model.level3_nodes[0].value); Assert.AreEqual(12.23456m / 23.45678m, model.level3_nodes[1].value); Assert.AreEqual(42.71615m / (12.23456m * 12.23456m - 12.23456m), model.level3_nodes[2].value); Assert.AreEqual((67.28380m + 92.71615m) / 12.23456m, model.level3_nodes[3].value); Assert.AreEqual(17.28380m / 12.23456m, model.level3_nodes[4].value); Assert.AreEqual(12.23456m / 23.45678m, model.level3_nodes[5].value); Assert.AreEqual((67.28380m * 67.28380m) / (23.45678m * 23.45678m), model.level3_nodes[6].value); Assert.AreEqual((117.28380m * 117.28380m) / (67.28380m * 67.28380m), model.level3_nodes[7].value); Assert.AreEqual(3 / 12.23456m, model.level3_nodes[8].value); Assert.AreEqual((117.28380m * 117.28380m) / (67.28380m * 67.28380m), model.level3_nodes[9].value); Assert.AreEqual(3 / 12.23456m, model.level3_nodes[10].value); Assert.AreEqual(12.23456m / 23.45678m, model.level3_nodes[11].value); Assert.AreEqual((67.28380m * 67.28380m) / (23.45678m * 23.45678m), model.level3_nodes[12].value); //verify the value of Level 2 foreach (var node in model.level2_nodes) { Assert.AreEqual(0, node.value); } //verify the value of Level 1 foreach (var node in model.level1_nodes) { Assert.AreEqual(0, node.value); } ////裁剪模型的情况 //model.CreateModel(); //GlobalData.GlobalData.globalData.if_assess_modularity = false; //model.TrimModel(); //model.CalculateModelWeight(); //mv.SetLevel4UnitValue(model.level4_nodes); //mv.SetLevel3UnitValue(model.level3_nodes); //mv.SetLevel3UnitValue(model.level2_nodes); //mv.SetLevel3UnitValue(model.level1_nodes); ////验证第3层的值 //Assert.AreEqual(17.28380m / 12.23456m, model.level3_nodes[0].value); //Assert.AreEqual(12.23456m / 23.45678m, model.level3_nodes[1].value); //Assert.AreEqual(42.71615m / (12.23456m * 12.23456m - 12.23456m), model.level3_nodes[2].value); //Assert.AreEqual((67.28380m + 92.71615m) / 12.23456m, model.level3_nodes[3].value); //Assert.AreEqual(17.28380m / 12.23456m, model.level3_nodes[4].value); //Assert.AreEqual(12.23456m / 23.45678m, model.level3_nodes[5].value); //Assert.AreEqual((67.28380m * 67.28380m) / (23.45678m * 23.45678m), model.level3_nodes[6].value); //Assert.AreEqual((117.28380m * 117.28380m) / (67.28380m * 67.28380m), model.level3_nodes[7].value); //Assert.AreEqual(3 / 12.23456m, model.level3_nodes[8].value); //Assert.AreEqual((117.28380m * 117.28380m) / (67.28380m * 67.28380m), model.level3_nodes[9].value); //Assert.AreEqual(3 / 12.23456m, model.level3_nodes[10].value); ////验证第2层的值 //foreach (var node in model.level2_nodes) // Assert.AreEqual(0, node.value); ////验证第1层的值 //foreach (var node in model.level1_nodes) // Assert.AreEqual(0, node.value); }
public virtual ModelFitness Evaluate(List <ModelDataSet> data, ModelValue prediction) { return(default(ModelFitness)); }
// train public Model(IEnumerable <ModelDataSet> data, ModelValue prediction) { }
// train public ModelMLNet(IEnumerable <ModelDataSet> data, ModelValue prediction) { #if ML_LEGACY var pipeline = new LearningPipeline(); // add data pipeline.Add(CollectionDataSource.Create(data)); // choose what to predict switch (prediction) { case ModelValue.Action: pipeline.Add(new ColumnCopier(("Action", "Label"))); break; case ModelValue.Angle: pipeline.Add(new ColumnCopier(("FaceAngle", "Label"))); break; case ModelValue.XY: pipeline.Add(new ColumnCopier(("MoveAngle", "Label"))); break; default: throw new Exception("Unknown prediction : " + prediction); } // add columns as features // do not include the features which should be predicted pipeline.Add(new ColumnConcatenator("Features", "CenterAngle", "InZone", "Health", "Shield", "Primary", "PrimaryAmmo", "PrimaryClip", "Secondary", "SecondaryAmmo", "SecondaryClip", // "Ammo", "Angle_1", "Distance_1", // "Bandage", "Angle_2", "Distance_2", // "Helmet", "Angle_3", "Distance_3", // "Ak47", "Angle_4", "Distance_4", // "Shotgun", "Angle_5", "Distance_5", // "Pistol", "Angle_6", "Distance_6", // "Obstacle", "Angle_7", "Distance_7", // "Player", "Angle_8", "Distance_8" )); // add a classifier // action 0.25, xy 84.9777, angle 71.591 //pipeline.Add(new FastTreeRegressor()); pipeline.Add(new FastTreeRegressor() { // NumTrees // 100 - default // 100 - xy 79 // 1000 - xy 63, angle 52 (slow) NumTrees = 100, // NumLeaves // 50 - default // 100 - xy 63, angle 71 // 1000 - xy 59, angle 49 (slow) NumLeaves = 50, // NumThreads // 5 - default NumThreads = 50, // EntropyCoe // 0.30 - default // 0.70 - xy 79, angle 66 // 0.99 - xy 79, angle 66 // 0.05 - xy 79, angle 66 EntropyCoefficient = 0.3 }); // action 0.26 xy 85.1606, angle 72.5194 //pipeline.Add(new FastTreeTweedieRegressor()); // took too long //pipeline.Add(new GeneralizedAdditiveModelRegressor()); // runtime exception //pipeline.Add(new LightGbmRegressor()); // action 0.4736, xy 105.8815, angle 91.2677 //pipeline.Add(new OnlineGradientDescentRegressor()); // runtime exception //pipeline.Add(new OrdinaryLeastSquaresRegressor()); // action 0.4628, xy 106.0547, angle 91.0179 //pipeline.Add(new PoissonRegressor()); // action 0.4599, xy 105.8474, angle 90.4134 //pipeline.Add(new StochasticDualCoordinateAscentRegressor()); // train the model TrainedModel = pipeline.Train <ModelDataSet, ModelDataSetPrediction>(); #else Context = new MLContext(); // add data var textLoader = GetTextLoader(Context, prediction); // spill to disk !?!?! since there is no way to load from a collection var pathToData = ""; try { // write data to disk pathToData = WriteToDisk(data, prediction); // read in data IDataView dataView = textLoader.Read(pathToData); // configurations var label = ""; switch (prediction) { case ModelValue.Action: label = "Action"; break; case ModelValue.Angle: label = "FaceAngle"; break; case ModelValue.XY: label = "MoveAngle"; break; default: throw new Exception("Unknown value for prediction : " + prediction); } var dataPipeline = Context.Transforms.CopyColumns(label, "Label") .Append(Context.Transforms.Concatenate("Features", ColumnNames(false /* do not include the label */, prediction))); // set the training algorithm var trainer = Context.Regression.Trainers.FastTree(label: "Label", features: "Features"); var trainingPipeline = dataPipeline.Append(trainer); TrainedModel = trainingPipeline.Fit(dataView); } finally { // cleanup if (!string.IsNullOrWhiteSpace(pathToData) && File.Exists(pathToData)) { File.Delete(pathToData); } } #endif }
public void SetLevel4UnitValueTest() { Model model = new Model(); ModelValue mv = new ModelValue(); SetModelValueSample(mv); //calculate Level 4 values mv.CalculateLevel4Values(); model.CreateModel(); //GlobalData.GlobalData.globalData.if_assess_modularity = true; //model.TrimModel(); model.CalculateModelWeight(); mv.SetLevel4UnitValue(model.level4_nodes); mv.SetLevel4UnitValue(model.level3_nodes); mv.SetLevel4UnitValue(model.level2_nodes); mv.SetLevel4UnitValue(model.level1_nodes); //verify the value of Level 4 Assert.AreEqual(12.23456m, model.level4_nodes[0].value); Assert.AreEqual(17.28380m, model.level4_nodes[1].value); Assert.AreEqual(12.23456m, model.level4_nodes[2].value); Assert.AreEqual(23.45678m, model.level4_nodes[3].value); Assert.AreEqual(12.23456m, model.level4_nodes[4].value); Assert.AreEqual(42.71615m, model.level4_nodes[5].value); Assert.AreEqual(12.23456m, model.level4_nodes[6].value); Assert.AreEqual(67.28380m, model.level4_nodes[7].value); Assert.AreEqual(92.71615m, model.level4_nodes[8].value); Assert.AreEqual(12.23456m, model.level4_nodes[9].value); Assert.AreEqual(17.28380m, model.level4_nodes[10].value); Assert.AreEqual(12.23456m, model.level4_nodes[11].value); Assert.AreEqual(23.45678m, model.level4_nodes[12].value); Assert.AreEqual(12.23456m, model.level4_nodes[13].value); Assert.AreEqual(23.45678m, model.level4_nodes[14].value); Assert.AreEqual(67.28380m, model.level4_nodes[15].value); Assert.AreEqual(23.45678m, model.level4_nodes[16].value); Assert.AreEqual(117.28380m, model.level4_nodes[17].value); Assert.AreEqual(67.28380m, model.level4_nodes[18].value); Assert.AreEqual(12.23456m, model.level4_nodes[19].value); Assert.AreEqual(3, model.level4_nodes[20].value); Assert.AreEqual(23.45678m, model.level4_nodes[21].value); Assert.AreEqual(117.28380m, model.level4_nodes[22].value); Assert.AreEqual(67.28380m, model.level4_nodes[23].value); Assert.AreEqual(12.23456m, model.level4_nodes[24].value); Assert.AreEqual(3, model.level4_nodes[25].value); Assert.AreEqual(12.23456m, model.level4_nodes[26].value); Assert.AreEqual(23.45678m, model.level4_nodes[27].value); Assert.AreEqual(12.23456m, model.level4_nodes[28].value); Assert.AreEqual(23.45678m, model.level4_nodes[29].value); Assert.AreEqual(67.28380m, model.level4_nodes[30].value); //verify the value of Level 3 foreach (var node in model.level3_nodes) { Assert.AreEqual(0, node.value); } //verify the value of Level 2 foreach (var node in model.level2_nodes) { Assert.AreEqual(0, node.value); } //verify the value of Level 1 foreach (var node in model.level1_nodes) { Assert.AreEqual(0, node.value); } ////裁剪模型的情况 //model.CreateModel(); //GlobalData.GlobalData.globalData.if_assess_modularity = false; //model.TrimModel(); //model.CalculateModelWeight(); //mv.SetLevel4UnitValue(model.level4_nodes); //mv.SetLevel4UnitValue(model.level3_nodes); //mv.SetLevel4UnitValue(model.level2_nodes); //mv.SetLevel4UnitValue(model.level1_nodes); ////验证第4层的值 //Assert.AreEqual(12.23456m, model.level4_nodes[0].value); //Assert.AreEqual(17.28380m, model.level4_nodes[1].value); //Assert.AreEqual(12.23456m, model.level4_nodes[2].value); //Assert.AreEqual(23.45678m, model.level4_nodes[3].value); //Assert.AreEqual(12.23456m, model.level4_nodes[4].value); //Assert.AreEqual(42.71615m, model.level4_nodes[5].value); //Assert.AreEqual(12.23456m, model.level4_nodes[6].value); //Assert.AreEqual(67.28380m, model.level4_nodes[7].value); //Assert.AreEqual(92.71615m, model.level4_nodes[8].value); //Assert.AreEqual(12.23456m, model.level4_nodes[9].value); //Assert.AreEqual(17.28380m, model.level4_nodes[10].value); //Assert.AreEqual(12.23456m, model.level4_nodes[11].value); //Assert.AreEqual(23.45678m, model.level4_nodes[12].value); //Assert.AreEqual(12.23456m, model.level4_nodes[13].value); //Assert.AreEqual(23.45678m, model.level4_nodes[14].value); //Assert.AreEqual(67.28380m, model.level4_nodes[15].value); //Assert.AreEqual(23.45678m, model.level4_nodes[16].value); //Assert.AreEqual(117.28380m, model.level4_nodes[17].value); //Assert.AreEqual(67.28380m, model.level4_nodes[18].value); //Assert.AreEqual(12.23456m, model.level4_nodes[19].value); //Assert.AreEqual(3, model.level4_nodes[20].value); //Assert.AreEqual(23.45678m, model.level4_nodes[21].value); //Assert.AreEqual(117.28380m, model.level4_nodes[22].value); //Assert.AreEqual(67.28380m, model.level4_nodes[23].value); //Assert.AreEqual(12.23456m, model.level4_nodes[24].value); //Assert.AreEqual(3, model.level4_nodes[25].value); ////验证第3层的值 //foreach (var node in model.level3_nodes) // Assert.AreEqual(0, node.value); ////验证第2层的值 //foreach (var node in model.level2_nodes) // Assert.AreEqual(0, node.value); ////验证第1层的值 //foreach (var node in model.level1_nodes) // Assert.AreEqual(0, node.value); }
public Value(ModelValue model, double value = 0) { this.model = model; this.value = value; }
public string GetRawModelXML(ModelValue model) { return((model?.NameVersion?.CCacheId as NodeSetFileModel)?.FileCache); }
protected ModelValue DetermineParameterValue(ModelParameter parameter) { ModelValues values = new ModelValues(); values.Add(parameter.Values); //Parameter values, can be specified in numerous ways: // 1. Value list - simply choose one of them // 2. Expression - generate value that meets the criteria (ie: <, >, !=, etc) // 3. Variable - simply obtain the value by calling a method/field //#3. Variable - simply obtain the value by calling a method/field if(parameter.Variable != null) { object current = parameter.Variable.CachedValue; if(current is IEnumerable && !typeof(IEnumerable).IsAssignableFrom(parameter.Type)) { foreach(object v in (IEnumerable)current) values.Add(new ModelValue(v)); } else { values.Add(new ModelValue(current)); } } //First ensure we have a set of values/requirements to choose from if(values.Count <= 0) throw new ModelException(parameter, "No values specified to choose from"); //Note: Since we allow the operator on the individual values, this is a little more complex. //Note: We allow multiple operators, not just one, (ie: x > 5, < 10, and != 7). //This gives you great power and flexibility in expressions, but means we have to work a //little hard in determing a value that meets the requirements //#1. Value list - simply choose one of them //Note: Bitmask is already exploded into combinations ModelValues equalvalues = values.FindOperator(ModelValueOperator.Equal); //#2. Expression - generate value that meets the criteria (ie: <, >, !=, etc) //Note: Since we allow the operator on the individual values, this is a little more complex. //Note: We allow multiple operators, not just one, (ie: x > 5, < 10, and != 7). //This gives you great power and flexibility in expressions, but means we have to work a //little hard in determing a value that meets the requirements. int min = Int32.MinValue; int max = Int32.MaxValue; //Adjust our parameter, simplier to loop over all of them foreach(ModelValue value in values.FindOperator(ModelValueOperator.Equal, false).FindOperator(ModelValueOperator.NotEqual, false)) { //To keep this simple, we just support integers (for now). if(!(value.Value is int || value.Value is Nullable<int>)) throw new ModelException(parameter, "Generated value range must be specified in terms of integers, not '" + value.Type + "'"); //Simplify our life int v = (int)value.Value; //Adjust Max (if there is one) switch(value.Operator) { case ModelValueOperator.LessThanOrEqual: if(v < max) max = v; break; case ModelValueOperator.GreaterThanOrEqual: if(v > min) min = v; break; case ModelValueOperator.LessThan: if(v-1 < max && v > Int32.MinValue/*prevent underflow*/) max = v-1; break; case ModelValueOperator.GreaterThan: if(v+1 > min && v < Int32.MaxValue/*prevent overflow*/) min = v+1; break; }; } //Choose a new value, within the specified range //Note: We retry in case it equals one of the existing invalid values (ie: !=) while(true) { ModelValue choice = null; if(equalvalues.Count > 0) { //Simple: Choose one of the specified values choice = new ModelValue(equalvalues.Choose(this).Value); } else { //Otherwise: Choose a value within in the range //Note: Random.Next = min <= x < max (so we have to add one) int index = _options.Random.Next(min, max < Int32.MaxValue ? max + 1 : max); //Prevent overflow choice = new ModelValue(index); } //As soon as we find a value, within the range, (and not in the invalid list), were done bool valid = true; foreach(ModelValue invalid in values.FindOperator(ModelValueOperator.NotEqual)) { if(invalid.Evaluate(choice.Value, ModelValueOperator.Equal)) valid = false; } if(valid) return choice; } }
// train public ModelOpenCV(List <ModelDataSet> input, ModelValue prediction) { if (input == null || input.Count == 0) { throw new Exception("Must have valid input"); } // convert features into proper form var features = new float[input.Count, input[0].Features()]; var labels = new float[input.Count]; for (int i = 0; i < input.Count; i++) { for (int j = 0; j < input[i].Features(); j++) { features[i, j] = input[i].Feature(j); } switch (prediction) { case ModelValue.Action: labels[i] = input[i].Action; break; case ModelValue.Angle: labels[i] = input[i].FaceAngle; break; case ModelValue.XY: labels[i] = input[i].MoveAngle; break; default: throw new Exception("Unknown prediction type : " + prediction); } } // train var labelInput = InputArray.Create <float>(labels); var dataInput = InputArray.Create <float>(features); TrainedModel = RTrees.Create(); TrainedModel.RegressionAccuracy = 0.00001f; // RTrees.MaxDepath (r^2) // default - action 0.3424, xy 0.1735, angle 0.2208 // 5 - // 20 - action 0.6482, xy 0.5912, angle 0.6414 (new default) // 100 - action 0.6408, xy 0.5914, angle 0.6419 TrainedModel.MaxDepth = 20; // RTress.MinSampleCount // default(10) - see 20 above // 1 - actions 0.6625, xy 0.5077, angle 0.6376 // 50 - actions 0.6464, xy 0.5627, angle 0.6217 //TrainedModel.MinSampleCount = 1; // fails //TrainedModel = LogisticRegression.Create(); // fails // TrainedModel = DTrees.Create(); // failed //TrainedModel = SVM.Create(); //TrainedModel.KernelType = SVM.KernelTypes.Linear; //TrainedModel.Type = SVM.Types.NuSvr; //TrainedModel.C = 1; //TrainedModel.P = 0.01; //TrainedModel.Gamma = 10f; //TrainedModel.Degree = 0.1; //TrainedModel.Coef0 = 0; //TrainedModel.Nu = 0.1; TrainedModel.Train(dataInput, SampleTypes.RowSample, labelInput); }
//Constructor public ModelExpression(ModelFunction func, ModelValue value) : base(null, value) { _func = func; }
public void LearnPS(int N, int episodes) { subgoalVar = new List <double>(); forceStop = false; //int stateHold = currentState; float alpha = 0.2f, gamma = 0.95f, theta = 0.001f; int s = currentState; //Initialize List <PriorityObj> PQueue = new List <PriorityObj>(); visit = new Dictionary <int, int>(); int epsilon_counter = 0; int steps = 0; while (true) { if (forceStop) { break; } if (epsilon_counter++ % 100 == 0) { epsilon_counter = 1; e = 0.01 + 0.7 * Math.Pow(Math.E, -(discount++) / 20.0); } int tempSteps = steps; //Number of steps per episode if (EpisodeSteps.Count > 1500) { forceStop = true; } //(b) a <- policy(s,Q) //int a = e_greedy(Q[s]); int a = softmaxWVisits(Q[s]); // Q[s][a] -= -0.001; double tempVal = Q[s][a]; //tempFirstVisitHist[s[0], s[1]] += 1; //(c) Execute action a int sP = 0; double r = 0; Context curCtxt = currentContext; lock (threadLock) { executeAction(s, a, ref sP, ref r, ref steps); } if (!visit.ContainsKey(sP)) { visit.Add(sP, 0); } Context nexCtxt = currentContext; if (curCtxt != nexCtxt) { //double tmp = curCtxt.Q[s][Q[s].GetMaxAction()]; //for (int i = 0; i < curCtxt.Q[s].Actions.Count; i++) //{ // Q[s][i] += curCtxt.Q[s][i];//tmp / 8; //} continue; } //SwitchContext(curCtxt); int result = sP; //(d) Model(s,a) <- s', r //Model[s[0], s[1], a] = new int[] { sP[0], sP[1], r, steps - tempSteps }; if (Model[s, a] == null) { ModelValue m = new ModelValue(s, a, sP, r, steps - tempSteps); Model[s, a] = m; } else if (Model[s, a].calculateEm(sP, r) > 0) { Model[s, a].Update(s, a, sP, r, steps - tempSteps); } //System.Diagnostics.Debug.WriteLine("\tContext: " + currentContext.cID + "\t(s,a,sp): " + s + ", " + a + ", " + sP + "\tTm0: " /*+ (Model[s, a].Tm.Count > 0 ? Model[s,a].Tm[0].ToString() : " ") */ + "\tem0: " + em + "\tEm0: " + contexts[0].Em + "\tEm1: " + (contexts.Count > 1 ? contexts[1].Em.ToString() : "-")); //(e) p <- |r + gama*maxA'Q(s',a') - Q(s,a)| //float p = Math.Abs(r + gamma * getQ(sP[0], sP[1], maxA(sP)) - getQ(s[0], s[1], a)); double p = Math.Abs(r + gamma * Q[sP][Q[sP].GetMaxAction()] - Q[s][a]); //(f) if p > tetha, then insert s,a into PQueue with priority p PQueue.Clear(); if (p > theta) { InsertQueue(PQueue, s, a, p); } //(g) Repeat N times while PQueue is not empty for (int i = 0; i < N && 0 < PQueue.Count; i++) { //(-)s, a <- first(PQueue) PriorityObj obj = PQueue[0];// dene!!!!!!!!!!!!11 PQueue.Remove(obj); s = obj.State; a = obj.Action; //(-)s', r <- Modell(s,a) sP = Model[s, a].sP; r = Model[s, a].reward; int tsteps = Model[s, a].steps; //(-)Q(s, a) <- Q(s,a) + alpha[r + gama*maxA'Q(s',a') - Q(s,a)] //e_updates.Add(new double[] { s, a, Q[s][a] }); Q[s][a] = Q[s][a] + alpha * (r + Math.Pow(gamma, tsteps) * Q[sP][Q[sP].GetMaxAction()] - Q[s][a]); //float t = (float)(getQ(s[0], s[1], a) + alpha * (r + Math.Pow(gamma, tsteps) * getQ(sP[0], sP[1], maxA(sP)) - getQ(s[0], s[1], a))); //setQ(s[0], s[1], a, t); //(-)Repeat, for all s",a" predicted to lead to s List <ModelValue> list = Model.All_SA_PredictedToLeadTo(s); for (int j = 0; j < list.Count; j++) { int sDP = list[j].s; int aDP = list[j].a; // r" <- predicted reward double rDP = list[j].reward; // p <- |r" + gamma*maxaQ(s,a) - Q(s",a")| p = Math.Abs(rDP + gamma * Q[s][Q[s].GetMaxAction()] - Q[sDP][aDP]); // if p > tetha, then insert s",a" into PQueue with priority p if (p > theta) { InsertQueue(PQueue, sDP, aDP, p); } } } s = result; environment.State = s; if (environment.isGoal(s)) { //visit = new Dictionary<int, int>();/////////// //if (subgoalValues != null) //{ // subgoalValues[s] = 0.1; // for (int f = 0; f < 4; f++) // { // if (Model.StateCount < s) // Model.States.Add(new ModelState(s)); // if (Model[s] == null) // Model[s] = new ModelState(s); // if(Model[s].Count <= 4) // Model[s].Add(new ModelValue(s, f, s, 0, 0)); // } //} if (Model.States.Count <= s) { Model[s, a] = null; Model.States[s] = new ModelState(s); } //e = 0.9 * Math.Pow(Math.E, -(discount++) / 50.0); //Update epsilon if (!DisableOptions && !m_optsLearned) { //initiationSetMembers = new int[Model.StateCount]; //initiationSets = new List<InitiationSet>(); Subgoals = SearchSubgoals(); //Subgoals.Add(new SubGoal(s)); SubgoalCounts.Add(Subgoals.Count); double mean = 0; double std = 100; int sc = 10; if (SubgoalCounts.Count > sc) { std = 0; for (int c = SubgoalCounts.Count - sc; c < SubgoalCounts.Count; c++) { mean += SubgoalCounts[c] / (double)sc; } for (int c = SubgoalCounts.Count - sc; c < SubgoalCounts.Count; c++) { std += Math.Pow(SubgoalCounts[c] - mean, 2) / sc; } subgoalVar.Add(std); std = Math.Sqrt(std); } else { subgoalVar.Add(-1); } if (std < 0.01) { List <OptionN> options = createOptFromSubGoals(Subgoals); currentContext.options = options; optNonLearnedVals = 0; optLearnedVals = 0; for (int i = 0; i < Model.StateCount; i++) { if (Model[i] == null) { optNonLearnedVals += 4; } else { for (int j = 0; j < Model[i].Count; j++) { if (Model[i][j] == null || Q[i][j] == 0) { optNonLearnedVals += 1; } else { optLearnedVals += 1; } } } } //SearchInitiationSets(Subgoals); //Bu satır if in içinde olmalı (ki öyle(değil mi?)) //List<Option> options = CreateOptions(); OptionLearn(options); //currentContext.options = options; } } s = currentState; EpisodeSteps.Add(steps); ContextTrack.Add(currentContext.cID); currentContext.EpisodeSteps.Add(steps); steps = 0; int calcLength = 9; if (currentContext.EpisodeSteps.Count > calcLength) { double mean = 0; double std = 0; for (int c = currentContext.EpisodeSteps.Count - calcLength; c < currentContext.EpisodeSteps.Count; c++) { mean += EpisodeSteps[c] / (double)calcLength; } for (int c = currentContext.EpisodeSteps.Count - calcLength; c < currentContext.EpisodeSteps.Count; c++) { std += Math.Pow(currentContext.EpisodeSteps[c] - mean, 2) / calcLength; } std = Math.Sqrt(std); currentContext.std = std; bool stop = true; for (int cx = 0; cx < contexts.Count; cx++) { if (contexts[cx].std > 10) { stop = false; } //System.Diagnostics.Debug.Write(cx + ":" + contexts[cx].std + " "); } //System.Diagnostics.Debug.Write("\n"); if (stopByEpisode) { if (stopEpisode <= EpisodeSteps.Count) { break; } } else { if (stop || forceStop) { break; } } }//Varyans ile dene //if (!m_optsLearned) //{ // OptionSearch(); // OptionLearn(); //} PQueue.Clear(); if (curCtxt != nexCtxt) { SwitchContext(nexCtxt); } if (oneStepLearn) { break; } e_updates = new List <double[]>(); } } environment.State = currentState; }
public DataAnalyze(JSONDataStruct.Rootobject datasource, ModelValue mv) { data = datasource; modelValue = mv; }
public static Value Max(ModelValue model) { return(new Value(model, model.Max)); }
public void Add(ModelValue value) { modelValues.Add(value); }