Example #1
0
        /// <summary>
        /// Find a new pattern (use slave model to calc size contents of new pattern)
        /// </summary>
        /// <returns>True if found a new pattern, otherwise false</returns>
        public bool FindNewPattern()
        {
            //try to find a new pattern which will eventually result in less total rolls
            SetCurrentToSlaveModel();

            Solution sol = _context.Solve();

            Goal     objective  = sol.Goals.First();
            Decision newPattern = sol.Decisions.First();

            if (sol.Quality == SolverQuality.Optimal && objective.ToDouble() < _objLimit)
            {
                Console.WriteLine("\nFound a new {0}th pattern (based on prev shadow prices)", _patternCount + 1);
                //Console.WriteLine("  ({0})",
                //    string.Join(", ",_shadowPrices.Select(pair => string.Format("{0}=>{1}",pair.Key, pair.Value)))
                //);
                foreach (Object[] patternRoll in newPattern.GetValues())
                {
                    _patternRolls.Add(new PatternItemSize(id: _patternCount, width:  Convert.ToInt32(patternRoll[1]), count: Convert.ToInt32(patternRoll[0])));
                }
                _patterns.Add(new CuttingPattern()
                {
                    PatternID = _patternCount, Count = 0
                });
                _patternCount++;
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #2
0
        private void button2_Click(object sender, EventArgs e)
        {
            Decision mixedStrategy = SBFollower.SBFF();

            listBox2.Items.Clear();
            listBox2.Items.Add(" ");
            foreach (object[] value in mixedStrategy.GetValues())
            {
                listBox2.Items.Add(value[1].ToString() + " : " + ((double)value[0]).ToString("P2"));
                listBox2.Items.Add("-------------------------------------------------");
            }
        }
Example #3
0
        private IDictionary <int, double> GetStartTimesByTask()
        {
            // The "start" decision has the start times for each *event*. Index 0 has the start and index 1 has the event.
            // Recall that the event set was created using Rationals, so we must convert to int here.
            var eventToStart = start.GetValues().ToDictionary(o => int.Parse(o[1].ToString()), o => (double)o[0]);

            // Group the isActive decision by task.
            var isActiveByTask = isActive.GetValues().GroupBy(o => o[1]);

            // Find the first event where each task is active, and transform the output into (task, event) pairs.
            var firstActiveByTask = isActiveByTask
                                    .Select(g => g.First(a => (double)a[0] > 0.5))
                                    .Select(o => new { TaskID = (int)((double)o[1]), EventID = int.Parse(o[2].ToString()) });

            // Now we can use the (task, event) pairs to get a dictionary that maps task IDs to start dates.
            return(firstActiveByTask.ToDictionary(f => f.TaskID, f => eventToStart[f.EventID]));
        }
        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


        }
Example #5
0
        static void Main(string[] args)
        {
            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) };

            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("assign1",
              Model.ForEach(city, i => Model.Sum(Model.ForEachWhere(city, j => assign[i, j],
                j => i != j)) == 1));
            model.AddConstraint("assign2",
              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();

            // Retrieve solution information.
            Console.WriteLine("Cost = {0}", goal.ToDouble());
            Console.WriteLine("Tour:");
            var tour = from p in assign.GetValues() where (double)p[0] > 0.9 select p[2];
            foreach (var i in tour.ToArray())
            {
                Console.Write(i + " -> ");
            }
            Console.WriteLine();
            Console.WriteLine("Press enter to continue...");
            Console.ReadLine();
        }