Esempio n. 1
0
        //public bool IsSolutionCompleted { get; private set; } = false;

        public Swarm(SwarmSetting setting)
        {
            Setting = setting;
            N       = setting.CostMatrix.GetLength(0);

            for (int i = 0; i < setting.SwarmSize; i++)
            {
                var b = new Bat(0, N)
                {
                    A = (double)BatProblem.Random.Next(700, 1000) / 1000.0,
                    R = BatProblem.Random.NextDouble(),
                    V = 1,
                };
                Bats.Add(b);
            }
            BestPath = new Path();
            BestPath.Cities.Add(0);
        }
Esempio n. 2
0
        public List <int> Solve()
        {
            int n = CostMatrix.Length;

            Path   bestPath     = new Path();
            double bestSolution = double.MaxValue;

            var swarmSetting = new SwarmSetting()
            {
                CostMatrix = CostMatrix,
                SwarmSize  = SwarmSize,
                InitialR   = Random.NextDouble() % 0.41
            };

            for (int t = 0; t < MaxIteration; t++)
            {
                Swarm swarm = new Swarm(swarmSetting);
                for (int tt = 0; tt < n; tt++)
                {
                    if (tt == n - 1)
                    {
                        var tttt = swarm.BestPath.Cities.Distinct().ToList();
                        var dd   = swarm.BestPath.Cities.Where(i => swarm.BestPath.Cities.Count(ii => ii == i) > 1).ToList();
                    }
                    swarm.NextStep();
                }

                if (swarm.BestCost < bestSolution)
                {
                    bestSolution = swarm.BestCost;
                    bestPath     = swarm.BestPath;
                }
            }

            return(bestPath.Cities);
        }