/// <summary> /// Returns the index of the best Solution using a <code>Comparator</code>. /// If there are more than one occurrences, only the index of the first one is returned /// </summary> /// <param name="comparator"><code>Comparator</code> used to compare solutions.</param> /// <returns>The index of the best Solution attending to the comparator or <code>-1<code> if the SolutionSet is empty</returns> public int IndexBest(IComparer <Solution> comparator) { int result = 0; if ((SolutionsList == null) || (SolutionsList.Count == 0)) { result = -1; } else { Solution bestKnown = SolutionsList.ElementAt(0); Solution candidateSolution; int flag; for (int i = 1; i < SolutionsList.Count; i++) { candidateSolution = SolutionsList.ElementAt(i); flag = comparator.Compare(bestKnown, candidateSolution); if (flag == 1) { result = i; bestKnown = candidateSolution; } } } return(result); }
/// <summary> /// Write the encodings.variable values of feasible solutions into a file /// </summary> /// <param name="path">File name</param> public void PrintFeasibleVAR(string path) { try { if (Size() > 0) { int numberOfVariables = SolutionsList.ElementAt(0).Variable.Count(); using (StreamWriter outFile = new StreamWriter(path)) { foreach (Solution s in this.SolutionsList) { if (s.OverallConstraintViolation == 0.0) { for (int i = 0; i < numberOfVariables; i++) { outFile.WriteLine(s.Variable[i].ToString()); } } } } } } catch (Exception ex) { Logger.Log.Error("SolutionSet.PrintFeasibleVAR", ex); Console.Write(ex.StackTrace); } }
/// <summary> /// Writes the decision encodings.variable values of the <code>Solution</code> solutions objects into the set in a file. /// </summary> /// <param name="path">The output file name</param> public void PrintVariablesToFile(string path) { try { if (Size() > 0) { int numberOfVariables = SolutionsList.ElementAt(0).Variable.Count(); using (StreamWriter outFile = new StreamWriter(path)) { foreach (Solution s in this.SolutionsList) { for (int i = 0; i < numberOfVariables; i++) { outFile.Write(s.Variable[i].ToString() + " "); } outFile.Write(Environment.NewLine); } } } } catch (Exception ex) { Logger.Log.Error("SolutionSet.PrintVariablesToFile", ex); Console.WriteLine(ex.StackTrace); } }
public List <string> GetVariable() { string var = ""; List <string> varList = new List <string>(); try { if (Size() > 0) { int numberOfVariables = SolutionsList.ElementAt(0).Variable.Count(); foreach (Solution s in this.SolutionsList) { for (int i = 0; i < numberOfVariables; i++) { var += s.Variable[i].ToString(); } varList.Add(var); var = ""; } } } catch (Exception ex) { Logger.Log.Error("SolutionSet.PrintVariablesToFile", ex); Console.WriteLine(ex.StackTrace); } return(varList); }
/// <summary> /// Returns the worst Solution using a <code>Comparator</code>.Returns the worst Solution using a <code>Comparator</code>. /// If there are more than one occurrences, only the first one is returned /// </summary> /// <param name="comparator"><code>Comparator</code> used to compare solutions.</param> /// <returns>The worst Solution attending to the comparator or <code>null<code>The worst Solution attending to the comparator or <code>null<code> if the SolutionSet is empty</returns> public Solution Worst(IComparer <Solution> comparator) { Solution result; int index = IndexWorst(comparator); if (index < 0) { result = null; } else { result = SolutionsList.ElementAt(index); } return(result); }
/// <summary> /// Returns the best Solution using a <code>Comparator</code>. /// If there are more than one occurrences, only the first one is returned. /// </summary> /// <param name="comparator"><code>Comparator</code> used to compare solutions.<code>Comparator</code> used to compare solutions.</param> /// <returns>The best Solution attending to the comparator or <code>null<code> if the SolutionSet is empty</returns> public Solution Best(IComparer <Solution> comparator) { Solution result; int indexBest = IndexBest(comparator); if (indexBest < 0) { result = null; } else { return(SolutionsList.ElementAt(indexBest)); } return(result); }
public string VariablesToString() { StringBuilder result = new StringBuilder(); if (Size() > 0) { int numberOfVariables = SolutionsList.ElementAt(0).Variable.Count(); foreach (Solution s in this.SolutionsList) { for (int i = 0; i < numberOfVariables; i++) { result.Append(s.Variable[i].ToString() + " "); } result.Append("\r\n"); } } return(result.ToString()); }