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); }
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 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 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()); }
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)); }
/// <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 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> /// 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); input.CopyTo(CurrentState.Data, 0, input.Count); Run(); EngineArray.ArrayCopy(CurrentState.Data, result.Data); return(result); }
/// <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); }
/// <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); }
public void Evaluate(HopfieldNetwork hopfield, String[][] pattern) { for (int i = 0; i < pattern.Length; i++) { BiPolarMLData pattern1 = ConvertPattern(pattern, i); hopfield.CurrentState = pattern1; int cycles = hopfield.RunUntilStable(100); BiPolarMLData pattern2 = hopfield.CurrentState; Console.WriteLine("Cycles until stable(max 100): " + cycles + ", result="); Display(pattern1, pattern2); Console.WriteLine(@"----------------------"); } }
/// <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); input.CopyTo(CurrentState.Data, 0, input.Count); Run(); for (int i = 0; i < CurrentState.Count; i++) { result.SetBoolean(i, BiPolarUtil.Double2bipolar(CurrentState[i])); } EngineArray.ArrayCopy(CurrentState.Data, result.Data); return(result); }
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); }
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> /// 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); }
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); }
/// <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(); } }
/// <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 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> /// 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> /// 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); }
/// <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); }