예제 #1
0
        /// <summary>
        /// Calculates the increase of fitness when one round is increased/descreased in weights.
        /// </summary>
        /// <param name="fitness"></param>
        /// <param name="round"></param>
        /// <param name="increase"></param>
        /// <param name="problem"></param>
        /// <returns></returns>
        public Fitness Adjust(
            Problem problem,
            Fitness fitness,
            int round,
            double increase)
        {
            // re-calculate times.
            List <double> times = new List <double>(fitness.Times);

            times[round] = times[round] + increase;

            // re-calculate the rest.
            return(this.Calculate(problem, times));
        }
예제 #2
0
 protected bool Equals(Fitness other)
 {
     return(Vehicles == other.Vehicles && SmallestRoundCategory == other.SmallestRoundCategory && LargestRoundCategory == other.LargestRoundCategory && TotalTime.Equals(other.TotalTime) && MinimumTime.Equals(other.MinimumTime) && MaximumTime.Equals(other.MaximumTime) && Equals(LargestRoundCategories, other.LargestRoundCategories) && Equals(SmallestRoundCategories, other.SmallestRoundCategories) && Feasable.Equals(other.Feasable));
 }
예제 #3
0
        /// <summary>
        /// Compares this fitness object to another.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            if (obj is Fitness)
            {
                //Fitness other = (obj as Fitness);
                //int this_weighed_count = this.CategorySum; // (int)((double)this.LargestRoundCategory * 1.2 + (double)this.SmallestRoundCategory);
                //if (this_weighed_count < this.TotalTolerance)
                //{
                //    this_weighed_count = 0;
                //}
                //int other_weighed_count = other.CategorySum; //  (int)((double)other.LargestRoundCategory * 1.2 + (double)other.SmallestRoundCategory);
                //if (other_weighed_count < other.TotalTolerance)
                //{
                //    other_weighed_count = 0;
                //}
                //int equals = this_weighed_count.CompareTo(other_weighed_count);
                //if (equals == 0)
                //{
                //    ////equals = this.SmallestRoundCategory.CompareTo(other.SmallestRoundCategory);
                //    ////if (equals == 0)
                //    ////{
                //    if (this.SmallestRoundCategory == 50 &&
                //        other.SmallestRoundCategory == 50)
                //    {
                //        equals =
                //            this.Vehicles.CompareTo(other.Vehicles);
                //        if (equals == 0)
                //        {
                //            equals =
                //                this.TotalTime.CompareTo(other.TotalTime);
                //        }
                //    }
                //    else
                //    {
                //        equals =
                //            this.TotalTime.CompareTo(other.TotalTime);
                //    }
                //    //}
                //}
                //return -equals;

                //Fitness other = (obj as Fitness);
                //int this_weighed_count = this.CategorySum; // (int)((double)this.LargestRoundCategory * 1.2 + (double)this.SmallestRoundCategory);
                //int other_weighed_count = other.CategorySum; //  (int)((double)other.LargestRoundCategory * 1.2 + (double)other.SmallestRoundCategory);
                //int equals = this_weighed_count.CompareTo(other_weighed_count);
                //if (equals == 0)
                //{
                //    //equals = this.SmallestRoundCategory.CompareTo(other.SmallestRoundCategory);
                //    //if (equals == 0)
                //    //{
                //        equals =
                //            this.Vehicles.CompareTo(other.Vehicles);
                //        if (equals == 0)
                //        {
                //            equals =
                //                this.TotalTime.CompareTo(other.TotalTime);
                //        }
                //    //}
                //}
                //return -equals;

                //Fitness other = (obj as Fitness);
                //int equals = this.LargestRoundCategory.CompareTo(other.LargestRoundCategory);
                //if (equals == 0)
                //{
                //    equals = this.SmallestRoundCategory.CompareTo(other.SmallestRoundCategory);
                //    if (equals == 0)
                //    {
                //        equals =
                //            this.Vehicles.CompareTo(other.Vehicles);
                //        if (equals == 0)
                //        {
                //            equals =
                //                this.TotalTime.CompareTo(other.TotalTime);
                //        }
                //    }
                //}
                //return -equals;
                //Fitness other = (obj as Fitness);
                //int equals =
                //        this.CategorySum.CompareTo(other.CategorySum);
                //if (equals == 0)
                //{
                //    equals =
                //        this.Vehicles.CompareTo(other.Vehicles);
                //    if (equals == 0)
                //    {
                //        equals =
                //            this.TotalTime.CompareTo(other.TotalTime);
                //    }
                //}
                //return -equals;

                Fitness other  = (obj as Fitness);
                int     equals = 0;
                if (this.Feasable && other.Feasable)
                { // if in regime only consider total time.
                    equals = this.TotalTime.CompareTo(other.TotalTime);
                }
                else
                { // compare the category sum.
                    equals = this.CategorySum.CompareTo(other.CategorySum);
                    if (equals == 0)
                    { // compare the vehicles.
                        equals = this.Vehicles.CompareTo(other.Vehicles);
                        if (equals == 0)
                        { // compare the total time.
                            equals = this.TotalTime.CompareTo(other.TotalTime);
                        }
                    }
                }
                return(-equals);
            }
            else if (obj == null)
            {
                return(1);
            }
            throw new ArgumentOutOfRangeException();
        }
