2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 05:23:13 +00:00
This commit is contained in:
2022-10-30 18:43:45 +03:00
parent 584cd458de
commit e83f51f952
5 changed files with 46 additions and 18 deletions

View File

@ -9,7 +9,7 @@ namespace Esiur.Analysis.Fuzzy
{
public MembershipFunction Function { get; set; }
public double AlphaCut { get; set; } = double.MinValue;
public double AlphaCut { get; set; } = double.MaxValue;
@ -35,7 +35,7 @@ namespace Esiur.Analysis.Fuzzy
var results = Function(input);
return results < AlphaCut ? 0 : results;
return results > AlphaCut ? AlphaCut : results;
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
@ -48,12 +49,26 @@ namespace Esiur.Analysis.Fuzzy
public KeyValuePair<double, double>[] Maximas
{
get {
get
{
var max = vector.Values.Max();
return vector.Where(x => x.Value == max).ToArray();
}
}
public double Integral(double from, double to)
{
return vector.Where(x => x.Key >= from && x.Key <= to).Sum(x => x.Value);
}
public double Centroid(double from, double to)
{
var r = vector.Where(x => x.Key >= from && x.Key <= to).ToArray();
return r.Sum(x => x.Key * x.Value ) / r.Sum(x=>x.Value);
}
public KeyValuePair<double, double>[] Minimas
{
get
@ -63,5 +78,6 @@ namespace Esiur.Analysis.Fuzzy
}
}
}
}

View File

@ -10,7 +10,6 @@ namespace Esiur.Analysis.Fuzzy
CenterOfGravity,
FirstMaxima,
LastMaxima,
Bisector,
MeanOfMaxima,
}
public class MamdaniDefuzzifier
@ -21,9 +20,20 @@ namespace Esiur.Analysis.Fuzzy
var union = sets.FuzzyUnion();
var output = union.ToDiscrete(from, to, step);
var max = output.Maximas;
return max[0].Key;
if (method == MamdaniDefuzzifierMethod.CenterOfGravity)
return output.Centroid(from, to);
else if (method == MamdaniDefuzzifierMethod.FirstMaxima)
return output.Maximas.First().Key;
else if (method == MamdaniDefuzzifierMethod.LastMaxima)
return output.Maximas.Last().Key;
else if (method == MamdaniDefuzzifierMethod.MeanOfMaxima)
{
var max = output.Maximas;
return max.First().Key + ((max.Last().Key - max.First().Key) / 2);
}
else
throw new Exception("Unknown method");
}
}

View File

@ -19,7 +19,7 @@ namespace Esiur.Analysis.Fuzzy
public INumericalSet<double>[] Sets { get; internal set; }
public double AlphaCut { get; set; }
public double AlphaCut { get; set; } = double.MaxValue;
public double this[double index]
{
@ -32,7 +32,7 @@ namespace Esiur.Analysis.Fuzzy
x = Sets.Min(x => x[index]);
// Alpha might be changed for this instance
return x < AlphaCut ? 0 : x;
return x > AlphaCut ? AlphaCut : x;
}
}
@ -42,7 +42,7 @@ namespace Esiur.Analysis.Fuzzy
if (operation == Operation.Intersection)
AlphaCut = sets.Min(x => x.AlphaCut);
else if (Operation == Operation.Union)
else if (operation == Operation.Union)
AlphaCut = sets.Max(x => x.AlphaCut);
Operation = operation;

View File

@ -83,23 +83,25 @@ namespace Test
var excelent = new ContinuousSet(MembershipFunctions.Ascending(70, 100));
var small = new ContinuousSet(MembershipFunctions.Descending(100, 200));
var avg = new ContinuousSet(MembershipFunctions.Triangular(150, 200, 250));
var avg = new ContinuousSet(MembershipFunctions.Triangular(100, 200, 300));
var big = new ContinuousSet(MembershipFunctions.Ascending(200, 300));
//var speedIsLowThenSmall = new FuzzyRule("Low=>Small", low, small);
double temp = 34;
double rating = 70;
double rating = 80;
var v = MamdaniDefuzzifier.Evaluate(new INumericalSet<double>[]
for (double temp = 60; temp < 100; temp++)
{
temp.Is(low).Or(rating.Is(bad)).Then(small),
temp.Is(mid).Or(rating.Is(ok)).Then(avg),
temp.Is(high).Or(rating.Is(excelent)).Then(big),
}, MamdaniDefuzzifierMethod.FirstMaxima, 0, 600, 1);
var v = MamdaniDefuzzifier.Evaluate(new INumericalSet<double>[]
{
temp.Is(low).And(rating.Is(bad)).Then(small),
temp.Is(mid).And(rating.Is(ok)).Then(avg),
temp.Is(high).And(rating.Is(excelent)).Then(big),
}, MamdaniDefuzzifierMethod.CenterOfGravity, 100, 300, 1);
Console.WriteLine(v);
Console.WriteLine(temp + " " + v);
}
// Create stores to keep objects.
var system = await Warehouse.Put("sys", new MemoryStore());