|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
using System.Diagnostics;
|
|
|
using System.Linq;
|
|
|
|
|
|
namespace SwarmOps
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class ProblemIndex
|
|
|
{
|
|
|
#region Constructors.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ProblemIndex(Problem[] problems)
|
|
|
: base()
|
|
|
{
|
|
|
Debug.Assert(problems.Length > 0);
|
|
|
|
|
|
int numProblems = problems.Count();
|
|
|
double weight = 1.0 / numProblems;
|
|
|
|
|
|
Index = new List<ProblemFitness>(numProblems);
|
|
|
|
|
|
foreach (Problem problem in problems)
|
|
|
{
|
|
|
Index.Add(new ProblemFitness(problem, weight));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ProblemIndex(WeightedProblem[] weightedProblems)
|
|
|
: base()
|
|
|
{
|
|
|
|
|
|
Debug.Assert(weightedProblems.Length > 0);
|
|
|
|
|
|
Index = new List<ProblemFitness>(weightedProblems.Length);
|
|
|
|
|
|
double weightSum = weightedProblems.Sum(o => o.Weight);
|
|
|
|
|
|
foreach (WeightedProblem weightedProblem in weightedProblems)
|
|
|
{
|
|
|
Problem problem = weightedProblem.Problem;
|
|
|
double weight = weightedProblem.Weight;
|
|
|
double weightNormalized = weight / weightSum;
|
|
|
|
|
|
|
|
|
Debug.Assert(weight > 0);
|
|
|
|
|
|
Index.Add(new ProblemFitness(problem, weightNormalized));
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Internal class.
|
|
|
|
|
|
|
|
|
|
|
|
class ProblemFitness
|
|
|
{
|
|
|
public ProblemFitness(Problem problem, double weight)
|
|
|
{
|
|
|
Problem = problem;
|
|
|
Weight = weight;
|
|
|
Fitness = problem.MaxFitness;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Problem Problem
|
|
|
{
|
|
|
get;
|
|
|
private set;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double Weight
|
|
|
{
|
|
|
get;
|
|
|
private set;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double Fitness
|
|
|
{
|
|
|
get;
|
|
|
set;
|
|
|
}
|
|
|
|
|
|
public static int Compare(ProblemFitness x, ProblemFitness y)
|
|
|
{
|
|
|
Debug.Assert(x != null && y != null);
|
|
|
|
|
|
return System.Math.Sign(y.Fitness - x.Fitness);
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Internal variables.
|
|
|
|
|
|
|
|
|
|
|
|
List<ProblemFitness> Index;
|
|
|
#endregion
|
|
|
|
|
|
#region Public fields.
|
|
|
|
|
|
|
|
|
|
|
|
public int Count
|
|
|
{
|
|
|
get { return Index.Count; }
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region Public methods.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Problem GetProblem(int i)
|
|
|
{
|
|
|
return Index[i].Problem;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public double GetWeight(int i)
|
|
|
{
|
|
|
return Index[i].Weight;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void SetFitness(int i, double fitness)
|
|
|
{
|
|
|
Index[i].Fitness = fitness;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Sort()
|
|
|
{
|
|
|
Index.Sort(ProblemFitness.Compare);
|
|
|
}
|
|
|
#endregion
|
|
|
}
|
|
|
}
|
|
|
|