mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-27 05:23:13 +00:00
Soft Computing
This commit is contained in:
@ -15,7 +15,6 @@ namespace Esiur.Analysis.DSP
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
public static double[] Convolve(this double[] signal, double[] filter)
|
||||
{
|
||||
var length = signal.Length + filter.Length - 1;
|
||||
|
@ -66,7 +66,11 @@ namespace Esiur.Analysis.Fuzzy
|
||||
{
|
||||
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);
|
||||
var total = r.Sum(x => x.Value);
|
||||
if (total == 0)
|
||||
return 0;
|
||||
else
|
||||
return r.Sum(x => x.Key * x.Value ) / total;
|
||||
}
|
||||
|
||||
public KeyValuePair<double, double>[] Minimas
|
||||
@ -78,6 +82,6 @@ namespace Esiur.Analysis.Fuzzy
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public double[] ToArray() => vector.Values.ToArray();
|
||||
}
|
||||
}
|
||||
|
@ -41,5 +41,42 @@ namespace Esiur.Analysis.Fuzzy
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
public static double[] Sample(this INumericalSet<double> set, double[] time)
|
||||
{
|
||||
var rt = new double[time.Length];
|
||||
for (var i = 0; i < time.Length; i++)
|
||||
rt[i] = set[time[i]];
|
||||
return rt;
|
||||
}
|
||||
|
||||
public static double[] Sample(this INumericalSet<double> set, double from, double to, double step)
|
||||
{
|
||||
var size = (int)((to - from) / step);
|
||||
|
||||
var rt = new double[size];
|
||||
var s = 0;
|
||||
for (var i = from; i < to && s < size; i+=step)
|
||||
rt[s++] = set[i];
|
||||
return rt;
|
||||
|
||||
}
|
||||
|
||||
public static double[] Range(double from, double to, double step)
|
||||
{
|
||||
var size = (int)((to - from) / step);
|
||||
|
||||
if (size == 0)
|
||||
return new double[] { from };
|
||||
|
||||
var rt = new double[size];
|
||||
var s = 0;
|
||||
for (var i = from; i < to && s < size; i += step)
|
||||
rt[s++] = i;
|
||||
return rt;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ namespace Esiur.Analysis.Fuzzy
|
||||
{
|
||||
return new MembershipFunction(x =>
|
||||
{
|
||||
if (x <= peak) return 1;
|
||||
if (x <= peak) return 0;
|
||||
if (x > peak && x < end) return (end - x) / (end - peak);
|
||||
return 0;
|
||||
});
|
||||
@ -33,7 +33,7 @@ namespace Esiur.Analysis.Fuzzy
|
||||
{
|
||||
return new MembershipFunction(x =>
|
||||
{
|
||||
if (x >= peak) return 1;
|
||||
if (x >= peak) return 0;
|
||||
if (x < peak && x > start) return (x - start) / (peak - start);
|
||||
return 0;
|
||||
});
|
||||
|
@ -88,23 +88,25 @@ namespace Esiur.Analysis.Optimization
|
||||
|
||||
}
|
||||
|
||||
public T Evaluate(int maxIterations)
|
||||
public IEnumerable<(int, double, T)> Evaluate(int maxIterations)
|
||||
{
|
||||
GeneratePopultation();
|
||||
|
||||
var generation = 0;
|
||||
|
||||
T best;
|
||||
KeyValuePair<T, double> best;
|
||||
|
||||
do
|
||||
{
|
||||
var ordered = GetFitness().OrderBy(x => x.Value).ToArray();
|
||||
|
||||
best = ordered[0].Key;
|
||||
|
||||
if (ordered[0].Value == 0)
|
||||
best = ordered[0];
|
||||
|
||||
if (best.Value == 0)
|
||||
break;
|
||||
|
||||
yield return (generation, best.Value, best.Key);
|
||||
|
||||
// Elitism selection ( 10% of fittest population )
|
||||
|
||||
var eliteCount = (int)(ordered.Length * 0.1);
|
||||
@ -124,12 +126,14 @@ namespace Esiur.Analysis.Optimization
|
||||
Population = newGeneration;
|
||||
|
||||
Debug.WriteLine($"Gen {generation} Fittest: {ordered.First().Value} {ordered.First().Key.ToString()} ");
|
||||
|
||||
|
||||
} while (generation++ < maxIterations);
|
||||
|
||||
Debug.WriteLine($"Gen {generation} Best: {best.ToString()} ");
|
||||
|
||||
return best;
|
||||
yield return (generation, best.Value, best.Key);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user