当前位置: 首页>>代码示例>>C#>>正文


C# Decision.GetValues方法代码示例

本文整理汇总了C#中Decision.GetValues方法的典型用法代码示例。如果您正苦于以下问题:C# Decision.GetValues方法的具体用法?C# Decision.GetValues怎么用?C# Decision.GetValues使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Decision的用法示例。


在下文中一共展示了Decision.GetValues方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Run

        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


//.........这里部分代码省略.........
开发者ID:jerbio,项目名称:My24HourTimerWPF,代码行数:101,代码来源:DistanceSolver.cs

示例2: Main

        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();
        }
开发者ID:jamesjrg,项目名称:taipan,代码行数:60,代码来源:Program.cs


注:本文中的Decision.GetValues方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。