Exemple #1
0
        public void CalculateWeights(BoltzmannMachine logic)
        {
            for (var sourceTour = 0; sourceTour < NUM_CITIES; sourceTour++)
            {
                for (var sourceCity = 0; sourceCity < NUM_CITIES; sourceCity++)
                {
                    var sourceIndex = sourceTour * NUM_CITIES + sourceCity;
                    for (var targetTour = 0; targetTour < NUM_CITIES; targetTour++)
                    {
                        for (var targetCity = 0; targetCity < NUM_CITIES; targetCity++)
                        {
                            var    targetIndex = targetTour * NUM_CITIES + targetCity;
                            double weight      = 0;

                            if (sourceIndex != targetIndex)
                            {
                                var predTargetTour = targetTour == 0 ? NUM_CITIES - 1 : targetTour - 1;
                                var succTargetTour = targetTour == NUM_CITIES - 1 ? 0 : targetTour + 1;
                                if ((sourceTour == targetTour) || (sourceCity == targetCity))
                                {
                                    weight = -gamma;
                                }
                                else if ((sourceTour == predTargetTour) || (sourceTour == succTargetTour))
                                {
                                    weight = -distance[sourceCity][targetCity];
                                }
                            }
                            logic.SetWeight(sourceIndex, targetIndex, weight);
                        }
                    }
                    logic.Threshold[sourceIndex] = -gamma / 2;
                }
            }
        }
 private void ValidateHopfield(BoltzmannMachine network)
 {
     network.Run();// at least see if it can run without an exception
     Assert.AreEqual(4, network.NeuronCount);
     Assert.AreEqual(4, network.CurrentState.Count);
     Assert.AreEqual(16, network.Weights.Length);
     Assert.AreEqual(1.0, network.GetWeight(1, 1));
     Assert.AreEqual(2.0, network.Threshold[2]);
 }
        /// <summary>
        /// Generate the network.
        /// </summary>
        public IMLMethod Generate()
        {
            var boltz = new BoltzmannMachine(_neuronCount);

            boltz.Temperature  = _temperature;
            boltz.RunCycles    = _runCycles;
            boltz.AnnealCycles = _annealCycles;
            return(boltz);
        }
        public void TestPersistSerial()
        {
            BoltzmannMachine network = new BoltzmannMachine(4);

            network.SetWeight(1, 1, 1);
            network.Threshold[2] = 2;

            SerializeObject.Save(SERIAL_FILENAME.ToString(), network);
            BoltzmannMachine network2 = (BoltzmannMachine)SerializeObject.Load(SERIAL_FILENAME.ToString());

            ValidateHopfield(network2);
        }
        public void TestPersistEG()
        {
            BoltzmannMachine network = new BoltzmannMachine(4);

            network.SetWeight(1, 1, 1);
            network.Threshold[2] = 2;

            EncogDirectoryPersistence.SaveObject(EG_FILENAME, network);
            BoltzmannMachine network2 = (BoltzmannMachine)EncogDirectoryPersistence.LoadObject(EG_FILENAME);

            ValidateHopfield(network2);
        }
Exemple #6
0
        public void run()
        {
            var boltz = new BoltzmannMachine(NEURON_COUNT);

            CreateCities();
            CalculateWeights(boltz);

            boltz.Temperature = 100;
            do
            {
                boltz.EstablishEquilibrium();
                Console.WriteLine(boltz.Temperature + " : " + DisplayTour(boltz.CurrentState));
                boltz.DecreaseTemperature(0.99);
            } while (!IsValidTour(boltz.CurrentState));

            Console.WriteLine("Final Length: " + LengthOfTour(boltz.CurrentState));
        }
        public void CalculateWeights(BoltzmannMachine network)
        {
            int    n1, n2, n3, n4;
            int    i, j;
            int    predN3, succN3;
            double weight;

            for (n1 = 0; n1 < NUM_CITIES; n1++)
            {
                for (n2 = 0; n2 < NUM_CITIES; n2++)
                {
                    i = n1 * NUM_CITIES + n2;
                    for (n3 = 0; n3 < NUM_CITIES; n3++)
                    {
                        for (n4 = 0; n4 < NUM_CITIES; n4++)
                        {
                            j      = n3 * NUM_CITIES + n4;
                            weight = 0;
                            if (i != j)
                            {
                                predN3 = (n3 == 0 ? NUM_CITIES - 1 : n3 - 1);
                                succN3 = (n3 == NUM_CITIES - 1 ? 0 : n3 + 1);
                                if ((n1 == n3) || (n2 == n4))
                                {
                                    weight = -gamma;
                                }
                                else if ((n1 == predN3) || (n1 == succN3))
                                {
                                    weight = -distance[n2][n4];
                                }
                            }
                            network.SetWeight(i, j, weight);
                        }
                    }
                    network.Threshold[i] = (-gamma / 2);
                }
            }
        }