private static void CheckType2(int stage = 3) { List<FuzzyVariable> inputVariables = DataProvider.DataProvider.GetInputVariables(); FuzzyVariable outputVariable = DataProvider.DataProvider.GetOutputVariable(); RulesList ruleDefinitions = DataProvider.DataProvider.GetRulesDefinitions(); List<string> includedVariables = DataProvider.DataProvider.GetIncludedVariables(stage); var decisionMakerT1 = new DecisionMakerType1(inputVariables, outputVariable, ruleDefinitions, includedVariables); // DataProvider.DataProvider.SaveInputVariables(decisionMakerT1.SerializableParameters); var decisionMakerT2 = new DecisionMakerType2(decisionMakerT1); var learningDataSet = DataProvider.DataProvider.GetLearningDataSet(); decisionMakerT2.Initialize(learningDataSet); Console.WriteLine("Initialized the type-2 FLS."); // DataProvider.DataProvider.SaveInputVariables(decisionMakerT2.SerializableParameters); Console.WriteLine("Learning data set ({0} vectors):", learningDataSet.Count); int i = 1; foreach (MeasurementWithResult vector in learningDataSet) { var result = decisionMakerT2.Infer(vector.InputValues); Console.WriteLine( "X{0}: [{1}; {2}]", i, Math.Round(result.Lower, 2), Math.Round(result.Upper, 2)); i++; } var testDataSet = DataProvider.DataProvider.GetTestDataSet(); Console.WriteLine("Test data set ({0} vectors):", testDataSet.Count); i = 1; foreach (MeasurementWithResult vector in testDataSet) { var result = decisionMakerT2.Infer(vector.InputValues); Console.WriteLine( "X{0}: [{1}; {2}]", i, Math.Round(result.Lower, 2), Math.Round(result.Upper, 2)); i++; } }
private static MamdaniFuzzySystem UpdateParamsByMultiplier( DecisionMakerType1 originalDecisionMaker, double multiplier, VariableMFParam paramToUpdate, out bool canStretchMore) { canStretchMore = false; // Create empty fuzzy system MamdaniFuzzySystem newFs = new MamdaniFuzzySystem(); // Create input and output variables for the system foreach (var inputVariable in originalDecisionMaker.fsWellEval.Input) { FuzzyVariable fsVariable = new FuzzyVariable(inputVariable.Name, inputVariable.Min, inputVariable.Max); foreach (var term in inputVariable.Terms) { var mf = term.MembershipFunction as NormalMembershipFunction; if (paramToUpdate == VariableMFParam.Mean) { if (mf != null && (mf.B * multiplier >= fsVariable.Min && mf.B * multiplier <= fsVariable.Max)) { fsVariable.Terms.Add( new FuzzyTerm(term.Name, new NormalMembershipFunction(mf.B * multiplier, mf.Sigma))); canStretchMore = true; } else { if (mf != null) { fsVariable.Terms.Add(new FuzzyTerm(term.Name, new NormalMembershipFunction(mf.B, mf.Sigma))); canStretchMore = false; } } } if (paramToUpdate == VariableMFParam.Deviation) { if (mf != null && (mf.Sigma * multiplier >= 0.001 && mf.Sigma * multiplier <= 10)) { fsVariable.Terms.Add( new FuzzyTerm(term.Name, new NormalMembershipFunction(mf.B, mf.Sigma * multiplier))); } else { if (mf != null) { fsVariable.Terms.Add(new FuzzyTerm(term.Name, new NormalMembershipFunction(mf.B, mf.Sigma))); } } } if (paramToUpdate == VariableMFParam.RuleWeight) { if (mf != null) { fsVariable.Terms.Add(new FuzzyTerm(term.Name, new NormalMembershipFunction(mf.B, mf.Sigma))); } } } newFs.Input.Add(fsVariable); } foreach (var outputVariable in originalDecisionMaker.fsWellEval.Output) { var newVariable = new FuzzyVariable( outputVariable.Name, outputVariable.Min, outputVariable.Max, outputVariable.Unit); newVariable.Terms.Clear(); newVariable.Terms.AddRange(outputVariable.Terms); newFs.Output.Add(newVariable); } for (int i = 0; i < originalDecisionMaker.RulesDefinitions.Items.Count; i++) { MamdaniFuzzyRule newRule = newFs.ParseRule(originalDecisionMaker.RulesDefinitions.Items[i].Definition); double oldWeight = originalDecisionMaker.fsWellEval.Rules[i].Weight; if (paramToUpdate == VariableMFParam.RuleWeight) { if (oldWeight * multiplier >= 0.001 && oldWeight * multiplier <= 1) { newRule.Weight = oldWeight * multiplier; } } else { newRule.Weight = oldWeight; } newFs.Rules.Add(newRule); } return newFs; }
public static List<FuzzyVariable> Stretch( List<MeasurementWithResult> learningSet, DecisionMakerType1 originalDecisionMaker, VariableMFParam toStretch, BoundType boundType) { List<MeasurementWithResult> withResultsFromFs = (from measurement in learningSet select new MeasurementWithResult { InputValues = new Dictionary<string, double>(measurement.InputValues), OutputString = originalDecisionMaker.InferTerm(measurement.InputValues) }).ToList(); double multiplier = 1; int error = 0; bool canStretchMore = true; if (boundType == BoundType.Upper) { while (error == 0 && canStretchMore) { multiplier += 0.001; MamdaniFuzzySystem newFuzzySystem = UpdateParamsByMultiplier( originalDecisionMaker, multiplier, toStretch, out canStretchMore); var updatedDecisionMaker = new DecisionMakerType1(newFuzzySystem, originalDecisionMaker.RulesDefinitions); error = (from measurement in withResultsFromFs where updatedDecisionMaker.InferTerm(measurement.InputValues) != measurement.OutputString select measurement).Count(); } multiplier -= 0.001; var finalFuzzySystem = UpdateParamsByMultiplier( originalDecisionMaker, multiplier, toStretch, out canStretchMore); return finalFuzzySystem.Input; } else { while (error == 0 && canStretchMore) { multiplier -= 0.001; MamdaniFuzzySystem newFuzzySystem = UpdateParamsByMultiplier( originalDecisionMaker, multiplier, toStretch, out canStretchMore); var updatedDecisionMaker = new DecisionMakerType1(newFuzzySystem, originalDecisionMaker.RulesDefinitions); error = (from measurement in withResultsFromFs where updatedDecisionMaker.InferTerm(measurement.InputValues) != measurement.OutputString select measurement).Count(); } multiplier += 0.001; var finalFuzzySystem = UpdateParamsByMultiplier( originalDecisionMaker, multiplier, toStretch, out canStretchMore); return finalFuzzySystem.Input; } }
public ActionResult InitializeFLS() { try { // initialize the FLS List<FuzzyVariable> inputVariables = GetInputVariables(db); FuzzyVariable outputVariable = GetOutputVariable(db); var ruleDefinitions = new RulesList(); ruleDefinitions.Items = (from r in db.Rules select new RuleDef { Definition = r.Definition, Weight = r.Weight }).ToList(); var decisionMakerT1 = new DecisionMakerType1(inputVariables, outputVariable, ruleDefinitions); // DataProvider.DataProvider.SaveInputVariables(decisionMakerT1.SerializableParameters); var decisionMakerT2 = new DecisionMakerType2(decisionMakerT1); var learningDataSet = DataProvider.DataProvider.GetLearningDataSet(); decisionMakerT2.Initialize(learningDataSet); // DataProvider.DataProvider.SaveInputVariables(decisionMakerT2.SerializableParameters); var decisionMakerProperties = new DecisionMakerProperties { ParametersXml = DataProvider.DataProvider.WriteInputVariables(decisionMakerT2.SerializableParameters) }; db.Database.ExecuteSqlCommand("TRUNCATE TABLE DecisionMakerProperties"); db.SaveChanges(); db.DecisionMakerProperties.Add(decisionMakerProperties); db.SaveChanges(); return new HttpStatusCodeResult(HttpStatusCode.OK); } catch (Exception ex) { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.Message); } }
public ActionResult Infer(WorkspaceModel workspace) { try { List<FuzzyVariable> inputVariables = GetInputVariables(); FuzzyVariable outputVariable = GetOutputVariable(); RulesList ruleDefinitions = GetRulesDefinitions(); List<string> includedVariables = GetIncludedVariables(workspace.ParametersValues); var decisionMakerT1 = new DecisionMakerType1(inputVariables, outputVariable, ruleDefinitions, includedVariables); // DataProvider.DataProvider.SaveInputVariables(decisionMakerT1.SerializableParameters); var decisionMakerT2 = new DecisionMakerType2(decisionMakerT1); var learningDataSet = DataProvider.DataProvider.GetLearningDataSet(); decisionMakerT2.Initialize(learningDataSet); // var decisionMakerProperties = db.DecisionMakerProperties.Single(d => true); // var decisionMakerT2 = new DecisionMakerType2(decisionMakerProperties.ParametersXml, outputVariable, ruleDefinitions, inputVariables); Dictionary<string, double> vector = GetInputVector(workspace.ParametersValues); var result = decisionMakerT2.Infer(vector); var linguisticTerms = DecisionMakerType2.IntervalToLinguistic(result); ResultModel resultModel = ResultToModel(result, workspace.IsComplete, workspace.ID, workspace.Name, string.Join(", ", linguisticTerms)); return this.PartialView("_Result", resultModel); } catch (Exception ex) { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.Message); } }
public DecisionMakerType2(DecisionMakerType1 type1Source) { this.type1Source = type1Source; // input variables inputVariables = new List<Type2FuzzyVariable>(); foreach (var variable in type1Source.InputVariables) { Type2FuzzyVariable newVariable = new Type2FuzzyVariable( variable.Name, variable.Min, variable.Max, variable.Unit); foreach (var term in variable.Terms) { Type2FuzzyTerm newTerm = new Type2FuzzyTerm( term.Name, new NormalMembershipFunction( (term.MembershipFunction as NormalMembershipFunction).B, (term.MembershipFunction as NormalMembershipFunction).Sigma), new NormalMembershipFunction( (term.MembershipFunction as NormalMembershipFunction).B, (term.MembershipFunction as NormalMembershipFunction).Sigma)); newVariable.Terms.Add(newTerm); } inputVariables.Add(newVariable); } // output variable outputVariable = new Type2FuzzyVariable( type1Source.OutputVariable.Name, type1Source.OutputVariable.Min, type1Source.OutputVariable.Max, type1Source.OutputVariable.Unit); foreach (var term in type1Source.OutputVariable.Terms) { Type2FuzzyTerm newTerm = new Type2FuzzyTerm( term.Name, new NormalMembershipFunction( (term.MembershipFunction as NormalMembershipFunction).B, (term.MembershipFunction as NormalMembershipFunction).Sigma), new NormalMembershipFunction( (term.MembershipFunction as NormalMembershipFunction).B, (term.MembershipFunction as NormalMembershipFunction).Sigma)); outputVariable.Terms.Add(newTerm); } // rules rules = new List<Type2MamdaniFuzzyRule>(); foreach (var rule in type1Source.RulesDefinitions.Items) { Type2MamdaniFuzzyRule newRule = ParseRule(rule.Definition); newRule.Weight = rule.Weight; rules.Add(newRule); } isInitialized = false; }