public void Execute(IExampleInterface app) { this.app = app; // Create the neural network. BasicLayer hopfield; var network = new HopfieldNetwork(4); // This pattern will be trained bool[] pattern1 = {true, true, false, false}; // This pattern will be presented bool[] pattern2 = {true, false, false, false}; IMLData result; var data1 = new BiPolarMLData(pattern1); var data2 = new BiPolarMLData(pattern2); var set = new BasicMLDataSet(); set.Add(data1); // train the neural network with pattern1 app.WriteLine("Training Hopfield network with: " + FormatBoolean(data1)); network.AddPattern(data1); // present pattern1 and see it recognized result = network.Compute(data1); app.WriteLine("Presenting pattern:" + FormatBoolean(data1) + ", and got " + FormatBoolean(result)); // Present pattern2, which is similar to pattern 1. Pattern 1 // should be recalled. result = network.Compute(data2); app.WriteLine("Presenting pattern:" + FormatBoolean(data2) + ", and got " + FormatBoolean(result)); }
public ART1(int theF1Count, int theF2Count) { if ((((uint) theF2Count) + ((uint) theF2Count)) <= uint.MaxValue) { goto Label_00FE; } goto Label_00F2; Label_0026: this.Reset(); if (-2 != 0) { if (0 == 0) { return; } goto Label_00FE; } goto Label_00F2; Label_0031: this._noWinner = this._f2Count; if (((uint) theF1Count) <= uint.MaxValue) { goto Label_0026; } goto Label_00D2; Label_0071: this._weightsF2ToF1 = new Matrix(this._f2Count, this._f1Count); this._inhibitF2 = new bool[this._f2Count]; this._outputF1 = new BiPolarMLData(this._f1Count); this._outputF2 = new BiPolarMLData(this._f2Count); goto Label_0031; Label_00D2: if (0 != 0) { goto Label_0071; } if (0 == 0) { this._weightsF1ToF2 = new Matrix(this._f1Count, this._f2Count); if ((((uint) theF1Count) | 0xff) == 0) { goto Label_0031; } goto Label_0071; } goto Label_0026; Label_00F2: this._f1Count = theF1Count; this._f2Count = theF2Count; goto Label_00D2; Label_00FE: this._a1 = 1.0; this._b1 = 1.5; this._c1 = 5.0; this._d1 = 0.9; this._l = 3.0; this._vigilance = 0.9; goto Label_00F2; }
public override sealed IMLData Compute(IMLData input) { int num; BiPolarMLData data = new BiPolarMLData(input.Count); if (0 == 0) { if (((uint) num) <= uint.MaxValue) { if (3 == 0) { return data; } goto Label_0053; } } else { goto Label_0053; } Label_003B: EngineArray.ArrayCopy(base.CurrentState.Data, data.Data); return data; Label_0053: EngineArray.ArrayCopy(input.Data, base.CurrentState.Data); this.Run(); for (num = 0; num < base.CurrentState.Count; num++) { data.SetBoolean(num, BiPolarUtil.Double2bipolar(base.CurrentState[num])); } goto Label_003B; }
public void Display(BiPolarMLData pattern1, BiPolarMLData pattern2) { int index1 = 0; int index2 = 0; for (int row = 0; row < HEIGHT; row++) { var line = new StringBuilder(); for (int col = 0; col < WIDTH; col++) { if (pattern1.GetBoolean(index1++)) line.Append('O'); else line.Append(' '); } line.Append(" -> "); for (int col = 0; col < WIDTH; col++) { if (pattern2.GetBoolean(index2++)) line.Append('O'); else line.Append(' '); } Console.WriteLine(line.ToString()); } }
/// <summary> /// Note: for Hopfield networks, you will usually want to call the "run" /// method to compute the output. /// This method can be used to copy the input data to the current state. A /// single iteration is then run, and the new current state is returned. /// </summary> /// /// <param name="input">The input pattern.</param> /// <returns>The new current state.</returns> public override sealed IMLData Compute(IMLData input) { var result = new BiPolarMLData(input.Count); EngineArray.ArrayCopy(input.Data, CurrentState.Data); Run(); for (int i = 0; i < CurrentState.Count; i++) { result.SetBoolean(i, BiPolarUtil.Double2bipolar(CurrentState[i])); } EngineArray.ArrayCopy(CurrentState.Data, result.Data); return result; }
/// <summary> /// Compute the output from the F1 layer. /// </summary> /// /// <param name="input">The input to the F1 layer.</param> private void ComputeF1(BiPolarMLData input) { for (int i = 0; i < _f1Count; i++) { double sum = _weightsF1ToF2[i, Winner] *((_outputF2.GetBoolean(Winner)) ? 1 : 0); double activation = (((input.GetBoolean(i)) ? 1 : 0) + _d1*sum - _b1) /(1 + _a1 *(((input.GetBoolean(i)) ? 1 : 0) + _d1*sum) + _c1); _outputF1.SetBoolean(i, activation > 0); } }
/// <summary> /// Compute the output for the BasicNetwork class. /// </summary> /// /// <param name="input">The input to the network.</param> /// <returns>The output from the network.</returns> public IMLData Compute(IMLData input) { if (!(input is BiPolarMLData)) { throw new NeuralNetworkError( "Input to ART1 logic network must be BiPolarNeuralData."); } var output = new BiPolarMLData(_f1Count); Compute((BiPolarMLData) input, output); return output; }
/// <summary> /// Classify the input data to a class number. /// </summary> /// /// <param name="input">The input data.</param> /// <returns>The class that the data belongs to.</returns> public int Classify(IMLData input) { var input2 = new BiPolarMLData(_f1Count); var output = new BiPolarMLData(_f2Count); if (input.Count != input2.Count) { throw new NeuralNetworkError("Input array size does not match."); } for (int i = 0; i < input2.Count; i++) { input2.SetBoolean(i, input[i] > 0); } Compute(input2, output); return HasWinner ? Winner : -1; }
/// <summary> /// Construct the network with the specicified neuron count. /// </summary> /// /// <param name="neuronCount">The number of neurons.</param> protected ThermalNetwork(int neuronCount) { _neuronCount = neuronCount; _weights = new double[neuronCount*neuronCount]; _currentState = new BiPolarMLData(neuronCount); }
public BiPolarMLData StringToBipolar(String str) { var result = new BiPolarMLData(str.Length*BITS_PER_CHAR); int currentIndex = 0; for (int i = 0; i < str.Length; i++) { char ch = char.ToUpper(str[i]); int idx = ch - FIRST_CHAR; int place = 1; for (int j = 0; j < BITS_PER_CHAR; j++) { bool value = (idx & place) > 0; result.SetBoolean(currentIndex++, value); place *= 2; } } return result; }
public override sealed IMLData Compute(IMLData input) { BiPolarMLData data = new BiPolarMLData(input.Count); EngineArray.ArrayCopy(input.Data, base.CurrentState.Data); this.Run(); EngineArray.ArrayCopy(base.CurrentState.Data, data.Data); return data; }
private String DisplayTour(BiPolarMLData data) { var result = new StringBuilder(); int n1, n2; bool first; for (n1 = 0; n1 < NUM_CITIES; n1++) { first = true; result.Append("["); for (n2 = 0; n2 < NUM_CITIES; n2++) { if (data.GetBoolean(n1*NUM_CITIES + n2)) { if (first) { first = false; result.Append(n2); } else { result.Append(", " + n2); } } } result.Append("]"); if (n1 != NUM_CITIES - 1) { result.Append(" -> "); } } return result.ToString(); }
public double LengthOfTour(BiPolarMLData data) { double result; int n1, n2, n3; result = 0; for (n1 = 0; n1 < NUM_CITIES; n1++) { for (n2 = 0; n2 < NUM_CITIES; n2++) { if (data.GetBoolean(((n1)%NUM_CITIES)*NUM_CITIES + n2)) break; } for (n3 = 0; n3 < NUM_CITIES; n3++) { if (data.GetBoolean(((n1 + 1)%NUM_CITIES)*NUM_CITIES + n3)) break; } result += distance[n2][n3]; } return result; }
public bool IsValidTour(BiPolarMLData data) { int cities, stops; for (int n1 = 0; n1 < NUM_CITIES; n1++) { cities = 0; stops = 0; for (int n2 = 0; n2 < NUM_CITIES; n2++) { if (data.GetBoolean(n1*NUM_CITIES + n2)) { if (++cities > 1) return false; } if (data.GetBoolean(n2*NUM_CITIES + n1)) { if (++stops > 1) return false; } } if ((cities != 1) || (stops != 1)) return false; } return true; }
public void Execute(IExampleInterface app) { this.app = app; SetupInput(); var pattern = new ART1Pattern(); pattern.InputNeurons = INPUT_NEURONS; pattern.OutputNeurons = OUTPUT_NEURONS; var network = (ART1) pattern.Generate(); for (int i = 0; i < PATTERN.Length; i++) { var dataIn = new BiPolarMLData(input[i]); var dataOut = new BiPolarMLData(OUTPUT_NEURONS); network.Compute(dataIn, dataOut); if (network.HasWinner) { app.WriteLine(PATTERN[i] + " - " + network.Winner); } else { app.WriteLine(PATTERN[i] + " - new Input and all Classes exhausted"); } } }
/// <summary> /// Init the network. /// </summary> /// /// <param name="neuronCount">The neuron count.</param> /// <param name="weights">The weights.</param> /// <param name="output">The toutpu</param> public void Init(int neuronCount, double[] weights, double[] output) { if (neuronCount != output.Length) { throw new NeuralNetworkError("Neuron count(" + neuronCount + ") must match output count(" + output.Length + ")."); } if ((neuronCount*neuronCount) != weights.Length) { throw new NeuralNetworkError("Weight count(" + weights.Length + ") must be the square of the neuron count(" + neuronCount + ")."); } _neuronCount = neuronCount; _weights = weights; _currentState = new BiPolarMLData(neuronCount) {Data = output}; }
/// <summary> /// Set the current state. /// </summary> /// <param name="s">The new current state.</param> public void SetCurrentState(double[] s) { _currentState = new BiPolarMLData(s.Length); EngineArray.ArrayCopy(s, _currentState.Data); }
public String BipolalToString(BiPolarMLData data) { var result = new StringBuilder(); int j, a, p; for (int i = 0; i < (data.Count/BITS_PER_CHAR); i++) { a = 0; p = 1; for (j = 0; j < BITS_PER_CHAR; j++) { if (data.GetBoolean(i*BITS_PER_CHAR + j)) a += p; p *= 2; } result.Append((char) (a + FIRST_CHAR)); } return result.ToString(); }
/// <summary> /// Construct the ART1 network. /// </summary> /// /// <param name="theF1Count">The neuron count for the f1 layer.</param> /// <param name="theF2Count">The neuron count for the f2 layer.</param> public ART1(int theF1Count, int theF2Count) { _a1 = 1; _b1 = 1.5d; _c1 = 5; _d1 = 0.9d; _l = 3; _vigilance = 0.9d; _f1Count = theF1Count; _f2Count = theF2Count; _weightsF1ToF2 = new Matrix(_f1Count, _f2Count); _weightsF2ToF1 = new Matrix(_f2Count, _f1Count); _inhibitF2 = new bool[_f2Count]; _outputF1 = new BiPolarMLData(_f1Count); _outputF2 = new BiPolarMLData(_f2Count); _noWinner = _f2Count; Reset(); }
public BiPolarMLData RandomBiPolar(int size) { var result = new BiPolarMLData(size); for (int i = 0; i < size; i++) { if (ThreadSafeRandom.NextDouble() > 0.5) result[i] = -1; else result[i] = 1; } return result; }
/// <summary> /// Compute the output from the ART1 network. This can be called directly or /// used by the BasicNetwork class. Both input and output should be bipolar /// numbers. /// </summary> /// /// <param name="input">The input to the network.</param> /// <param name="output">The output from the network.</param> public void Compute(BiPolarMLData input, BiPolarMLData output) { int i; for (i = 0; i < _f2Count; i++) { _inhibitF2[i] = false; } bool resonance = false; bool exhausted = false; do { Input = input; ComputeF2(); GetOutput(output); if (Winner != _noWinner) { ComputeF1(input); double magnitudeInput1 = Magnitude(input); double magnitudeInput2 = Magnitude(_outputF1); if ((magnitudeInput2/magnitudeInput1) < _vigilance) { _inhibitF2[Winner] = true; } else { resonance = true; } } else { exhausted = true; } } while (!(resonance || exhausted)); if (resonance) { AdjustWeights(); } }
public int Classify(IMLData input) { int num; BiPolarMLData data = new BiPolarMLData(this._f1Count); BiPolarMLData output = new BiPolarMLData(this._f2Count); if (input.Count != data.Count) { throw new NeuralNetworkError("Input array size does not match."); } goto Label_0022; Label_0013: return -1; Label_0022: num = 0; while (num < data.Count) { data.SetBoolean(num, input[num] > 0.0); num++; } this.Compute(data, output); if (!this.HasWinner) { goto Label_0013; } return this.Winner; if (-1 == 0) { goto Label_0013; } goto Label_0022; }
/// <summary> /// Get the magnitude of the specified input. /// </summary> /// /// <param name="input">The input to calculate the magnitude for.</param> /// <returns>The magnitude of the specified pattern.</returns> public double Magnitude(BiPolarMLData input) { double result; result = 0; for (int i = 0; i < _f1Count; i++) { result += (input.GetBoolean(i)) ? 1 : 0; } return result; }
public void Compute(BiPolarMLData input, BiPolarMLData output) { bool flag; bool flag2; double num2; double num3; int index = 0; if ((((uint) flag) & 0) == 0) { goto Label_016D; } if ((((uint) index) + ((uint) flag)) >= 0) { goto Label_009E; } Label_0040: if (!flag) { if (!flag2) { goto Label_012B; } } else if ((((uint) num2) - ((uint) index)) < 0) { goto Label_016D; } if (flag) { this.AdjustWeights(); if ((((uint) num3) & 0) == 0) { return; } } else { if ((((uint) num2) | 0x7fffffff) != 0) { return; } goto Label_009E; } goto Label_0040; Label_009E: if ((num3 / num2) < this._vigilance) { this._inhibitF2[this.Winner] = true; } else { flag = true; } goto Label_0040; Label_012B: this.Input = input; this.ComputeF2(); this.GetOutput(output); if (this.Winner != this._noWinner) { this.ComputeF1(input); } else { flag2 = true; goto Label_0040; } if ((((uint) num2) | uint.MaxValue) == 0) { goto Label_0197; } if ((((uint) num2) - ((uint) num2)) <= uint.MaxValue) { if ((((uint) num2) - ((uint) flag2)) < 0) { goto Label_018A; } num2 = this.Magnitude(input); num3 = this.Magnitude(this._outputF1); goto Label_009E; } Label_016D: if ((((uint) index) + ((uint) num2)) > uint.MaxValue) { goto Label_0040; } goto Label_0197; Label_018A: this._inhibitF2[index] = false; index++; Label_0197: if (index < this._f2Count) { goto Label_018A; } flag = false; flag2 = false; goto Label_012B; }
/// <summary> /// Copy the output from the network to another object. /// </summary> /// /// <param name="output">The target object for the output from the network.</param> private void GetOutput(BiPolarMLData output) { for (int i = 0; i < _f2Count; i++) { output.SetBoolean(i, _outputF2.GetBoolean(i)); } }
public double Magnitude(BiPolarMLData input) { double num = 0.0; int i = 0; while (i < this._f1Count) { num += input.GetBoolean(i) ? ((double) 1) : ((double) 0); i++; if ((((uint) num) + ((uint) num)) >= 0) { } } return num; }
public BiPolarMLData ConvertPattern(String[][] data, int index) { int resultIndex = 0; var result = new BiPolarMLData(WIDTH*HEIGHT); for (int row = 0; row < HEIGHT; row++) { for (int col = 0; col < WIDTH; col++) { char ch = data[index][row][col]; result.SetBoolean(resultIndex++, ch == 'O'); } } return result; }
private void ComputeF1(BiPolarMLData input) { int i = 0; if ((((uint) i) - ((uint) i)) >= 0) { goto Label_001E; } Label_001A: i++; Label_001E: if (i >= this._f1Count) { return; } double num2 = this._weightsF1ToF2[i, this.Winner] * (this._outputF2.GetBoolean(this.Winner) ? ((double) 1) : ((double) 0)); double num3 = (((input.GetBoolean(i) ? ((double) 1) : ((double) 0)) + (this._d1 * num2)) - this._b1) / ((1.0 + (this._a1 * ((input.GetBoolean(i) ? ((double) 1) : ((double) 0)) + (this._d1 * num2)))) + this._c1); if ((((uint) num2) - ((uint) i)) > uint.MaxValue) { goto Label_001E; } this._outputF1.SetBoolean(i, num3 > 0.0); goto Label_001A; }
/// <summary> /// Note: for Boltzmann networks, you will usually want to call the "run" /// method to compute the output. /// This method can be used to copy the input data to the current state. A /// single iteration is then run, and the new current state is returned. /// </summary> /// /// <param name="input">The input pattern.</param> /// <returns>The new current state.</returns> public override sealed IMLData Compute(IMLData input) { var result = new BiPolarMLData(input.Count); EngineArray.ArrayCopy(input.Data, CurrentState.Data); Run(); EngineArray.ArrayCopy(CurrentState.Data, result.Data); return result; }
public void Init(int neuronCount, double[] weights, double[] output) { object[] objArray; object[] objArray2; if (neuronCount != output.Length) { if (((uint) neuronCount) <= uint.MaxValue) { objArray = new object[5]; objArray[0] = "Neuron count("; } objArray[1] = neuronCount; goto Label_00FE; } Label_0022: if ((neuronCount * neuronCount) != weights.Length) { objArray2 = new object[5]; } else { this._neuronCount = neuronCount; this._weights = weights; BiPolarMLData data = new BiPolarMLData(neuronCount) { Data = output }; this._currentState = data; return; } Label_0063: if (4 != 0) { objArray2[0] = "Weight count("; objArray2[1] = weights.Length; objArray2[2] = ") must be the square of the neuron count("; if ((((uint) neuronCount) - ((uint) neuronCount)) <= uint.MaxValue) { objArray2[3] = neuronCount; objArray2[4] = ")."; throw new NeuralNetworkError(string.Concat(objArray2)); } return; } Label_00FE: objArray[2] = ") must match output count("; objArray[3] = output.Length; objArray[4] = ")."; throw new NeuralNetworkError(string.Concat(objArray)); if ((((uint) neuronCount) + ((uint) neuronCount)) <= uint.MaxValue) { if ((((uint) neuronCount) + ((uint) neuronCount)) >= 0) { } goto Label_0022; } goto Label_0063; }