예제 #4
0
        /// <summary>
        /// Calculates the rest of the fitness indicators using the times per round.
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="times"></param>
        /// <returns></returns>
        private Fitness Calculate(
            Problem problem,
            List <double> times)
        {
            // TODO: optimize this entire calculation!!!!
            Fitness result = new Fitness();

            result.SmallestRoundCategories = new List <int>();
            result.LargestRoundCategories  = new List <int>();
            result.Vehicles = times.Count;

            // the latest round is taken.
            //int longest_round = 0;
            //int smallest_round = 0;

            double smallest_round_time = double.MaxValue;
            double longest_round_time  = double.MinValue;
            double total_time          = 0;

            // calculate the categories.
            // TODO: save a calculation by calculating this in the contructor.
            double category_size =
                problem.TargetTime.Value / (double)_category_count;

            // find the smallest/largest round and calculate total time.
            for (int round_idx = 0; round_idx < times.Count; round_idx++)
            {
                double round_time = times[round_idx];
                // calculate total time.
                total_time = total_time + round_time;

                // calculate categories
                // calculate the smallest category.
                if (round_time < problem.TargetTime.Value)
                {
                    double smallest_diff =
                        problem.TargetTime.Value - round_time;

                    int temp_smallest_category = (int)System.Math.Floor(smallest_diff / category_size);
                    if (temp_smallest_category < problem.Tolerance)
                    {
                        temp_smallest_category = 0;
                    }
                    result.SmallestRoundCategories
                    .Add(temp_smallest_category);
                }
                else
                {
                    result.SmallestRoundCategories.Add(0);
                }

                // calculate the largest category.
                if (round_time > problem.TargetTime.Value)
                {
                    double largest_diff =
                        round_time - problem.TargetTime.Value;

                    //largest_category = (int)System.Math.Min(System.Math.Floor(largest_diff / category_size),
                    //    _category_count);
                    int temp_largest_category = (int)System.Math.Floor(largest_diff / category_size);
                    if (temp_largest_category < problem.Tolerance)
                    {
                        temp_largest_category = 0;
                    }
                    result.LargestRoundCategories
                    .Add(temp_largest_category);
                }
                else
                {
                    result.LargestRoundCategories.Add(0);
                }

                // find smallest/largest round.
                if (smallest_round_time > round_time)
                {
                    //smallest_round = round_idx;
                    smallest_round_time = round_time;
                }
                if (longest_round_time < round_time)
                {
                    //longest_round = round_idx;
                    longest_round_time = round_time;
                }
            }

            // set longset/shortest time.
            result.MinimumTime = smallest_round_time;
            result.MaximumTime = longest_round_time;

            // calculate the smallest category.
            int smallest_category = 0;

            if (smallest_round_time < problem.TargetTime.Value)
            {
                double smallest_diff =
                    problem.TargetTime.Value - smallest_round_time;

                smallest_category = (int)System.Math.Floor(smallest_diff / category_size);
                if (smallest_category < problem.Tolerance)
                {
                    smallest_category = 0;
                }
            }

            // calculate the largest category.
            int largest_category = 0;

            if (longest_round_time > problem.TargetTime.Value)
            {
                double largest_diff =
                    longest_round_time - problem.TargetTime.Value;

                //largest_category = (int)System.Math.Min(System.Math.Floor(largest_diff / category_size),
                //    _category_count);
                largest_category = (int)System.Math.Floor(largest_diff / category_size);
                if (largest_category < problem.Tolerance)
                {
                    largest_category = 0;
                }
            }

            // create and return the fitness object.
            result.LargestRoundCategory  = largest_category;
            result.SmallestRoundCategory = smallest_category;
            result.TotalTime             = total_time;
            result.Times = times;

            // calculate feasability
            result.Feasable = result.MaximumTime <problem.MaximumTime.Value &&
                                                  result.MinimumTime> problem.MinimumTime.Value;

            return(result);
        }
예제 #5
0
 protected bool Equals(Fitness other)
 {
     return Vehicles == other.Vehicles && SmallestRoundCategory == other.SmallestRoundCategory && LargestRoundCategory == other.LargestRoundCategory && TotalTime.Equals(other.TotalTime) && MinimumTime.Equals(other.MinimumTime) && MaximumTime.Equals(other.MaximumTime) && Equals(LargestRoundCategories, other.LargestRoundCategories) && Equals(SmallestRoundCategories, other.SmallestRoundCategories) && Feasable.Equals(other.Feasable);
 }
예제 #6
0
 protected Individual(bool fitness_calculated, Fitness fitness)
     : base(fitness_calculated, fitness)
 {
 }