mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-05-06 03:32:57 +00:00
Genetic
This commit is contained in:
parent
e83f51f952
commit
f1d5b0a38b
19
Esiur.Analysis.Test/Esiur.Analysis.Test.csproj
Normal file
19
Esiur.Analysis.Test/Esiur.Analysis.Test.csproj
Normal file
@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWindowsForms>true</UseWindowsForms>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ScottPlot.WinForms" Version="4.1.58" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Esiur.Analysis\Esiur.Analysis.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
72
Esiur.Analysis.Test/FMain.Designer.cs
generated
Normal file
72
Esiur.Analysis.Test/FMain.Designer.cs
generated
Normal file
@ -0,0 +1,72 @@
|
||||
namespace Esiur.Analysis.Test
|
||||
{
|
||||
partial class FMain
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.formsPlot1 = new ScottPlot.FormsPlot();
|
||||
this.button1 = new System.Windows.Forms.Button();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// formsPlot1
|
||||
//
|
||||
this.formsPlot1.Location = new System.Drawing.Point(57, 47);
|
||||
this.formsPlot1.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4);
|
||||
this.formsPlot1.Name = "formsPlot1";
|
||||
this.formsPlot1.Size = new System.Drawing.Size(1006, 576);
|
||||
this.formsPlot1.TabIndex = 0;
|
||||
//
|
||||
// button1
|
||||
//
|
||||
this.button1.Location = new System.Drawing.Point(12, 316);
|
||||
this.button1.Name = "button1";
|
||||
this.button1.Size = new System.Drawing.Size(85, 31);
|
||||
this.button1.TabIndex = 1;
|
||||
this.button1.Text = "button1";
|
||||
this.button1.UseVisualStyleBackColor = true;
|
||||
this.button1.Click += new System.EventHandler(this.button1_Click);
|
||||
//
|
||||
// FMain
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(1175, 658);
|
||||
this.Controls.Add(this.button1);
|
||||
this.Controls.Add(this.formsPlot1);
|
||||
this.Name = "FMain";
|
||||
this.Text = "PID Controller with Fuzzy";
|
||||
this.Load += new System.EventHandler(this.FMain_Load);
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private ScottPlot.FormsPlot formsPlot1;
|
||||
private Button button1;
|
||||
}
|
||||
}
|
345
Esiur.Analysis.Test/FMain.cs
Normal file
345
Esiur.Analysis.Test/FMain.cs
Normal file
@ -0,0 +1,345 @@
|
||||
using Esiur.Analysis.DSP;
|
||||
using Esiur.Analysis.Fuzzy;
|
||||
using Esiur.Analysis.Optimization;
|
||||
using Esiur.Analysis.Signals;
|
||||
using Esiur.Analysis.Units;
|
||||
using Microsoft.VisualBasic.Logging;
|
||||
using ScottPlot;
|
||||
using ScottPlot.Drawing.Colormaps;
|
||||
using System.Security.Cryptography;
|
||||
using Esiur.Analysis.Statistics;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Esiur.Analysis.Test
|
||||
{
|
||||
public partial class FMain : Form
|
||||
{
|
||||
public FMain()
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
//var outage = Capacity.ComputeOutage(20000000, new Capacity.CSI[]
|
||||
//{
|
||||
// new Capacity.CSI(PowerUnit.FromDb(20), 0.1),
|
||||
// new Capacity.CSI(PowerUnit.FromDb(15), 0.15),
|
||||
// new Capacity.CSI(PowerUnit.FromDb(10), 0.25),
|
||||
// new Capacity.CSI(PowerUnit.FromDb(5), 0.25),
|
||||
// new Capacity.CSI(PowerUnit.FromDb(0), 0.15),
|
||||
// new Capacity.CSI(PowerUnit.FromDb(-5), 0.1),
|
||||
//});
|
||||
var outage = Capacity.ComputeOutage(1, new Capacity.CSI[]
|
||||
{
|
||||
new Capacity.CSI(PowerUnit.FromDb(30), 0.2),
|
||||
new Capacity.CSI(PowerUnit.FromDb(20), 0.3),
|
||||
new Capacity.CSI(PowerUnit.FromDb(10), 0.3),
|
||||
new Capacity.CSI(PowerUnit.FromDb(0), 0.2),
|
||||
});
|
||||
|
||||
|
||||
var low = new ContinuousSet(MembershipFunctions.Descending(20, 40));
|
||||
var mid = new ContinuousSet(MembershipFunctions.Triangular(20, 40, 60));
|
||||
var high = new ContinuousSet(MembershipFunctions.Ascending(40, 60));
|
||||
|
||||
var bad = new ContinuousSet(MembershipFunctions.Descending(0, 30));
|
||||
var ok = new ContinuousSet(MembershipFunctions.Triangular(20, 50, 80));
|
||||
var excelent = new ContinuousSet(MembershipFunctions.Ascending(70, 100));
|
||||
|
||||
var small = new ContinuousSet(MembershipFunctions.Descending(100, 200));
|
||||
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 rating = 80;
|
||||
|
||||
for (double temp = 60; temp < 100; temp++)
|
||||
{
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void FMain_Load(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
var lowErr = new ContinuousSet(MembershipFunctions.Descending(-5, 4.5));
|
||||
var midErr = new ContinuousSet(MembershipFunctions.Triangular(-4, 6.5, 16.5));
|
||||
var highErr = new ContinuousSet(MembershipFunctions.Ascending(8, 18));
|
||||
|
||||
var lowAccErr = new ContinuousSet(MembershipFunctions.Descending(0, 0.02));
|
||||
var midAccErr = new ContinuousSet(MembershipFunctions.Triangular(0.02, 0.04, 0.06));
|
||||
var highAccErr = new ContinuousSet(MembershipFunctions.Ascending(0.04, 0.06));
|
||||
|
||||
var small = new ContinuousSet(MembershipFunctions.Descending(0.1, 0.5));
|
||||
var avg = new ContinuousSet(MembershipFunctions.Triangular(0.1, 0.5, 1.1));
|
||||
var big = new ContinuousSet(MembershipFunctions.Ascending(-10, 1.1));
|
||||
|
||||
|
||||
var x = Enumerable.Range(0, 1000).Select(x => x * 0.01).ToArray();
|
||||
|
||||
|
||||
var step = Enumerable.Repeat(1, 1000).Select(x => (double)x).ToArray();
|
||||
step[0] = 0;
|
||||
|
||||
var motor = new TransferFunction(new double[] { 1, 2 }, new double[] { 1, 1, 2 }, 0.01);
|
||||
var motorPID = new TransferFunction(new double[] { 1, 2 }, new double[] { 1, 1, 2 }, 0.01);
|
||||
var motorFuzzyPID = new TransferFunction(new double[] { 1, 2 }, new double[] { 1, 1, 2 }, 0.01);
|
||||
|
||||
//double Kp = 2, Ki = 0.4, Kd = 0.4;
|
||||
|
||||
double Ki = -1.9181372, Kp = 18.625, Kd = 0.38281253;
|
||||
|
||||
|
||||
var pid = new TransferFunction(new double[] { Kd, Kp, Ki }, new double[] { 1, 1 }, 0.01);
|
||||
var fuzzyPID = new TransferFunction(new double[] { Kd, Kp, Ki }, new double[] { 1, 1 }, 0.01);
|
||||
|
||||
|
||||
var sysOut = new double[step.Length];
|
||||
var sysOutFuzzyPID = new double[step.Length];
|
||||
var sysOutPID = new double[step.Length];
|
||||
|
||||
var pidOut = new double[step.Length];
|
||||
var pidOutFuzzy = new double[step.Length];
|
||||
var errorOutPID = new double[step.Length];
|
||||
|
||||
var errOut = new double[step.Length];
|
||||
var errAccOut = new double[step.Length];
|
||||
|
||||
//var errorAccOut = new double[step.Length];
|
||||
|
||||
var errorOutFuzzy = new double[step.Length];
|
||||
var errorOutAccFuzzy = new double[step.Length];
|
||||
|
||||
for (var i = 0; i < step.Length; i++)
|
||||
{
|
||||
sysOut[i] = motor.Evaluate(step[i]);
|
||||
errOut[i] = step[i] - sysOut[i];
|
||||
errAccOut[i] = errOut[i] - (i == 0 ? 0 : errOut[i - 1]);
|
||||
|
||||
sysOutPID[i] = motorPID.Evaluate(step[i] + (i == 0 ? 0 : pidOut[i - 1]));
|
||||
sysOutFuzzyPID[i] = motorFuzzyPID.Evaluate(step[i] + (i == 0 ? 0 : pidOutFuzzy[i - 1]));
|
||||
|
||||
|
||||
errorOutPID[i] = (step[i] - sysOutPID[i]);
|
||||
errorOutFuzzy[i] = (step[i] - sysOutFuzzyPID[i]);
|
||||
errorOutAccFuzzy[i] = (errorOutFuzzy[i] - (i == 0 ? 0 : errorOutFuzzy[i - 1]));
|
||||
|
||||
pidOut[i] = pid.Evaluate(errorOutPID[i]);
|
||||
pidOutFuzzy[i] = fuzzyPID.Evaluate(errorOutFuzzy[i]);
|
||||
|
||||
|
||||
var k = MamdaniDefuzzifier.Evaluate(new INumericalSet<double>[]
|
||||
{
|
||||
errorOutFuzzy[i].Is(lowErr).And(errorOutAccFuzzy[i].Is(lowAccErr)).Then(small),
|
||||
errorOutFuzzy[i].Is(lowErr).And(errorOutAccFuzzy[i].Is(midAccErr)).Then(small),
|
||||
errorOutFuzzy[i].Is(lowErr).And(errorOutAccFuzzy[i].Is(highAccErr)).Then(avg),
|
||||
errorOutFuzzy[i].Is(midErr).And(errorOutAccFuzzy[i].Is(lowAccErr)).Then(small),
|
||||
errorOutFuzzy[i].Is(midErr).And(errorOutAccFuzzy[i].Is(midAccErr)).Then(avg),
|
||||
errorOutFuzzy[i].Is(midErr).And(errorOutAccFuzzy[i].Is(highAccErr)).Then(big),
|
||||
errorOutFuzzy[i].Is(highAccErr).And(errorOutAccFuzzy[i].Is(lowAccErr)).Then(avg),
|
||||
errorOutFuzzy[i].Is(highAccErr).And(errorOutAccFuzzy[i].Is(midAccErr)).Then(big),
|
||||
errorOutFuzzy[i].Is(highAccErr).And(errorOutAccFuzzy[i].Is(highAccErr)).Then(big),
|
||||
}, MamdaniDefuzzifierMethod.CenterOfGravity, 0, 1, 0.05);
|
||||
|
||||
fuzzyPID.InputCoefficients[1] = k;
|
||||
fuzzyPID.InputCoefficients[1] = k;
|
||||
fuzzyPID.InputCoefficients[1] = k;
|
||||
}
|
||||
|
||||
Debug.WriteLine($"Error Values Min: {errOut.Min()} Max: {errOut.Max()} ");
|
||||
Debug.WriteLine($"Error Acc Values Min: {errAccOut.Min()} Max: {errAccOut.Max()} ");
|
||||
|
||||
formsPlot1.Plot.AddScatter(x, sysOut, Color.Red);
|
||||
formsPlot1.Plot.AddScatter(x, sysOutPID, Color.Blue);
|
||||
formsPlot1.Plot.AddScatter(x, sysOutFuzzyPID, Color.Green);
|
||||
|
||||
formsPlot1.Refresh();
|
||||
|
||||
}
|
||||
|
||||
|
||||
struct KK
|
||||
{
|
||||
public float Ki;
|
||||
public float Kp;
|
||||
public float Kd;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Ki {Ki} Kp {Kp} Kd {Kd}";
|
||||
}
|
||||
}
|
||||
|
||||
struct FuzzyChromosome
|
||||
{
|
||||
public sbyte KiInputErrPosition;
|
||||
public sbyte KiInputErrScale;
|
||||
|
||||
public sbyte KiInputErrAccPosition;
|
||||
public sbyte KiInputErrAccScale;
|
||||
|
||||
public sbyte KiOutputPosition;
|
||||
public sbyte KiOutputScale;
|
||||
}
|
||||
|
||||
private double CalculateFuzzyPIDStepError(FuzzyChromosome config, double errStart, double errEnd)
|
||||
{
|
||||
var errPos = config.KiInputErrPosition * 0.1;
|
||||
var errScale = config.KiInputErrPosition * 0.1;
|
||||
|
||||
var lowErr = new ContinuousSet(MembershipFunctions.Descending(config.KiInputErrPosition * 0.1, config.kiLowStart * 0.1 + Math.Abs(config.kiLowEnd * 0.1)));
|
||||
var midErr = new ContinuousSet(MembershipFunctions.Triangular(config.KiInputErrPosition * 0.1, config.kiMidStart * 0.1 + Math.Abs(config.kiMidMid * 0.1), config.kiMidStart * 0.1 + Math.Abs(config.kiMidMid * 0.1) + Math.Abs(config.kiMidEnd * 0.1)));
|
||||
var highErr = new ContinuousSet(MembershipFunctions.Ascending(config.KiInputErrPosition * 0.1, config.kiHiStart * 0.1 + Math.Abs(config.kiHiEnd * 0.1)));
|
||||
|
||||
var lowAccErr = new ContinuousSet(MembershipFunctions.Descending(config.KiInputErrAccPosition * 0.1, Math.Abs(config.kiLowAccStart * 0.1) + Math.Abs(config.kiLowAccEnd * 0.1)));
|
||||
var midAccErr = new ContinuousSet(MembershipFunctions.Triangular(config.KiInputErrAccPosition * 0.1, Math.Abs(config.kiMidAccStart * 0.1) + Math.Abs(config.kiMidAccMid * 0.1), config.kiMidAccStart * 0.1 + Math.Abs(config.kiMidAccMid * 0.1) + Math.Abs(config.kiMidAccEnd * 0.1)));
|
||||
var highAccErr = new ContinuousSet(MembershipFunctions.Ascending(config.KiInputErrAccPosition * 0.1, config.kiHiAccStart * 0.1 + Math.Abs(config.kiHiAccEnd * 0.1)));
|
||||
|
||||
var small = new ContinuousSet(MembershipFunctions.Descending(config.kiSmallStart * 0.1, config.kiSmallStart * 0.1 + Math.Abs(config.kiSmallEnd * 0.1)));
|
||||
var avg = new ContinuousSet(MembershipFunctions.Triangular(config.kiAvgStart * 0.1, config.kiAvgStart * 0.1 + Math.Abs(config.kiAvgMid * 0.1), config.kiAvgStart * 0.1 + Math.Abs(config.kiAvgMid * 0.1) + Math.Abs(config.kiAvgEnd * 0.1)));
|
||||
var big = new ContinuousSet(MembershipFunctions.Ascending(config.kiBigStart * 0.1, config.kiBigStart * 0.1 + Math.Abs(config.kiBigEnd * 0.1)));
|
||||
|
||||
double Ki = -1.9181372, Kp = 18.625, Kd = 0.38281253;
|
||||
|
||||
var step = Enumerable.Repeat(1, 1000).Select(x => (double)x).ToArray();
|
||||
step[0] = 0;
|
||||
|
||||
var motor = new TransferFunction(new double[] { 1, 2 }, new double[] { 1, 1, 2 }, 0.01);
|
||||
var fuzzyPID = new TransferFunction(new double[] { Kd, Kp, Ki }, new double[] { 1, 1 }, 0.01);
|
||||
|
||||
var sysOutFuzzyPID = new double[step.Length];
|
||||
|
||||
var pidOut = new double[step.Length];
|
||||
var pidOutFuzzy = new double[step.Length];
|
||||
|
||||
var errorOutFuzzy = new double[step.Length];
|
||||
var errorOutAccFuzzy = new double[step.Length];
|
||||
|
||||
for (var i = 0; i < step.Length; i++)
|
||||
{
|
||||
sysOutFuzzyPID[i] = motor.Evaluate(step[i] + (i == 0 ? 0 : pidOutFuzzy[i - 1]));
|
||||
|
||||
|
||||
errorOutFuzzy[i] = (step[i] - sysOutFuzzyPID[i]);
|
||||
errorOutAccFuzzy[i] = (errorOutFuzzy[i] - (i == 0 ? 0 : errorOutFuzzy[i - 1]));
|
||||
|
||||
pidOutFuzzy[i] = fuzzyPID.Evaluate(errorOutFuzzy[i]);
|
||||
|
||||
|
||||
var k = MamdaniDefuzzifier.Evaluate(new INumericalSet<double>[]
|
||||
{
|
||||
errorOutFuzzy[i].Is(lowErr).And(errorOutAccFuzzy[i].Is(lowAccErr)).Then(small),
|
||||
errorOutFuzzy[i].Is(lowErr).And(errorOutAccFuzzy[i].Is(midAccErr)).Then(small),
|
||||
errorOutFuzzy[i].Is(lowErr).And(errorOutAccFuzzy[i].Is(highAccErr)).Then(avg),
|
||||
errorOutFuzzy[i].Is(midErr).And(errorOutAccFuzzy[i].Is(lowAccErr)).Then(small),
|
||||
errorOutFuzzy[i].Is(midErr).And(errorOutAccFuzzy[i].Is(midAccErr)).Then(avg),
|
||||
errorOutFuzzy[i].Is(midErr).And(errorOutAccFuzzy[i].Is(highAccErr)).Then(big),
|
||||
errorOutFuzzy[i].Is(highAccErr).And(errorOutAccFuzzy[i].Is(lowAccErr)).Then(avg),
|
||||
errorOutFuzzy[i].Is(highAccErr).And(errorOutAccFuzzy[i].Is(midAccErr)).Then(big),
|
||||
errorOutFuzzy[i].Is(highAccErr).And(errorOutAccFuzzy[i].Is(highAccErr)).Then(big),
|
||||
}, MamdaniDefuzzifierMethod.CenterOfGravity, -100, 100, 0.5);
|
||||
|
||||
fuzzyPID.InputCoefficients[1] = k;
|
||||
//fuzzyPID.InputCoefficients[1] = k;
|
||||
//fuzzyPID.InputCoefficients[1] = k;
|
||||
}
|
||||
|
||||
return errorOutFuzzy.RMS();
|
||||
|
||||
}
|
||||
|
||||
private double CalculatePIDStepError(double Kd, double Kp, double Ki)
|
||||
{
|
||||
var step = Enumerable.Repeat(1, 1000).Select(x => (double)x).ToArray();
|
||||
step[0] = 0;
|
||||
|
||||
var motor = new TransferFunction(new double[] { 1, 2 }, new double[] { 1, 1, 2 }, 0.01);
|
||||
|
||||
var sysOutPID = new double[step.Length];
|
||||
|
||||
var pidOut = new double[step.Length];
|
||||
var errorOutPID = new double[step.Length];
|
||||
var pid = new TransferFunction(new double[] { Kd, Kp, Ki }, new double[] { 1, 1 }, 0.01);
|
||||
|
||||
for (var i = 0; i < step.Length; i++)
|
||||
{
|
||||
sysOutPID[i] = motor.Evaluate(step[i] + (i == 0 ? 0 : pidOut[i - 1]));
|
||||
|
||||
if (double.IsInfinity(sysOutPID[i]))
|
||||
Console.WriteLine();
|
||||
|
||||
errorOutPID[i] = (step[i] - sysOutPID[i]);
|
||||
|
||||
if (double.IsNaN(errorOutPID[i]))
|
||||
Console.WriteLine();
|
||||
|
||||
pidOut[i] = pid.Evaluate(errorOutPID[i]);
|
||||
|
||||
if (double.IsInfinity(pidOut[i]))
|
||||
Console.WriteLine();
|
||||
|
||||
}
|
||||
|
||||
return errorOutPID.RMS();
|
||||
}
|
||||
|
||||
private void button1_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
//var gen = new Genetic<KK>(100, k =>
|
||||
//{
|
||||
// if (float.IsNaN(k.Ki) || float.IsNaN(k.Kp) || float.IsNaN(k.Kd))
|
||||
// return (double.MaxValue);
|
||||
|
||||
|
||||
// var r = CalculatePIDStepError(k.Kd, k.Kp, k.Ki);
|
||||
// if (double.IsNaN(r))
|
||||
// Console.WriteLine();
|
||||
// return r;
|
||||
//});
|
||||
|
||||
var gen = new Genetic<KKFF>(100, k =>
|
||||
{
|
||||
if (float.IsNaN(k.kiAvgEnd)
|
||||
|| float.IsNaN(k.kiAvgMid)
|
||||
|| float.IsNaN(k.kiAvgStart)
|
||||
|| float.IsNaN(k.kiBigEnd)
|
||||
|| float.IsNaN(k.kiBigStart)
|
||||
|| float.IsNaN(k.kiHiAccEnd)
|
||||
|| float.IsNaN(k.kiHiAccStart)
|
||||
|| float.IsNaN(k.kiHiEnd)
|
||||
|| float.IsNaN(k.kiHiStart)
|
||||
|| float.IsNaN(k.kiLowAccEnd)
|
||||
|| float.IsNaN(k.kiLowAccStart)
|
||||
|| float.IsNaN(k.kiLowEnd)
|
||||
|| float.IsNaN(k.kiLowStart)
|
||||
|| float.IsNaN(k.kiMidAccEnd)
|
||||
|| float.IsNaN(k.kiMidAccMid)
|
||||
|| float.IsNaN(k.kiMidAccStart)
|
||||
|| float.IsNaN(k.kiMidEnd)
|
||||
|| float.IsNaN(k.kiMidMid)
|
||||
|| float.IsNaN(k.kiMidStart)
|
||||
|| float.IsNaN(k.kiSmallEnd)
|
||||
|| float.IsNaN(k.kiSmallStart))
|
||||
return (double.MaxValue);
|
||||
|
||||
|
||||
var r = CalculateFuzzyPIDStepError(k);
|
||||
if (double.IsNaN(r))
|
||||
Console.WriteLine();
|
||||
return r;
|
||||
});
|
||||
|
||||
|
||||
var ev = gen.Evaluate(1000);
|
||||
|
||||
Console.WriteLine(ev);
|
||||
}
|
||||
}
|
||||
}
|
60
Esiur.Analysis.Test/FMain.resx
Normal file
60
Esiur.Analysis.Test/FMain.resx
Normal file
@ -0,0 +1,60 @@
|
||||
<root>
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
17
Esiur.Analysis.Test/Program.cs
Normal file
17
Esiur.Analysis.Test/Program.cs
Normal file
@ -0,0 +1,17 @@
|
||||
namespace Esiur.Analysis.Test
|
||||
{
|
||||
internal static class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
// To customize application configuration such as set high DPI settings or default font,
|
||||
// see https://aka.ms/applicationconfiguration.
|
||||
ApplicationConfiguration.Initialize();
|
||||
Application.Run(new FMain());
|
||||
}
|
||||
}
|
||||
}
|
@ -2,9 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Analysis.Signals
|
||||
namespace Esiur.Analysis.DSP
|
||||
{
|
||||
public static class DSP
|
||||
public static class Functions
|
||||
{
|
||||
public static double[] ConvolveMany(params double[][] signals)
|
||||
{
|
||||
@ -27,9 +27,11 @@ namespace Esiur.Analysis.Signals
|
||||
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
for (var j = 0; j < signal.Length && i - j >= 0 && i - j < filter.Length; j++)
|
||||
|
||||
for (var j = 0; j < signal.Length; j++)
|
||||
{
|
||||
rt[i] = signal[j] * filter[i - j];
|
||||
if (i - j >= 0 && i - j < filter.Length)
|
||||
rt[i] += signal[j] * filter[i - j];
|
||||
}
|
||||
}
|
||||
|
73
Esiur.Analysis/DSP/TransferFunction.cs
Normal file
73
Esiur.Analysis/DSP/TransferFunction.cs
Normal file
@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.WebSockets;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Xml.Schema;
|
||||
|
||||
namespace Esiur.Analysis.DSP
|
||||
{
|
||||
public class TransferFunction
|
||||
{
|
||||
|
||||
public double Step { get; set; }
|
||||
|
||||
double[] inputs;
|
||||
double[] outputs;
|
||||
|
||||
public double[] InputCoefficients { get; set; }
|
||||
public double[] OutputCoefficients { get; set; }
|
||||
|
||||
public TransferFunction(double[] numerator, double[] denominator, double step = 0.01)
|
||||
{
|
||||
inputs = new double[numerator.Length];
|
||||
outputs = new double[denominator.Length];
|
||||
|
||||
InputCoefficients = numerator.Reverse().ToArray();
|
||||
OutputCoefficients = denominator.Reverse().ToArray();
|
||||
|
||||
Step = step;
|
||||
}
|
||||
|
||||
public double Evaluate(double value)
|
||||
{
|
||||
var xs = new double[inputs.Length];
|
||||
xs[0] = value;// * InputCoefficients.Last();
|
||||
|
||||
|
||||
// diffrentiate
|
||||
for(var i = 1; i < xs.Length; i++)
|
||||
xs[i] = (xs[i - 1] - inputs[i - 1]) / Step;
|
||||
|
||||
var ys = new double[outputs.Length];
|
||||
|
||||
// integrate
|
||||
for (var i = outputs.Length - 2; i >= 0; i--)
|
||||
{
|
||||
var iy = outputs[i] + (Step * outputs[i + 1]);
|
||||
if (double.IsNaN(iy) || double.IsInfinity(iy))
|
||||
ys[i] = outputs[i];
|
||||
else
|
||||
ys[i] = iy;
|
||||
}
|
||||
|
||||
var v = xs.Zip(InputCoefficients, (x, c) => x * c).Sum() - ys.Zip(OutputCoefficients, (y, c) => y * c).Sum();
|
||||
|
||||
if (double.IsNaN(v) || double.IsInfinity(v))
|
||||
ys[ys.Length - 1] = outputs[ys.Length - 1];
|
||||
else
|
||||
ys[ys.Length - 1] = v;
|
||||
|
||||
inputs = xs;
|
||||
outputs = ys;
|
||||
|
||||
return ys[0];
|
||||
}
|
||||
|
||||
public double[] Evaluate(double[] value)
|
||||
{
|
||||
return value.Select(x => Evaluate(x)).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@ -12,11 +13,14 @@
|
||||
<ItemGroup>
|
||||
<Folder Include="Processing\" />
|
||||
<Folder Include="Signals\Modulation\" />
|
||||
<Folder Include="Optimization\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="Fuzzy\FuzzyRule.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Esiur\Esiur.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
135
Esiur.Analysis/Optimization/Genetic.cs
Normal file
135
Esiur.Analysis/Optimization/Genetic.cs
Normal file
@ -0,0 +1,135 @@
|
||||
using Esiur.Data;
|
||||
using Esiur.Resource;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Analysis.Optimization
|
||||
{
|
||||
public class Genetic<T> where T : unmanaged
|
||||
{
|
||||
|
||||
Random rand = new Random();
|
||||
|
||||
public static unsafe byte[] Encode(in T value)
|
||||
{
|
||||
byte[] result = new byte[sizeof(T)];
|
||||
fixed (byte* dst = result)
|
||||
*(T*)dst = value;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static unsafe T Decode(byte[] data)
|
||||
{
|
||||
fixed (byte* src = data)
|
||||
return *(T*)src;
|
||||
}
|
||||
|
||||
public List<T> Population = new List<T>();
|
||||
|
||||
public int PopulationSize { get; set; }
|
||||
private int DataSize { get; set; }
|
||||
|
||||
public Func<T, double> FitnessFunction { get; set; }
|
||||
|
||||
public unsafe Genetic(int populationSize, Func<T, double> fitnessFunction)
|
||||
{
|
||||
FitnessFunction = fitnessFunction;
|
||||
PopulationSize = populationSize;
|
||||
DataSize = sizeof(T);
|
||||
}
|
||||
|
||||
void GeneratePopultation()
|
||||
{
|
||||
for (var i = 0; i < PopulationSize; i++)
|
||||
{
|
||||
byte[] buffer = new byte[DataSize];
|
||||
rand.NextBytes(buffer);
|
||||
var record = Decode(buffer);
|
||||
Population.Add(record);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
KeyValuePair<T, double>[] GetFitness()
|
||||
{
|
||||
var rt = new List<KeyValuePair<T, double>>();
|
||||
|
||||
foreach (var record in Population)
|
||||
rt.Add(new KeyValuePair<T, double>( record, FitnessFunction(record)));
|
||||
|
||||
return rt.ToArray();
|
||||
}
|
||||
|
||||
T Mate(T parent1, T parent2)
|
||||
{
|
||||
var dp1 = Encode(parent1);
|
||||
var dp2 = Encode(parent2);
|
||||
|
||||
var dc = new byte[dp1.Length];
|
||||
|
||||
|
||||
for (var i = 0; i < dc.Length; i++)
|
||||
{
|
||||
var prop = rand.NextDouble();
|
||||
if (prop < 0.45)
|
||||
dc[i] = dp1[i];
|
||||
else if (prop < 0.9)
|
||||
dc[i] = dp2[i];
|
||||
else
|
||||
dc[i] = (byte)rand.Next(0, 255);
|
||||
}
|
||||
|
||||
return Decode(dc);
|
||||
|
||||
}
|
||||
|
||||
public T Evaluate(int maxIterations)
|
||||
{
|
||||
GeneratePopultation();
|
||||
|
||||
var generation = 0;
|
||||
|
||||
T best;
|
||||
|
||||
do
|
||||
{
|
||||
var ordered = GetFitness().OrderBy(x => x.Value).ToArray();
|
||||
|
||||
best = ordered[0].Key;
|
||||
|
||||
if (ordered[0].Value == 0)
|
||||
break;
|
||||
|
||||
// Elitism selection ( 10% of fittest population )
|
||||
|
||||
var eliteCount = (int)(ordered.Length * 0.1);
|
||||
var neededCount = (int)(ordered.Length * 0.9);
|
||||
|
||||
var newGeneration = ordered.Select(x => x.Key).Take(eliteCount).ToList();
|
||||
|
||||
for (var i = 0; i < neededCount; i++)
|
||||
{
|
||||
var p1 = Population[rand.Next(0, PopulationSize / 2)];
|
||||
var p2 = Population[rand.Next(0, PopulationSize / 2)];
|
||||
|
||||
var offspring = Mate(p1, p2);
|
||||
newGeneration.Add(offspring);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -33,7 +33,13 @@ namespace Esiur.Analysis.Statistics
|
||||
}
|
||||
|
||||
|
||||
public static double RMS(this double[] x) => Math.Sqrt(x.Sum(x => x * x) / x.Length);
|
||||
public static double RMS(this double[] x)
|
||||
{
|
||||
var r = Math.Sqrt(x.Sum(x => x * x) / x.Length);
|
||||
if (double.IsNaN(r))
|
||||
Console.WriteLine();
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
public static double Correlation(this double[] x, double[] y)
|
||||
|
@ -10,7 +10,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Esiur.Stores.EntityCore", "
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test", "Test\Test.csproj", "{331F82B6-6B90-4533-9718-F7C8090D8F19}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Esiur.Analysis", "Esiur.Analysis\Esiur.Analysis.csproj", "{8D52DEEA-B7E8-4C93-9B6C-EDB9C2D25211}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Esiur.Analysis", "Esiur.Analysis\Esiur.Analysis.csproj", "{8D52DEEA-B7E8-4C93-9B6C-EDB9C2D25211}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Esiur.Analysis.Test", "Esiur.Analysis.Test\Esiur.Analysis.Test.csproj", "{72115ABD-BD54-4E88-8DB6-B2A953F57F0A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -38,6 +40,10 @@ Global
|
||||
{8D52DEEA-B7E8-4C93-9B6C-EDB9C2D25211}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8D52DEEA-B7E8-4C93-9B6C-EDB9C2D25211}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8D52DEEA-B7E8-4C93-9B6C-EDB9C2D25211}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{72115ABD-BD54-4E88-8DB6-B2A953F57F0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{72115ABD-BD54-4E88-8DB6-B2A953F57F0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{72115ABD-BD54-4E88-8DB6-B2A953F57F0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{72115ABD-BD54-4E88-8DB6-B2A953F57F0A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -56,53 +56,6 @@ namespace Test
|
||||
{
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
//var outage = Capacity.ComputeOutage(20000000, new Capacity.CSI[]
|
||||
//{
|
||||
// new Capacity.CSI(PowerUnit.FromDb(20), 0.1),
|
||||
// new Capacity.CSI(PowerUnit.FromDb(15), 0.15),
|
||||
// new Capacity.CSI(PowerUnit.FromDb(10), 0.25),
|
||||
// new Capacity.CSI(PowerUnit.FromDb(5), 0.25),
|
||||
// new Capacity.CSI(PowerUnit.FromDb(0), 0.15),
|
||||
// new Capacity.CSI(PowerUnit.FromDb(-5), 0.1),
|
||||
//});
|
||||
var outage = Capacity.ComputeOutage(1, new Capacity.CSI[]
|
||||
{
|
||||
new Capacity.CSI(PowerUnit.FromDb(30), 0.2),
|
||||
new Capacity.CSI(PowerUnit.FromDb(20), 0.3),
|
||||
new Capacity.CSI(PowerUnit.FromDb(10), 0.3),
|
||||
new Capacity.CSI(PowerUnit.FromDb(0), 0.2),
|
||||
});
|
||||
|
||||
|
||||
var low = new ContinuousSet(MembershipFunctions.Descending(20, 40));
|
||||
var mid = new ContinuousSet(MembershipFunctions.Triangular(20, 40, 60));
|
||||
var high = new ContinuousSet(MembershipFunctions.Ascending(40, 60));
|
||||
|
||||
var bad = new ContinuousSet(MembershipFunctions.Descending(0, 30));
|
||||
var ok = new ContinuousSet(MembershipFunctions.Triangular(20, 50, 80));
|
||||
var excelent = new ContinuousSet(MembershipFunctions.Ascending(70, 100));
|
||||
|
||||
var small = new ContinuousSet(MembershipFunctions.Descending(100, 200));
|
||||
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 rating = 80;
|
||||
|
||||
for (double temp = 60; temp < 100; temp++)
|
||||
{
|
||||
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(temp + " " + v);
|
||||
}
|
||||
|
||||
// Create stores to keep objects.
|
||||
var system = await Warehouse.Put("sys", new MemoryStore());
|
||||
var server = await Warehouse.Put("sys/server", new DistributedServer());
|
||||
|
Loading…
x
Reference in New Issue
Block a user