public void testHopfieldPersist() throws Exception { boolean input[] = {true, false, true, false}; BasicNetwork network = new BasicNetwork(); network.addLayer(new HopfieldLayer(4)); NeuralData data = new BiPolarNeuralData(input); Train train = new TrainHopfield(data, network); train.iteration(); EncogPersistedCollection encog = new EncogPersistedCollection(); encog.add(network); encog.save("encogtest.xml"); EncogPersistedCollection encog2 = new EncogPersistedCollection(); encog2.load("encogtest.xml"); new File("encogtest.xml").delete(); BasicNetwork network2 = (BasicNetwork) encog2.getList().get(0); BiPolarNeuralData output = (BiPolarNeuralData) network2.compute(new BiPolarNeuralData(input)); TestCase.assertTrue(output.getBoolean(0)); TestCase.assertFalse(output.getBoolean(1)); TestCase.assertTrue(output.getBoolean(2)); TestCase.assertFalse(output.getBoolean(3)); }
/** * Set the input to the neural network. * * @param input The input. */ private void setInput(final BiPolarNeuralData input) { double activation; for (int i = 0; i < this.layerF1.getNeuronCount(); i++) { activation = (input.getBoolean(i) ? 1 : 0) / (1 + this.a1 * ((input.getBoolean(i) ? 1 : 0) + this.b1) + this.c1); this.outputF1.setData(i, (activation > 0)); } }
/** * Compute the output from the F1 layer. * * @param input The input to the F1 layer. */ private void computeF1(final BiPolarNeuralData input) { double sum, activation; for (int i = 0; i < this.layerF1.getNeuronCount(); i++) { sum = this.synapseF1toF2.getMatrix().get(i, this.winner) * (this.outputF2.getBoolean(this.winner) ? 1 : 0); activation = ((input.getBoolean(i) ? 1 : 0) + this.d1 * sum - this.b1) / (1 + this.a1 * ((input.getBoolean(i) ? 1 : 0) + this.d1 * sum) + this.c1); this.outputF1.setData(i, activation > 0); } }
String displayTour(BiPolarNeuralData data) { StringBuilder result = new StringBuilder(); int n1, n2; boolean 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(BiPolarNeuralData 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; }
/** * Get the magnitude of the specified input. * * @param input The input to calculate the magnitude for. * @return The magnitude of the specified pattern. */ public double magnitude(final BiPolarNeuralData input) { double result; result = 0; for (int i = 0; i < this.layerF1.getNeuronCount(); i++) { result += input.getBoolean(i) ? 1 : 0; } return result; }
public boolean isValidTour(BiPolarNeuralData 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; }
/** * Copy the output from the network to another object. * * @param output The target object for the output from the network. */ private void getOutput(final BiPolarNeuralData output) { for (int i = 0; i < this.layerF2.getNeuronCount(); i++) { output.setData(i, this.outputF2.getBoolean(i)); } }