Beispiel #1
0
 public static void Main(string[] args)
 {
     using (var writer = new StreamWriter("stats.csv")
     {
         AutoFlush = true
     })
     {
         writer.WriteLine("fileName, cliqueCount, executionTime");
         var fileNames = Directory.EnumerateFiles(Directory.GetCurrentDirectory()).Where(n => n.Contains("clq"))
                         .ToArray();
         Parallel.ForEach(fileNames,
                          fileName =>
         {
             var graph     = GraphParser.ParseNewGraph(fileName);
             var algorithm = new CplexSolver(graph);
             var timer     = Stopwatch.StartNew();
             var result    = algorithm.FindMaxClique();
             lock (locker)
             {
                 writer.WriteLine(string.Join(", ", fileName, result.Count, timer.Elapsed));
             }
         });
     }
     Console.WriteLine("Done");
     Console.ReadKey(false);
 }
Beispiel #2
0
        public EczaneNobetSonucModel Solve(EczaneNobetTekGrupDataModel data)
        {
            var model = Model(data);
            // Get a solver instance, change your solver
            var solver = new CplexSolver();

            try
            {
                // solve the model
                var solution = solver.Solve(model);
                //var cplexConfiguration = solver.Configuration;

                var modelStatus    = solution.ModelStatus;
                var solutionStatus = solution.Status;
                var modelName      = solution.ModelName;
                var bestBound      = solution.BestBound;

                //var confilicts = new ConflictingSet();
                //confilicts = solution.ConflictingSet;
                //ConstraintsUB = new IEnumerable<Constraint>();
                //ConstraintsUB = confilicts.ConstraintsUB;

                if (modelStatus != ModelStatus.Feasible)
                {
                    throw new Exception("Uygun çözüm bulunamadı!");
                }
                else
                {
                    // import the results back into the model
                    model.VariableCollections.ForEach(vc => vc.SetVariableValues(solution.VariableValues));
                    var objective = solution.ObjectiveValues.Single();
                    var sure      = solution.OverallWallTime;

                    Results = new EczaneNobetSonucModel
                    {
                        CozumSuresi    = sure,
                        ObjectiveValue = objective.Value,
                        ResultModel    = new List <EczaneNobetCozum>()
                    };

                    foreach (var r in data.EczaneNobetTarihAralik.Where(s => _x[s].Value == 1))
                    {
                        Results.ResultModel.Add(new EczaneNobetCozum()
                        {
                            TakvimId          = r.TakvimId,
                            EczaneNobetGrupId = r.EczaneNobetGrupId,
                            NobetGorevTipId   = r.NobetGorevTipId
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                var mesaj = ex.Message;
                throw new Exception(mesaj);
            }
            return(Results);
        }
Beispiel #3
0
        public TransportSonucModel Solve(TransportDataModel data)
        {
            var config = new Configuration
            {
                NameHandling            = NameHandlingStyle.UniqueLongNames,
                ComputeRemovedVariables = true
            };

            using (var scope = new ModelScope(config))
            {
                DataModel = data;

                Model();

                // Get a solver instance, change your solver
                var solver = new CplexSolver();

                // solve the model
                var solution = solver.Solve(_model);

                // import the results back into the model
                _model.VariableCollections.ForEach(vc => vc.SetVariableValues(solution.VariableValues));

                // print objective and variable decisions
                var objective = solution.ObjectiveValues.Single();

                Results = new TransportSonucModel
                {
                    ObjectiveValue = objective.Value,
                    ResultModel    = new List <TransportSonuc>()
                };

                foreach (var r in DataModel.Maliyetler.Where(s => x[s].Value > 0))
                {
                    Results.ResultModel.Add(new TransportSonuc()
                    {
                        DepoId    = r.DepoId,
                        FabrikaId = r.FabrikaId,
                        Sonuc     = x[r].Value
                    });
                }
            }

            return(Results);
        }
        /// <summary>
        /// The main method
        /// </summary>
        /// <param name="args">
        /// no arguments required
        /// </param>
        static void Main(string[] args)
        {
            var size    = Enumerable.Range(0, 9).ToList();
            var section = size.GroupBy(s => s / 3);

            // you may manipulate the following game, which is the initial state of the Sudoku to your liking
            // note: obviously not observing the rules of Sudoku here makes the problem infeasible
            var game = new int?[, ] {
                //  0    1      2     3     4    5     6     7     8
                { null, 3, null, null, null, null, null, null, null },
                { null, null, null, 1, 9, 5, null, null, null },
                { null, null, 8, null, null, null, null, 6, null },

                { 8, null, null, null, 6, null, null, null, null },
                { 4, null, null, 8, null, null, null, null, 1 },
                { null, null, null, null, 2, null, null, null, null },

                { null, 6, null, null, null, null, 2, 8, null },
                { null, null, null, 4, 1, 9, null, null, 5 },
                { null, null, null, null, null, null, null, 7, null },
            };

            // use default settings
            var config = new Configuration();

            config.NameHandling            = NameHandlingStyle.UniqueLongNames;
            config.ComputeRemovedVariables = true;
            using (var scope = new ModelScope(config))
            {
                // create a model, based on given data and the model scope
                var sudokuModel = new SudokuModel(size, section, game);

                // get a solver instance, change your solver
                var solver = new CplexSolver();

                // solve the model
                var solution = solver.Solve(sudokuModel.Model);

                // print objective and variable decisions
                Console.WriteLine("Result: ");
                foreach (var row in size)
                {
                    foreach (var col in size)
                    {
                        foreach (var value in size)
                        {
                            if (sudokuModel.field[col, row, value].Value > 0)
                            {
                                Console.Write(string.Format("   {0}", value + 1));
                            }
                        }
                        if ((col + 1) % 3 == 0)
                        {
                            Console.Write("  ");
                        }
                    }
                    if ((row + 1) % 3 == 0)
                    {
                        Console.WriteLine();
                    }
                    Console.WriteLine();
                }
            }
        }
        /// <summary>
        /// The main method
        /// </summary>
        /// <param name="args">
        /// no arguments required
        /// </param>
        static void Main(string[] args)
        {
            // create jobs with their respective color and due date
            var jobs = new List <Job>
            {
                new Job {
                    Color = Color.White, DueDate = 40
                },
                new Job {
                    Color = Color.Brown, DueDate = 40
                },
                new Job {
                    Color = Color.Green, DueDate = 40
                },
                new Job {
                    Color = Color.Black, DueDate = 40
                },
            };

            // add setup times for the jobs created beforehand
            var setupTimes = new Dictionary <Job, int>()
            {
                { jobs.Single(j => j.Color == Color.White), 4 },
                { jobs.Single(j => j.Color == Color.Brown), 2 },
                { jobs.Single(j => j.Color == Color.Green), 3 },
                { jobs.Single(j => j.Color == Color.Black), 0 },
            };

            // add tasks to the different jobs created beforehand
            var tasks = new List <Task>
            {
                //  white
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.White), StepNumber = 1, Duration = 4
                },
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.White), StepNumber = 2, Duration = 3
                },
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.White), StepNumber = 3, Duration = 4
                },
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.White), StepNumber = 4, Duration = 2
                },

                // brown
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.Brown), StepNumber = 1, Duration = 4
                },
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.Brown), StepNumber = 2, Duration = 6
                },
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.Brown), StepNumber = 3, Duration = 4
                },
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.Brown), StepNumber = 4, Duration = 3
                },

                // green
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.Green), StepNumber = 1, Duration = 3
                },
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.Green), StepNumber = 2, Duration = 4
                },
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.Green), StepNumber = 3, Duration = 3
                },
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.Green), StepNumber = 4, Duration = 3
                },

                // black
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.Black), StepNumber = 1, Duration = 4
                },
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.Black), StepNumber = 2, Duration = 8
                },
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.Black), StepNumber = 3, Duration = 2
                },
                new Task()
                {
                    Job = jobs.Single(j => j.Color == Color.Black), StepNumber = 4, Duration = 8
                },
            };

            // create a rank for each task
            var ranks = Enumerable.Range(0, tasks.Count).ToList();

            // set up the machines with their name, the beforehand created setup times and the supported tasks as well as their setup cost
            var machines = new List <Machine>
            {
                new Machine
                {
                    MachineId      = "A",
                    SetupTimes     = setupTimes,
                    SupportedTasks = tasks.Where(task => new int[] { 1, 2 }.Contains(task.StepNumber)).ToList(),
                    Cost           = 1
                },
                new Machine
                {
                    MachineId      = "B",
                    SetupTimes     = setupTimes,
                    SupportedTasks = tasks.Where(task => new int[] { 1, 2, 3 }.Contains(task.StepNumber)).ToList(),
                    Cost           = 2
                },
                new Machine
                {
                    MachineId      = "C",
                    SetupTimes     = setupTimes,
                    SupportedTasks = tasks.Where(task => new int[] { 2, 3, 4 }.Contains(task.StepNumber)).ToList(),
                    Cost           = 3
                },
                new Machine
                {
                    MachineId      = "D",
                    SetupTimes     = setupTimes,
                    SupportedTasks = tasks.Where(task => new int[] { 3, 4 }.Contains(task.StepNumber)).ToList(),
                    Cost           = 4
                },
            };


            // register tasks with jobs
            jobs.ForEach(job => job.Tasks.AddRange(tasks.Where(task => task.Job == job).OrderBy(task => task.StepNumber)));

            var config = new Configuration();

            config.NameHandling            = NameHandlingStyle.UniqueLongNames;
            config.ComputeRemovedVariables = true;
            using (var scope = new ModelScope(config))
            {
                // create a model, based on given data and the model scope
                var jobScheduleModel = new JobScheduleModel(jobs, setupTimes, tasks, ranks, machines);

                // Get a solver instance, change your solver
                CplexSolverConfiguration cplexconfig = new CplexSolverConfiguration();
                cplexconfig.TimeLimit = 120;
                var solver = new CplexSolver(cplexconfig);

                // solve the model
                var solution = solver.Solve(jobScheduleModel.Model);

                // print objective and variable decisions
                Console.WriteLine($"Objective: {solution.ObjectiveValues.Single().Key} {(int)Math.Round(solution.ObjectiveValues.Single().Value)}");
                Console.WriteLine($"Latest End: {(int)jobScheduleModel.LatestEnd.Value}");

                foreach (var machine in machines)
                {
                    foreach (var rank in ranks)
                    {
                        foreach (var task in machine.SupportedTasks)
                        {
                            if ((int)Math.Round(jobScheduleModel.taskMachineAssignment[task, machine, rank].Value) > 0)
                            {
                                Console.WriteLine(
                                    $"Machine {machine}, Rank {rank}: Assigns Task={task}, Start: {(int)Math.Round(jobScheduleModel.startTime[task, machine, rank].Value):####}, Duration: {task.Duration:##}, End: {(int)Math.Round(jobScheduleModel.startTime[task, machine, rank].Value) + task.Duration:####}");
                            }
                        }
                    }

                    Console.WriteLine("---");
                }

                foreach (var job in jobs)
                {
                    foreach (var task in job.Tasks)
                    {
                        foreach (var machine in machines.Where(m => m.SupportedTasks.Contains(task)))
                        {
                            foreach (var rank in ranks)
                            {
                                if ((int)Math.Round(jobScheduleModel.taskMachineAssignment[task, machine, rank].Value) > 0)
                                {
                                    Console.WriteLine(
                                        $"Task={task}, Rank {rank}: Assigned Machine {machine}, Start: {(int)Math.Round(jobScheduleModel.startTime[task, machine, rank].Value):####}, Duration: {task.Duration:##}, End: {(int)Math.Round(jobScheduleModel.startTime[task, machine, rank].Value) + task.Duration:####}");
                                }
                            }
                        }
                    }

                    Console.WriteLine("---");
                }
            }
        }