update mit Graphischem Zeug
This commit is contained in:
parent
0aa4bc0529
commit
be5a61ac0a
|
@ -1,44 +1,44 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Data;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
using System.Windows.Forms.DataVisualization.Charting;
|
||||||
|
|
||||||
|
namespace NMLS_Graphisch
|
||||||
|
{
|
||||||
|
public partial class Form1 : Form
|
||||||
|
{
|
||||||
|
public Form1()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
// Variable mit der anzahl Punkten zwischen 0 - 255
|
|
||||||
static int NumberOfSamples = 1000;
|
static int NumberOfSamples = 1000;
|
||||||
|
|
||||||
// Variable zum tracken der Gewichte
|
|
||||||
const int tracking = 40;
|
const int tracking = 40;
|
||||||
|
static Stack<double> x = new Stack<double>();
|
||||||
// C# only zum erstellen von Randomzahlen
|
|
||||||
static Random rnd = new Random();
|
static Random rnd = new Random();
|
||||||
|
|
||||||
// Array mit den Testwerten
|
|
||||||
static double[] _x = new double[NumberOfSamples];
|
static double[] _x = new double[NumberOfSamples];
|
||||||
|
|
||||||
// Array mit den Gewichten
|
|
||||||
static double[,] w = new double[NumberOfSamples, NumberOfSamples];
|
static double[,] w = new double[NumberOfSamples, NumberOfSamples];
|
||||||
|
static double learnrate = 0.2;
|
||||||
// Lernrate
|
static double[] pixel_array;
|
||||||
static double learnrate = 1;
|
|
||||||
|
|
||||||
// Größe des Fensters
|
|
||||||
static int windowSize = 5;
|
static int windowSize = 5;
|
||||||
|
|
||||||
/**************************************************************
|
private void button1_Click(object sender, EventArgs e)
|
||||||
main() des Programms
|
|
||||||
***************************************************************/
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
|
|
||||||
// Initialisierung des Test Array + Gewichte
|
|
||||||
for (int i = 0; i < NumberOfSamples; i++)
|
|
||||||
{
|
{
|
||||||
_x[i] += ((255.0 / NumberOfSamples) * i);
|
|
||||||
for (int k = 0; k < windowSize; k++)
|
NumberOfSamples = Int32.Parse(comboBox2.SelectedItem.ToString());
|
||||||
|
chart1.ChartAreas[0].AxisX.Maximum = NumberOfSamples;
|
||||||
|
chart1.ChartAreas[0].AxisY.Maximum = 300;
|
||||||
|
chart1.ChartAreas[0].AxisY.Minimum = -5;
|
||||||
|
|
||||||
|
if (checkBox1.Checked)
|
||||||
{
|
{
|
||||||
w[k, i] = rnd.NextDouble();
|
|
||||||
//Console.WriteLine(String.Format("Weight: {0}", w[k, i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Zum erstellen eines Files mit den Gewichten vor den Updates
|
|
||||||
for (int i = 0; i < tracking; i++)
|
for (int i = 0; i < tracking; i++)
|
||||||
{
|
{
|
||||||
for (int k = 0; k < windowSize; k++)
|
for (int k = 0; k < windowSize; k++)
|
||||||
|
@ -48,11 +48,44 @@ int main(){
|
||||||
Encoding.UTF8);
|
Encoding.UTF8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Variante die Ausgeführt werden soll
|
Series[] series = new Series[2];
|
||||||
lokalerMittelWert();
|
switch (comboBox1.SelectedItem.ToString())
|
||||||
|
{
|
||||||
|
|
||||||
// Zum erstellen eines Files mit den Gewichten nach den Updates
|
case "lokaler Mittelwert":
|
||||||
|
series = lokalerMittelWert();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "direkter Vorgänger":
|
||||||
|
series = direkterVorgaenger();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "differenzieller Vorgänger":
|
||||||
|
series = diffVorgaenger();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (Series s in series)
|
||||||
|
{
|
||||||
|
if (chart1.Series.IndexOf(s.Name) < 0)
|
||||||
|
{
|
||||||
|
chart1.Series.Add(s);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
chart1.Series.RemoveAt(chart1.Series.IndexOf(s.Name));
|
||||||
|
chart1.Series.Add(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkBox1.Checked)
|
||||||
|
{
|
||||||
for (int i = 0; i < tracking; i++)
|
for (int i = 0; i < tracking; i++)
|
||||||
{
|
{
|
||||||
for (int k = 0; k < windowSize; k++)
|
for (int k = 0; k < windowSize; k++)
|
||||||
|
@ -63,77 +96,82 @@ int main(){
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**************************************************************
|
Series[] lokalerMittelWert()
|
||||||
Errechnet die 1. Variante, mit abziehen des lokalen Mittelwertes
|
|
||||||
***************************************************************/
|
|
||||||
|
|
||||||
void lokalerMittelWert()
|
|
||||||
{
|
{
|
||||||
//Array in dem e(n) gespeichert wird
|
|
||||||
double[] x_error = new double[NumberOfSamples];
|
|
||||||
|
|
||||||
//Laufzeitvariable
|
|
||||||
int x_count = 0;
|
int x_count = 0;
|
||||||
|
double[] x_error = new double[NumberOfSamples];
|
||||||
|
x_error[0] = 0;
|
||||||
|
|
||||||
|
//Graphischer Stuff
|
||||||
|
Series lokal_M_error = new Series("Lokaler Mittelwert Error");
|
||||||
|
Series lokal_M_predic = new Series("Lokaler Mittelwert Prediction");
|
||||||
|
lokal_M_error.ChartType = SeriesChartType.Spline;
|
||||||
|
lokal_M_predic.ChartType = SeriesChartType.Spline;
|
||||||
|
|
||||||
// x_count + 1 da x_count > 0 sein muss
|
|
||||||
while (x_count + 1 < NumberOfSamples)
|
while (x_count + 1 < NumberOfSamples)
|
||||||
{
|
{
|
||||||
//Erstellt ein neues Array der letzten "windowSize" Werte
|
|
||||||
double[] x_part_Array = new double[x_count];
|
double[] x_part_Array = new double[x_count];
|
||||||
|
|
||||||
//Anfang des Fensters das erstellt wird
|
|
||||||
int _sourceIndex = (x_count > windowSize) ? x_count - windowSize : 0;
|
int _sourceIndex = (x_count > windowSize) ? x_count - windowSize : 0;
|
||||||
|
int _arrayLength = (x_count > windowSize) ? windowSize + 1 : (x_count > 0) ? x_count : 0;
|
||||||
//Länge des Fensters das erstellt wird
|
|
||||||
int _arrayLength = (x_count > windowSize) ? windowSize : (x_count > 0) ? x_count - 1 : 0;
|
|
||||||
|
|
||||||
Array.Copy(_x, _sourceIndex, x_part_Array, 0, _arrayLength);
|
Array.Copy(_x, _sourceIndex, x_part_Array, 0, _arrayLength);
|
||||||
double x_middle = (x_count > 0) ? ( x_part_Array.Sum() / _arrayLength) : 0;
|
double x_middle = (x_count > 0) ? ( x_part_Array.Sum() / _arrayLength) : 0;
|
||||||
|
|
||||||
// Variable für die errechnete Zahl
|
|
||||||
double x_pred = 0.0;
|
double x_pred = 0.0;
|
||||||
|
double[] x_array = _x;
|
||||||
// Variable mit der eigentlichen Zahl
|
|
||||||
double x_actual = _x[x_count + 1];
|
double x_actual = _x[x_count + 1];
|
||||||
|
|
||||||
for (int i = _sourceIndex; i < _arrayLength; i++)
|
for (int i = 1; i < _arrayLength; i++)
|
||||||
{
|
{
|
||||||
x_pred += (w[i, x_count] * (_x[x_count - i] - x_middle));
|
x_pred += (w[i, x_count] * (x_array[x_count - i] - x_middle));
|
||||||
}
|
}
|
||||||
x_pred += x_middle;
|
x_pred += x_middle;
|
||||||
|
|
||||||
//Console.WriteLine(String.Format("X_sum: {0}", x_middle));
|
// Output Stuff
|
||||||
|
if (checkBox1.Checked)
|
||||||
|
{
|
||||||
|
File.AppendAllText("lokalerMittelwert.txt",
|
||||||
|
String.Format("{0}. X_pred {1}\n", x_count, x_pred),
|
||||||
|
Encoding.UTF8);
|
||||||
|
|
||||||
//Console.WriteLine(String.Format("X_pred: {0}", x_pred));
|
File.AppendAllText("lokalerMittelwert.txt",
|
||||||
//Console.WriteLine(String.Format("X_actual: {0}", x_actual));
|
String.Format("{0}. X_actual {1}\n", x_count, x_actual),
|
||||||
|
Encoding.UTF8);
|
||||||
|
}
|
||||||
|
|
||||||
x_error[x_count] = x_actual - x_pred;
|
x_error[x_count] = x_actual - x_pred;
|
||||||
|
|
||||||
// Funktion zum berechnen des Quadrates
|
|
||||||
double x_square = 0;
|
double x_square = 0;
|
||||||
for (int i = _sourceIndex; i < _arrayLength; i++)
|
|
||||||
|
//Output Stuff
|
||||||
|
if (checkBox1.Checked)
|
||||||
{
|
{
|
||||||
x_square += Math.Pow(_x[x_count - i], 2);
|
File.AppendAllText("lokalerMittelwert.txt",
|
||||||
|
String.Format("{0}. X_error {1}\n\n", x_count, x_error[x_count]),
|
||||||
|
Encoding.UTF8);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Funktion zum updaten der Gewichte
|
for (int i = 1; i < _arrayLength; i++)
|
||||||
for (int i = _sourceIndex; i < _arrayLength; i++)
|
|
||||||
{
|
{
|
||||||
w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * (_x[x_count - i] / x_square);
|
x_square += Math.Pow(x_array[x_count - i] - x_middle, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Laufzeitvariable hochzählen
|
for (int i = 1; i < _arrayLength; i++)
|
||||||
|
{
|
||||||
|
w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * ((x_array[x_count - i] - x_middle) / x_square);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Graphischer Stuff
|
||||||
|
lokal_M_error.Points.AddXY(x_count, x_error[x_count]);
|
||||||
|
lokal_M_predic.Points.AddXY(x_count, x_pred);
|
||||||
|
|
||||||
x_count += 1;
|
x_count += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Berechenen des mittleren Fehlers und aussortieren potenzieller NaN Werte im Fehler Array
|
|
||||||
double mittel = x_error.Where(d => !double.IsNaN(d)).Sum() / x_error.Length;
|
double mittel = x_error.Where(d => !double.IsNaN(d)).Sum() / x_error.Length;
|
||||||
|
|
||||||
// Berechenen der varianz des Fehlers und aussortieren potenzieller NaN Werte im Fehler Array
|
|
||||||
double varianz = 0.0;
|
double varianz = 0.0;
|
||||||
foreach (double x_e in x_error)
|
foreach (double x_e in x_error)
|
||||||
{
|
{
|
||||||
|
@ -141,100 +179,85 @@ void lokalerMittelWert()
|
||||||
varianz += Math.Pow(x_e - mittel, 2);
|
varianz += Math.Pow(x_e - mittel, 2);
|
||||||
}
|
}
|
||||||
varianz /= x_error.Length;
|
varianz /= x_error.Length;
|
||||||
|
if (checkBox1.Checked)
|
||||||
// Hängt dem Angegebenen File den Vorgegebenen String an
|
{
|
||||||
File.AppendAllText("ergebnisse.txt",
|
File.AppendAllText("ergebnisse.txt",
|
||||||
String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel),
|
String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel),
|
||||||
Encoding.UTF8);
|
Encoding.UTF8);
|
||||||
}
|
}
|
||||||
|
return new Series[] { lokal_M_predic, lokal_M_error };
|
||||||
|
}
|
||||||
|
|
||||||
/**************************************************************
|
Series[] direkterVorgaenger()
|
||||||
Errechnet die 2. Variante, mit abziehen des direkten Vorgängers
|
|
||||||
***************************************************************/
|
|
||||||
|
|
||||||
void direkterVorgaenger()
|
|
||||||
{
|
{
|
||||||
//Array in dem e(n) gespeichert wird
|
|
||||||
double[] x_error = new double[NumberOfSamples];
|
double[] x_error = new double[NumberOfSamples];
|
||||||
|
x_error[0] = 0;
|
||||||
//Laufzeitvariable
|
|
||||||
int x_count = 0;
|
int x_count = 0;
|
||||||
|
|
||||||
// x_count + 1 da x_count > 0 sein muss
|
// Graphischer Stuff
|
||||||
|
Series direkterVorgaenger_error = new Series("Direkter Vorgänger Error");
|
||||||
|
Series direkterVorgaenger_predic = new Series("Direkter Vorgänger Prediction");
|
||||||
|
direkterVorgaenger_error.ChartType = SeriesChartType.Spline;
|
||||||
|
direkterVorgaenger_predic.ChartType = SeriesChartType.Spline;
|
||||||
|
|
||||||
while (x_count + 1 < NumberOfSamples)
|
while (x_count + 1 < NumberOfSamples)
|
||||||
{
|
{
|
||||||
// Variable für die errechnete Zahl
|
|
||||||
double x_pred = 0.0;
|
double x_pred = 0.0;
|
||||||
|
double[] x_array = _x;
|
||||||
//Anfang des Fensters das erstellt wird
|
|
||||||
int _sourceIndex = (x_count > windowSize) ? x_count - windowSize : 0;
|
|
||||||
|
|
||||||
//Länge des Fensters das erstellt wird
|
|
||||||
int _arrayLength = (x_count > windowSize) ? windowSize : (x_count > 0) ? x_count - 1 : 0;
|
|
||||||
|
|
||||||
// Variable mit der eigentlichen Zahl
|
|
||||||
double x_actual = _x[x_count + 1];
|
double x_actual = _x[x_count + 1];
|
||||||
|
|
||||||
// Funktion fürs berechnen der Vorhersagezahl
|
if (x_count > 0)
|
||||||
for (int i = _sourceIndex; i < _arrayLength; i++)
|
|
||||||
{
|
{
|
||||||
x_pred += (w[i, x_count] * (_x[x_count - 1] - _x[x_count - i - 1]));
|
int _arrayLength = (x_count > windowSize) ? windowSize + 1 : x_count;
|
||||||
|
|
||||||
|
for (int i = 1; i < _arrayLength; i++)
|
||||||
|
{
|
||||||
|
x_pred += (w[i, x_count] * (x_array[x_count - 1] - x_array[x_count - i - 1]));
|
||||||
}
|
}
|
||||||
x_pred += _x[x_count - 1];
|
x_pred += x_array[x_count - 1];
|
||||||
|
|
||||||
//Console.WriteLine(String.Format("X_pred: {0}", x_pred));
|
// Output Stuff
|
||||||
|
if (checkBox1.Checked)
|
||||||
// Hängt dem Angegebenen File den Vorgegebenen String an
|
{
|
||||||
File.AppendAllText("direkterVorgaenger.txt",
|
File.AppendAllText("direkterVorgaenger.txt",
|
||||||
String.Format("{0}. X_pred {1}\n", x_count, x_pred),
|
String.Format("{0}. X_pred {1}\n", x_count, x_pred),
|
||||||
Encoding.UTF8);
|
Encoding.UTF8);
|
||||||
|
|
||||||
//Console.WriteLine(String.Format("X_actual: {0}", x_actual));
|
|
||||||
|
|
||||||
// Hängt dem Angegebenen File den Vorgegebenen String an
|
|
||||||
File.AppendAllText("direkterVorgaenger.txt",
|
File.AppendAllText("direkterVorgaenger.txt",
|
||||||
String.Format("{0}. X_actual {1}\n", x_count, x_actual),
|
String.Format("{0}. X_actual {1}\n", x_count, x_actual),
|
||||||
Encoding.UTF8);
|
Encoding.UTF8);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Berechnung des Fehlers
|
|
||||||
x_error[x_count] = x_actual - x_pred;
|
x_error[x_count] = x_actual - x_pred;
|
||||||
|
|
||||||
|
// Output Stuff
|
||||||
//Console.WriteLine(String.Format("X_error: {0}", x_error));
|
if (checkBox1.Checked)
|
||||||
|
{
|
||||||
// Hängt dem Angegebenen File den Vorgegebenen String an
|
|
||||||
File.AppendAllText("direkterVorgaenger.txt",
|
File.AppendAllText("direkterVorgaenger.txt",
|
||||||
String.Format("{0}. X_error {1}\n\n", x_count, x_error),
|
String.Format("{0}. X_error {1}\n\n", x_count, x_error[x_count]),
|
||||||
Encoding.UTF8);
|
Encoding.UTF8);
|
||||||
|
}
|
||||||
|
|
||||||
// Funktion zum berechnen des Quadrates
|
|
||||||
double x_square = 0;
|
double x_square = 0;
|
||||||
for (int i = _sourceIndex; i < _arrayLength; i++)
|
for (int i = 1; i < _arrayLength; i++)
|
||||||
{
|
{
|
||||||
x_square += Math.Pow(_x[x_count - 1] - _x[x_count - i - 1], 2);
|
x_square += Math.Pow(x_array[x_count - 1] - x_array[x_count - i - 1], 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Console.WriteLine(String.Format("X_square: {0}", x_square));
|
for (int i = 1; i < _arrayLength; i++)
|
||||||
|
|
||||||
// Hängt dem Angegebenen File den Vorgegebenen String an
|
|
||||||
//File.AppendAllText("direkterVorgaenger.txt",
|
|
||||||
// String.Format("{0}. X_square {1}\n", x_count, x_square),
|
|
||||||
// Encoding.UTF8);
|
|
||||||
|
|
||||||
// Funktion zum updaten der Gewichte
|
|
||||||
for (int i = _sourceIndex; i < _arrayLength; i++)
|
|
||||||
{
|
{
|
||||||
w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * ((_x[x_count - 1] - _x[x_count - i - 1]) / x_square);
|
w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * ((x_array[x_count - 1] - x_array[x_count - i - 1]) / x_square);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Laufzeitvariable hochzählen
|
//Graphischer Stuff
|
||||||
|
direkterVorgaenger_error.Points.AddXY(x_count, x_error[x_count]);
|
||||||
|
direkterVorgaenger_predic.Points.AddXY(x_count, x_pred);
|
||||||
|
|
||||||
x_count += 1;
|
x_count += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Berechenen des mittleren Fehlers und aussortieren potenzieller NaN Werte im Fehler Array
|
|
||||||
double mittel = x_error.Where(d => !double.IsNaN(d)).Sum() / x_error.Length;
|
double mittel = x_error.Where(d => !double.IsNaN(d)).Sum() / x_error.Length;
|
||||||
|
|
||||||
// Berechenen der varianz des Fehlers und aussortieren potenzieller NaN Werte im Fehler Array
|
|
||||||
double varianz = 0.0;
|
double varianz = 0.0;
|
||||||
foreach (double x_e in x_error)
|
foreach (double x_e in x_error)
|
||||||
{
|
{
|
||||||
|
@ -242,100 +265,87 @@ void direkterVorgaenger()
|
||||||
varianz += Math.Pow(x_e - mittel, 2);
|
varianz += Math.Pow(x_e - mittel, 2);
|
||||||
}
|
}
|
||||||
varianz /= x_error.Length;
|
varianz /= x_error.Length;
|
||||||
|
if (checkBox1.Checked)
|
||||||
// Hängt dem Angegebenen File den Vorgegebenen String an
|
{
|
||||||
File.AppendAllText("ergebnisse.txt",
|
File.AppendAllText("ergebnisse.txt",
|
||||||
String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel),
|
String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel),
|
||||||
Encoding.UTF8);
|
Encoding.UTF8);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**************************************************************
|
return new Series[] { direkterVorgaenger_error, direkterVorgaenger_predic };
|
||||||
Errechnet die 3. Variante, mit abziehen des differenziellen Vorgängers
|
}
|
||||||
***************************************************************/
|
|
||||||
|
|
||||||
void differenziellerVorgaenger()
|
Series[] diffVorgaenger()
|
||||||
{
|
{
|
||||||
//Array in dem e(n) gespeichert wird
|
|
||||||
double[] x_error = new double[NumberOfSamples];
|
double[] x_error = new double[NumberOfSamples];
|
||||||
|
x_error[0] = 0;
|
||||||
|
int x_count = 1;
|
||||||
|
|
||||||
//Laufzeitvariable
|
//Graphischer Stuff
|
||||||
int x_count = 0;
|
Series diffVorgaenger_error = new Series("Differenzieller Vorgänger Error");
|
||||||
|
Series diffVorgaenger_predic = new Series("Differenzieller Vorgänger Prediction");
|
||||||
|
diffVorgaenger_error.ChartType = SeriesChartType.Spline;
|
||||||
|
diffVorgaenger_predic.ChartType = SeriesChartType.Spline;
|
||||||
|
|
||||||
// x_count + 1 da x_count > 0 sein muss
|
|
||||||
while (x_count + 1 < NumberOfSamples)
|
while (x_count + 1 < NumberOfSamples)
|
||||||
{
|
{
|
||||||
// Variable für die errechnete Zahl
|
|
||||||
double x_pred = 0.0;
|
double x_pred = 0.0;
|
||||||
|
double[] x_array = _x;
|
||||||
// Anfang des Fensters das erstellt wird
|
|
||||||
int _sourceIndex = (x_count > windowSize) ? x_count - windowSize : 0;
|
|
||||||
|
|
||||||
//Länge des Fensters das erstellt wird
|
|
||||||
int _arrayLength = (x_count > windowSize) ? windowSize : (x_count > 0) ? x_count - 1 : 0;
|
|
||||||
|
|
||||||
// Variable mit der eigentlichen Zahl
|
|
||||||
double x_actual = _x[x_count + 1];
|
double x_actual = _x[x_count + 1];
|
||||||
|
if (x_count > 0)
|
||||||
// Funktion fürs berechnen der Vorhersagezahl
|
|
||||||
for (int i = _sourceIndex; i < _arrayLength; i++)
|
|
||||||
{
|
{
|
||||||
x_pred += (w[i, x_count] * (_x[x_count - i] - _x[x_count - i - 1]));
|
int _arrayLength = (x_count > windowSize) ? windowSize + 1 : x_count;
|
||||||
|
|
||||||
|
for (int i = 1; i < _arrayLength; i++)
|
||||||
|
{
|
||||||
|
x_pred += (w[i, x_count] * (x_array[x_count - i] - x_array[x_count - i - 1]));
|
||||||
}
|
}
|
||||||
x_pred += _x[x_count - 1];
|
x_pred += x_array[x_count - 1];
|
||||||
|
|
||||||
//Console.WriteLine(String.Format("X_pred: {0}", x_pred));
|
// Output Stuff
|
||||||
|
if (checkBox1.Checked)
|
||||||
// Hängt dem Angegebenen File den Vorgegebenen String an
|
{
|
||||||
File.AppendAllText("differenziellerVorgaenger.txt",
|
File.AppendAllText("differenziellerVorgaenger.txt",
|
||||||
String.Format("{0}. X_pred {1}\n", x_count, x_pred),
|
String.Format("{0}. X_pred {1}\n", x_count, x_pred),
|
||||||
Encoding.UTF8);
|
Encoding.UTF8);
|
||||||
|
|
||||||
//Console.WriteLine(String.Format("X_actual: {0}", x_actual));
|
|
||||||
|
|
||||||
// Hängt dem Angegebenen File den Vorgegebenen String an
|
|
||||||
File.AppendAllText("differenziellerVorgaenger.txt",
|
File.AppendAllText("differenziellerVorgaenger.txt",
|
||||||
String.Format("{0}. X_actual {1}\n", x_count, x_actual),
|
String.Format("{0}. X_actual {1}\n", x_count, x_actual),
|
||||||
Encoding.UTF8);
|
Encoding.UTF8);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Berechnung des Fehlers
|
|
||||||
x_error[x_count] = x_actual - x_pred;
|
x_error[x_count] = x_actual - x_pred;
|
||||||
|
|
||||||
|
// Output Stuff
|
||||||
//Console.WriteLine(String.Format("X_error: {0}", x_error));
|
if (checkBox1.Checked)
|
||||||
|
{
|
||||||
// Hängt dem Angegebenen File den Vorgegebenen String an
|
|
||||||
File.AppendAllText("differenziellerVorgaenger.txt",
|
File.AppendAllText("differenziellerVorgaenger.txt",
|
||||||
String.Format("{0}. X_error {1}\n\n", x_count, x_error),
|
String.Format("{0}. X_error {1}\n\n", x_count, x_error[x_count]),
|
||||||
Encoding.UTF8);
|
Encoding.UTF8);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Funktion zum berechnen des Quadrates
|
|
||||||
double x_square = 0;
|
double x_square = 0;
|
||||||
for (int i = _sourceIndex; i < _arrayLength; i++)
|
for (int i = 1; i < _arrayLength; i++)
|
||||||
{
|
{
|
||||||
x_square += Math.Pow(_x[x_count - i] - _x[x_count - i - 1], 2);
|
x_square += Math.Pow(x_array[x_count - i] - x_array[x_count - i - 1], 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Console.WriteLine(String.Format("X_square: {0}", x_square));
|
for (int i = 1; i < _arrayLength; i++)
|
||||||
|
|
||||||
// Hängt dem Angegebenen File den Vorgegebenen String an
|
|
||||||
//File.AppendAllText("direkterVorgaenger.txt",
|
|
||||||
// String.Format("{0}. X_square {1}\n", x_count, x_square),
|
|
||||||
// Encoding.UTF8);
|
|
||||||
|
|
||||||
// Funktion zum updaten der Gewichte
|
|
||||||
for (int i = _sourceIndex; i < _arrayLength; i++)
|
|
||||||
{
|
{
|
||||||
w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * ((_x[x_count - i] - _x[x_count - i - 1]) / x_square);
|
w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * ((x_array[x_count - i] - x_array[x_count - i - 1]) / x_square);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Laufzeitvariable hochzählen
|
}
|
||||||
|
|
||||||
|
//Graphischer Stuff
|
||||||
|
diffVorgaenger_error.Points.AddXY(x_count, x_error[x_count]);
|
||||||
|
diffVorgaenger_predic.Points.AddXY(x_count, x_pred);
|
||||||
|
|
||||||
x_count += 1;
|
x_count += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Berechenen des mittleren Fehlers und aussortieren potenzieller NaN Werte im Fehler Array
|
|
||||||
double mittel = x_error.Where(d => !double.IsNaN(d)).Sum() / x_error.Length;
|
double mittel = x_error.Where(d => !double.IsNaN(d)).Sum() / x_error.Length;
|
||||||
|
|
||||||
// Berechenen der varianz des Fehlers und aussortieren potenzieller NaN Werte im Fehler Array
|
|
||||||
double varianz = 0.0;
|
double varianz = 0.0;
|
||||||
foreach (double x_e in x_error)
|
foreach (double x_e in x_error)
|
||||||
{
|
{
|
||||||
|
@ -344,8 +354,93 @@ void differenziellerVorgaenger()
|
||||||
}
|
}
|
||||||
varianz /= x_error.Length;
|
varianz /= x_error.Length;
|
||||||
|
|
||||||
// Hängt dem Angegebenen File den Vorgegebenen String an
|
if (checkBox1.Checked)
|
||||||
|
{
|
||||||
File.AppendAllText("ergebnisse.txt",
|
File.AppendAllText("ergebnisse.txt",
|
||||||
String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel),
|
String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel),
|
||||||
Encoding.UTF8);
|
Encoding.UTF8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return new Series[] { diffVorgaenger_error, diffVorgaenger_predic };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inizialisierung von Arrays
|
||||||
|
private void Form1_Load(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
comboBox1.SelectedIndex = 0;
|
||||||
|
comboBox2.SelectedIndex = 0;
|
||||||
|
chart1.Series.Clear();
|
||||||
|
Series x_actual = new Series("Actual x Value");
|
||||||
|
x_actual.ChartType = SeriesChartType.Spline;
|
||||||
|
for (int i = 0; i < NumberOfSamples; i++)
|
||||||
|
{
|
||||||
|
_x[i] += ((255.0 / NumberOfSamples) * i);
|
||||||
|
for (int k = 0; k < windowSize; k++)
|
||||||
|
{
|
||||||
|
w[k, i] = rnd.NextDouble();
|
||||||
|
//Console.WriteLine(String.Format("Weight[{0}, {1}]: {2}",k,i, w[k, i]));
|
||||||
|
}
|
||||||
|
x_actual.Points.AddXY(i, _x[i]);
|
||||||
|
}
|
||||||
|
chart1.Series.Add(x_actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Graphen Clearen
|
||||||
|
private void button2_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
chart1.Series.Clear();
|
||||||
|
Series x_actual = new Series("Actual x Value");
|
||||||
|
x_actual.ChartType = SeriesChartType.Spline;
|
||||||
|
for (int i = 0; i < NumberOfSamples; i++)
|
||||||
|
{
|
||||||
|
x_actual.Points.AddXY(i, _x[i]);
|
||||||
|
}
|
||||||
|
chart1.Series.Add(x_actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bild Laden
|
||||||
|
private void button3_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
|
||||||
|
OpenFileDialog openFileDialog = new OpenFileDialog();
|
||||||
|
|
||||||
|
if(openFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Bitmap img = new Bitmap(openFileDialog.FileName);
|
||||||
|
pixel_array = new double[img.Width * img.Height];
|
||||||
|
|
||||||
|
for (int i = 1; i < img.Width; i++)
|
||||||
|
{
|
||||||
|
for (int j = 1; j < img.Height; j++)
|
||||||
|
{
|
||||||
|
|
||||||
|
Color pixel = img.GetPixel(i, j);
|
||||||
|
pixel_array[j*i] = pixel.R;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NumberOfSamples = (img.Width * img.Height) / 2;
|
||||||
|
comboBox2.Items.Add(NumberOfSamples);
|
||||||
|
_x = pixel_array;
|
||||||
|
w = new double[NumberOfSamples, NumberOfSamples];
|
||||||
|
for (int i = 0; i < NumberOfSamples; i++)
|
||||||
|
{
|
||||||
|
for (int k = 1; k < NumberOfSamples; k++)
|
||||||
|
{
|
||||||
|
w[k, i] = rnd.NextDouble();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(Exception exep)
|
||||||
|
{
|
||||||
|
MessageBox.Show("Konnte Bild nicht laden.");
|
||||||
|
MessageBox.Show(String.Format("{0}", exep.ToString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue