Пример #1
0
        public Hive(int totalNumberBees, int numberInactive, int numberActive, int numberScout, int maxNumberVisits, int maxNumberCycles, CitiesData citiesData, int BeginningAndEnd = 0)  
        {
            //random = new Random(0);
            this.BeginningAndEnd = BeginningAndEnd;
            this.totalNumberBees = totalNumberBees;
            this.numberInactive = numberInactive;
            this.numberActive = numberActive;
            this.numberScout = numberScout;
            this.maxNumberVisits = maxNumberVisits;
            this.maxNumberCycles = maxNumberCycles;
            //this.maxCyclesWithNoImprovement = maxCyclesWithNoImprovement;

            //this.citiesData = new CitiesData(citiesData.cities.Length); // hive's copy of problem-specific data
            this.citiesData = citiesData; // reference to CityData

            // this.probPersuasion & this.probMistake are hard-coded in class definition

            this.bees = new Bee[totalNumberBees];
            this.bestMemoryMatrix = GenerateRandomMemoryMatrix(); // alternative initializations are possible
            this.bestMeasureOfQuality = MeasureOfQuality(this.bestMemoryMatrix);

            this.indexesOfInactiveBees = new int[numberInactive]; // indexes of bees which are currently inactive

            for (int i = 0; i < totalNumberBees; ++i) // initialize each bee, and best solution
            {
                int currStatus; // depends on i. need status before we can initialize Bee
                if (i < numberInactive)
                {
                    currStatus = 0; // inactive
                    indexesOfInactiveBees[i] = i; // curr bee is inactive
                }
                else if (i < numberInactive + numberScout)
                {
                    currStatus = 2; // scout
                }
                else
                {
                    currStatus = 1; // active
                }

                SubCalendarEvent[] randomMemoryMatrix = GenerateRandomMemoryMatrix();
                double mq = MeasureOfQuality(randomMemoryMatrix);
                int numberOfVisits = 0;

                bees[i] = new Bee(currStatus, randomMemoryMatrix, mq, numberOfVisits); // instantiate current bee

                // does this bee have best solution?
                if (bees[i].measureOfQuality < bestMeasureOfQuality) // curr bee is better (< because smaller is better)
                {
                    Array.Copy(bees[i].memoryMatrix, this.bestMemoryMatrix, bees[i].memoryMatrix.Length);
                    this.bestMeasureOfQuality = bees[i].measureOfQuality;
                }
            } // each bee

        } // TravelingSalesmanHive ctor
Пример #2
0
        public static Tuple<ICollection<SubCalendarEvent>, double> Run(ICollection<SubCalendarEvent> ListOfElements, int BeginningAndEnd=0)
        {
#if EnableHive            
            if (ListOfElements.Count < 3)
            {
                return new Tuple<ICollection<SubCalendarEvent>, double>(ListOfElements, 0);
            }
            
            CitiesData citiesData = new CitiesData(ListOfElements.ToList());
            int totalNumberBees = 100;
            int numberInactive = 20;
            int numberActive = 50;
            int numberScout = 30;

            int maxNumberVisits = 50;
            int maxNumberCycles = 20;

            Hive hive = new Hive(totalNumberBees, numberInactive, numberActive, numberScout, maxNumberVisits, maxNumberCycles, citiesData, BeginningAndEnd);


            bool doProgressBar = false;
            hive.Solve(doProgressBar);
            return hive.getBestPath();
#endif
            



#if LinearTSP
            Coordinate[] data = new Coordinate[ListOfElements.Count];
            Dictionary<int, SubCalendarEvent> DictOFData = new Dictionary<int,SubCalendarEvent>();
            int NameIndex = 0;
            foreach (SubCalendarEvent eachSubCalendarEvent in ListOfElements)
            {
                data[NameIndex] = new Coordinate(NameIndex, eachSubCalendarEvent);
                DictOFData.Add(NameIndex++, eachSubCalendarEvent);
            }

            SolverContext context = SolverContext.GetContext();
            Model model = context.CreateModel();

            // ------------
            // Parameters
            Set city = new Set(Domain.IntegerNonnegative, "city");
            Parameter dist = new Parameter(Domain.Real, "dist", city, city);
            var arcs = from p1 in data
                       from p2 in data
                       select new Arc { City1 = p1.Name, City2 = p2.Name, Distance = p1.Distance(p2, data.Length) };
            dist.SetBinding(arcs, "Distance", "City1", "City2");
            model.AddParameters(dist);

            // ------------
            // Decisions
            Decision assign = new Decision(Domain.IntegerRange(0, 1), "assign", city, city);
            Decision rank = new Decision(Domain.RealNonnegative, "rank", city);
            model.AddDecisions(assign, rank);

            // ------------
            // Goal: minimize the length of the tour.
            Goal goal = model.AddGoal("TourLength", GoalKind.Minimize,
              Model.Sum(Model.ForEach(city, i => Model.ForEachWhere(city, j => dist[i, j] * assign[i, j], j => i != j))));

            // ------------
            // Enter and leave each city only once.
            int N = data.Length;
            model.AddConstraint("assign_1",
              Model.ForEach(city, i => Model.Sum(Model.ForEachWhere(city, j => assign[i, j],
                j => i != j)) == 1));
            model.AddConstraint("assign_2",
              Model.ForEach(city, j => Model.Sum(Model.ForEachWhere(city, i => assign[i, j], i => i != j)) == 1));

            // Forbid subtours (Miller, Tucker, Zemlin - 1960...)
            model.AddConstraint("no_subtours",
              Model.ForEach(city,
                i => Model.ForEachWhere(city,
                  j => rank[i] + 1 <= rank[j] + N * (1 - assign[i, j]),
                  j => Model.And(i != j, i >= 1, j >= 1)
                )
              )
            );

            Solution solution = context.Solve();
            double Cost = goal.ToDouble();
            List<SubCalendarEvent> OptimizedSubCalEvents = new List<SubCalendarEvent>();

            var tour = from p in assign.GetValues() where (double)p[0] > 0.9 select p;

            foreach (var i in tour.ToArray())
            {
                int MyIndex =Convert.ToInt32(i[2]);
                OptimizedSubCalEvents.Add(DictOFData[MyIndex]);
                //Console.WriteLine(i[1] + " -> " + );
            }

            context.ClearModel();

            return new Tuple<ICollection<SubCalendarEvent>, double>(OptimizedSubCalEvents, Cost);
#endif


        }