/// <summary> /// Tests if solutions s1 dominates s2 /// by comparing each objective evaluation /// </summary> /// <param name="s1">First solution</param> /// <param name="s2">Second Solution</param> /// <param name="sm">The solution Manager to work with</param> /// <returns>the result of the test</returns> private bool checkifSlnDominates(ISolution s1, ISolution s2, ISolutionManger sm) { var result = new List <bool>(); // depending on what the objective is find if s1 dominates s2 // by doing simple equality checks for (int i = 0; i < sm.objs.Count; i++) { if (sm.objs[i].isAsc && s1.objectiveEvaluation[i].evaluationResult < s2.objectiveEvaluation[i].evaluationResult) { result.Add(true); continue; } if (!sm.objs[i].isAsc && s1.objectiveEvaluation[i].evaluationResult > s2.objectiveEvaluation[i].evaluationResult) { result.Add(true); continue; } if (s1.objectiveEvaluation[i].evaluationResult == s2.objectiveEvaluation[i].evaluationResult) { continue; } result.Add(false); } if (result.Count == 0 || result.Any(x => x == false)) { return(false); } return(true); }
///// <summary> ///// inserts deleted solutions into the database ///// </summary> ///// <param name="solutions">The solutions to be inserted into the database</param> ///// <returns>the same solutions but they are now in the deleted_solutions Table</returns> //public override IEnumerable<ISolution>insertIntoDatabase(IEnumerable<ISolution> solutions) //{ // var dpApi = new DbApi(); // return dpApi.insertIntoDeletedSolutions(solutions); //} /// <summary> /// runs the agent after checking if its activated by selecting solutions not to be deleted /// </summary> /// <param name="sm">the solution manager to work with</param> /// <returns>An empty list of solutions</returns> public override IEnumerable <ISolution> run(ISolutionManger sm) { // if activated destroy hehehehehawhawhaw if (activate(sm)) { destroy(select(sm)); } return(new List <ISolution>()); }
/// <summary> /// theis method evaluates the rule on the solution object /// by compiling the rule expression and returning a simple true or false /// depecting wether this rule is satsfied by the solution /// </summary> /// <param name="sm">the solution manager to work with</param> /// <returns>the solutions which satify this rule</returns> public override IEnumerable <ISolution> select(ISolutionManger sm) { //get where condition by compiling it var func = ruleExpr.Compile(); //apply the compiled where condition return(sm.population.Where(x => x.objectiveEvaluation .Any(y => y.objectiveName == _objectiveName && func(y)))); }
/// <summary> /// Selects a solution at random using Secure random a non-pseudo random number generator /// selects a soltuion clones it and returns it /// </summary> /// <param name="sm">the solution manger to work with </param> /// <returns>a list of the new solution</returns> public override IEnumerable <ISolution> select(ISolutionManger sm) { var rnd = new SecureRandom(); // create an object of a non-psedo random number generator int r = rnd.Next(sm.population.Count()); // find the next random number var sln = sm.population.ElementAt(r); // fetch the random solution var randomSln = sln.doClone(); // do a clone of the fetched object var result = new List <ISolution>(); result.Add(randomSln); return(result); // wrap the clonned object in a list and return }
/// <summary> /// First checks if the solution is activated and then creates new solution /// then inserts the newly created solutions into the database after assinging /// them a batch id and solution id else returns an empty solution /// </summary> /// <param name="sm">the solution manager to be used while running the agent</param> /// <returns>the solutions generated by the agent</returns> public override IEnumerable <ISolution> run(ISolutionManger sm) { var temp = new List <ISolution>(); // check if agent is active if (activate(sm)) { //create modified solutions and insert them into the database after //attaching a solution and bacth id return(insertIntoDatabase(runWithSolutions(select(sm), sm).Select(x => { x.solutionid = generateId.getId(); x.batchid = batchid; return x; }))); } return(temp); }
/// <summary> /// First checks if the solution is activated and then creates new solution /// then inserts the newly created solutions into the database after assinging /// them a batch id and solution id else returns an empty solution /// </summary> /// <param name="sm">the solution manager to be used while running the agent</param> /// <returns>the solutions generated by the agent</returns> public override IEnumerable <ISolution> run(ISolutionManger sm) { // check if the solution is active if (activate(sm)) { //create new solutions and then insert them into the database // after assigning them an id and bacth id return(insertIntoDatabase(createNewSolutions().Select(x => { x.batchid = batchid; x.solutionid = generateId.getId(); return x; }))); } else { return(new List <ISolution>()); } }
/// <summary> /// selects unique solutions from the solution population /// </summary> /// <param name="sm">the solution manager to work with </param> /// <returns>the unique solutions</returns> public override IEnumerable <ISolution> select(ISolutionManger sm) { // A simple distinct solves our problem this uses the .equals and .hascode method // please make sure they are performant return(sm.population.Distinct()); }
/// <summary> /// Finds and Advances every late order by one position /// </summary> /// <param name="selected">the selected solutions</param> /// <param name="sm">the solution manager to work with</param> /// <returns>list of newly created solutions</returns> protected override IEnumerable <ISolution> runWithSolutions(IEnumerable <ISolution> selected, ISolutionManger sm) { var result = new List <ISolution>(); using (var enumerator = selected.GetEnumerator()) { if (enumerator.MoveNext()) { var ps = (ProductionSchedule)enumerator.Current; var proposedOrders = ps.getProposedOrder(); //iterate on each machines current schedule proposedOrders.ForEach(assignedMachine => { int timeTaken = 0; Order prevOrder = null; for (int i = 0; i < assignedMachine.Count; i++) { if (prevOrder != null && assignedMachine[i].productCode != prevOrder.productCode) { timeTaken = timeTaken + ((MillProductionResources)ps.getResources()).milConfig .setup_time; } if (timeTaken + assignedMachine[i].productionCycles > assignedMachine[i].dueDate) { if (i != 0) { timeTaken = timeTaken + assignedMachine[i].productionCycles; prevOrder = assignedMachine[i - 1]; assignedMachine[i - 1] = assignedMachine[i]; assignedMachine[i] = prevOrder; continue; } } timeTaken = timeTaken + assignedMachine[i].productionCycles; prevOrder = assignedMachine[i]; } }); result.Add(ps); } } return(result); }
/// <summary> /// The algorithm used is specified in this paper /// http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.206.4467&rep=rep1&type=pdf ///O => result /// S1 => nonDominatedslns ///For every solution Oi /// (other than first solution ) ///of list O, compare solution Oi from the solutions ///of S1 ///i. If any element of set S1 dominate Oi /// , /// Delete Oi /// from the list ///ii. If Oi ///dominate any solution of the set ///S1, Delete that solution from S1 /// iii. If Oi ///is non dominated to set S1, Then /// update set S1 = S1 U Oi ///iv. If set S1 becomes empty add immediate /// solution at immediate solution to S1 /// </summary> /// <param name="sm">the solution manager to work with</param> /// <returns>A list of Non dominated solutions</returns> public override IEnumerable <ISolution> select(ISolutionManger sm) { IOrderedEnumerable <ISolution> slns; // sort the solution based on the first objective and wethr maximizing or minimizing // that objective is advantageous if (sm.objs[0].isAsc) { slns = sm.population.OrderBy(x => x.objectiveEvaluation[0].evaluationResult); } else { slns = sm.population.OrderByDescending(x => x.objectiveEvaluation[0].evaluationResult); } var nonDominatedslns = new List <ISolution>(); bool isFirstsln = true; // iterate over all solutions foreach (var sln in slns) { //if its the first solution add it to the list of non-dominated solutions // as we have sorted the solutions hence the first solution has to be in the list of non-dominated solutions if (isFirstsln) { isFirstsln = false; nonDominatedslns.Add(sln); continue; } //if the list of non dominated solutions becomes empty at any point // pick the next available solution if (!nonDominatedslns.Any()) { nonDominatedslns.Add(sln); continue; } // this code block checks if any solution in the non-dominated // list of solutions dominates this particular solution if yes // set the flag to true and move to the new solution var continueFlag = false; for (int i = 0; i < nonDominatedslns.Count; i++) { if (checkifSlnDominates(nonDominatedslns[i], sln, sm)) { continueFlag = true; break; } } if (continueFlag) { continue; } // find all the solutions in the non-dominated solution list // which this particular solution dominates and delete them from the list of non-dominated solutions // as we have found a better solution for (int i = nonDominatedslns.Count - 1; i >= 0; i--) { if (checkifSlnDominates(sln, nonDominatedslns[i], sm)) { nonDominatedslns.RemoveAt(i); } } // if this solution does not dominate any non dominated solution list which we have currently found // and no solution in our non -dominated list of solutions dominates this guy // add it to the list else forget it bool flag = false; foreach (var nonDsln in nonDominatedslns) { if (checkifSlnDominates(nonDsln, sln, sm) || checkifSlnDominates(sln, nonDsln, sm)) { flag = true; break; } } if (!flag) { nonDominatedslns.Add(sln); } } return(nonDominatedslns); }
/// <summary> /// Sorts the orders on each machine by their due date. /// </summary> /// <param name="selected">the solution selected</param> /// <param name="sm">the solution manager to work with</param> /// <returns>the newly generated solution after sorting orders </returns> protected override IEnumerable <ISolution> runWithSolutions(IEnumerable <ISolution> selected, ISolutionManger sm) { var result = new List <ISolution>(); using (var enumerator = selected.GetEnumerator()) { if (enumerator.MoveNext()) { var ps = (ProductionSchedule)enumerator.Current; var proposedOrders = ps.getProposedOrder(); proposedOrders.ForEach(assignedMachine => assignedMachine.Sort((x, y) => x.dueDate.CompareTo(y.dueDate))); result.Add(ps); } } return(result); }
/// <summary> /// Selects the solutions on which the agent will run /// </summary> /// <param name="sm">the solution manager which is used to select the solutions</param> /// <returns>selectd solutions</returns> public IEnumerable <ISolution> select(ISolutionManger sm) { return(selector.select(sm)); }
/// <summary> /// method to select solutions /// </summary> /// <param name="sm">the solution manager to work with</param> /// <returns>The selected solutions</returns> public abstract IEnumerable <ISolution> select(ISolutionManger sm);
/// <summary> /// Constructor to create destroyer agents /// </summary> /// <param name="sm">the solution manager to work with</param> /// <param name="act">the activator checks if the agent has to be activated</param> /// <param name="sct">the selector selects solutions on which the agent runs</param> public RemoveDominatedSolutions(ISolutionManger sm, IAgentActivator act, ISelector sct) : base(sm, act, sct) { }
/// <summary> /// Runs the agent using the solution manager /// </summary> /// <param name="sm">the solution manager to be used while running the agent</param> /// <returns>the solutions generated by the agent</returns> public abstract IEnumerable <ISolution> run(ISolutionManger sm);
/// <summary> /// works on the selected solution to produce a new solution /// </summary> /// <param name="selected">the selected solutions</param> /// <param name="sm">the solution manager to work with</param> /// <returns>list of newly created solutions</returns> abstract protected IEnumerable <ISolution> runWithSolutions(IEnumerable <ISolution> selected, ISolutionManger sm);
/// <summary> /// Selects no solutions /// </summary> /// <param name="sm">The solution manager to work with</param> /// <returns>an empty list</returns> public override IEnumerable <ISolution> select(ISolutionManger sm) { return(new List <ISolution>()); }
/// <summary> /// tells you wether the agent has to be activated /// </summary> /// <param name="sm">the solution manager as a required field to run the test</param> /// <returns>returns the result of the test</returns> public bool activate(ISolutionManger sm) { return(activator.activate(sm)); }
/// <summary> /// Constructor to create destroyer agents /// </summary> /// <param name="sm">the solution manager to work with</param> /// <param name="act">the activator checks if the agent has to be activated</param> /// <param name="sct">the selector selects solutions on which the agent runs</param> public UserEnabledDestroyer(ISolutionManger sm, IAgentActivator act, ISelector sct) : base(sm, act, sct) { }
/// <summary> /// Test for activation /// </summary> /// <param name="sm">The solution manager to be used for testing</param> /// <returns>the result of test</returns> public abstract bool activate(ISolutionManger sm);
/// <summary> /// Constructor to create destroyer agents /// </summary> /// <param name="sm">the solution manager to work with</param> /// <param name="act">the activator checks if the agent has to be activated</param> /// <param name="sct">the selector selects solutions on which the agent runs</param> protected DestroyerAgent(ISolutionManger sm, IAgentActivator act, ISelector sct) : base(act, sct) { slnManager = sm; }
/// <summary> /// always true /// </summary> /// <param name="sm">The solution manager to be used for testing</param> /// <returns>the result of test</returns> public override bool activate(ISolutionManger sm) { return(true); }
/// <summary> /// Constructor to create destroyer agents /// </summary> /// <param name="sm">the solution manager to work with</param> /// <param name="act">the activator checks if the agent has to be activated</param> /// <param name="sct">the selector selects solutions on which the agent runs</param> public RemoveDuplicates(ISolutionManger sm, IAgentActivator act, ISelector sct) : base(sm, act, sct) { }
/// <summary> /// returns true only when there is a solution in the population /// </summary> /// <param name="sm">The solution manager to be used for testing</param> /// <returns>the result of test</returns> public override bool activate(ISolutionManger sm) { return(sm.population.Any()); }
/// <summary> /// activates only after a certain number of solutions are presnet in the population /// </summary> /// <param name="sm">The solution manager to be used for testing</param> /// <returns>the result of test</returns> public override bool activate(ISolutionManger sm) { return(sm.population.Count() > minimum); }
/// <summary> /// Picks a random order from any machine and moves it to the end of a randomly selected machine /// (possibly the machine it is currently assigned to).</summary> /// <param name="selected">the solution selected</param> /// <param name="sm">the solution manager to work with</param> /// <returns>the newly generated solution after order change</returns> protected override IEnumerable <ISolution> runWithSolutions(IEnumerable <ISolution> selected, ISolutionManger sm) { var result = new List <ISolution>(); using (var enumerator = selected.GetEnumerator()) { if (enumerator.MoveNext()) { var ps = (ProductionSchedule)enumerator.Current; var millResources = (MillProductionResources)ps.getResources(); var proposedOrderSchedule = ps.getProposedOrder(); var rnadomListNumberGenerator = new SecureRandom(); int randomListNumber = rnadomListNumberGenerator.Next(millResources.milConfig.machines); var randomOrderNumberGenerator = new SecureRandom(); int randomOrderNumber = randomOrderNumberGenerator.Next(proposedOrderSchedule[randomListNumber].Count); if (proposedOrderSchedule[randomListNumber].Count != 0) { var order = proposedOrderSchedule[randomListNumber][randomOrderNumber]; proposedOrderSchedule[randomListNumber].RemoveAt(randomOrderNumber); randomListNumber = rnadomListNumberGenerator.Next(millResources.milConfig.machines); proposedOrderSchedule[randomListNumber].Add(order); } ps.setProposedList(proposedOrderSchedule); result.Add(ps); } } return(result); }