Example #1
0
        public void GapsBetweenSegmentsAreFilledUp()
        {
            // Arrange
            var constraints = new ConstraintsCollection(
                new VelocityConstraint(0, 1000, 500),
                new VelocityConstraint(1000, 1000, 400),
                new VelocityConstraint(2500, 1000, 200)); // start 500mm after end of last segment (2000)

            // Act
            var effectiveSegments = constraints.GetEffectiveConstraints();

            // Assert
            Assert.Equal(3, effectiveSegments.Count);

            Assert.Equal(0, effectiveSegments[0].Start, _delta);
            Assert.Equal(1000, effectiveSegments[0].Length, _delta);
            Assert.Equal(500, effectiveSegments[0].MaximumVelocity, _delta);

            Assert.Equal(1000, effectiveSegments[1].Start, _delta);
            Assert.Equal(1500, effectiveSegments[1].Length, _delta);   // length is filled up! original length of second segment was 1000
            Assert.Equal(400, effectiveSegments[1].MaximumVelocity, _delta);

            Assert.Equal(2500, effectiveSegments[2].Start, _delta);
            Assert.Equal(1000, effectiveSegments[2].Length, _delta);
            Assert.Equal(200, effectiveSegments[2].MaximumVelocity, _delta);
        }
Example #2
0
        public void GetEffectiveSegmentsWorks()
        {
            // Arrange
            var constraints = new ConstraintsCollection(
                new VelocityConstraint(0, 1000, 500),
                new VelocityConstraint(1000, 1000, 400),
                new VelocityConstraint(500, 1000, 200));

            // Act
            var effectiveSegments = constraints.GetEffectiveConstraints();

            // Assert
            Assert.Equal(3, effectiveSegments.Count);
            Assert.Equal(0, effectiveSegments[0].Start, _delta);
            Assert.Equal(500, effectiveSegments[0].Length, _delta);
            Assert.Equal(500, effectiveSegments[0].MaximumVelocity, _delta);

            Assert.Equal(500, effectiveSegments[1].Start, _delta);
            Assert.Equal(1000, effectiveSegments[1].Length, _delta);
            Assert.Equal(200, effectiveSegments[1].MaximumVelocity, _delta);

            Assert.Equal(1500, effectiveSegments[2].Start, _delta);
            Assert.Equal(500, effectiveSegments[2].Length, _delta);
            Assert.Equal(400, effectiveSegments[2].MaximumVelocity, _delta);
        }
Example #3
0
        public IActionResult GenerateTimetables(AlgorithmInputsViewModel model)
        {
            if (ModelState.IsValid)
            {
                var scheduler       = new Scheduler();
                var selectedCourses = new List <Course>();

                if (model.Ids == null)
                {
                    return(Content(string.Empty));
                }

                foreach (var id in model.Ids)
                {
                    var course = coursesDataService.Get(id);
                    selectedCourses.Add(course);
                }

                var constraints = new ConstraintsCollection
                {
                    new MinimumFreeDaysConstraint(model.FreeDays),
                    new ClashesConstraint(model.Clashes),
                    new MaxGapBetweenClassesConstraint(model.MaxGap)
                };

                var solutions = scheduler.SolveSssp(selectedCourses, constraints);

                var response = solutions
                               .Select(s => s.ExportToJson())
                               .ToArray();

                return(Json(response));

                // ReDirect To a HttpGet Action After a successful post operation.
                //return RedirectToAction(nameof(CourseDetails), new { courseId });
            }
            return(null);
            // else, return the form with user previous entered values
            //return View(model);
        }
Example #4
0
        protected void WriteConstraints(ConstraintsCollection constraints, bool isAdding = false)
        {
            var uniq = GetUniqueKeyWriter();

            if (ColonBeforeConstraints)
            {
                Builder.AppendLine(",");
            }
            if (constraints.PrimaryKey != null && !constraints.PrimaryKey.Ignore)
            {
                uniq.Write(constraints.PrimaryKey);
                Builder.AppendLine(",");
                if (isAdding)
                {
                    Builder.AppendFormat(" {0} ", GetAddConstraintPrefix());
                }
            }

            if (constraints.Uniques.Count > 0)
            {
                foreach (var uc in constraints.Uniques)
                {
                    uniq.Write(uc);
                    Builder.AppendLine(",");
                    if (isAdding)
                    {
                        Builder.AppendFormat(" {0} ", GetAddConstraintPrefix());
                    }
                }
            }

            if (constraints.ForeignKeys.Count > 0)
            {
                var w = GetForeignKeyWriter();
                foreach (var key in constraints.ForeignKeys)
                {
                    w.Write(key);
                    Builder.AppendLine(",");
                    if (isAdding)
                    {
                        Builder.AppendFormat(" {0} ", GetAddConstraintPrefix());
                    }
                }
            }

            if (constraints.Checks.Count > 0)
            {
                var chkWriter = GetCheckWriter();
                foreach (var ch in constraints.Checks)
                {
                    chkWriter.Write(ch);
                    Builder.AppendLine(",");
                    if (isAdding)
                    {
                        Builder.AppendFormat(" {0} ", GetAddConstraintPrefix());
                    }
                }
            }
            if (isAdding)
            {
                Builder.RemoveLastIfEquals(" " + GetAddConstraintPrefix() + " ");
            }
            var custom = constraints.GetSpecificConstraints(_engine);

            if (!custom.IsNullOrEmpty())
            {
                foreach (var ch in custom)
                {
                    Builder.AppendLine().Append(ch).AppendLine(",");
                }
            }
            Builder.RemoveLastIfEquals("," + Environment.NewLine);
        }