using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EvoBoat.EvoAlg.WWO
{
    /// <summary>
    /// water wave optimization with reduced size
    /// </summary>
    public abstract class VarWWOAlg<P, S, R, e> : WWOAlg<P, S, R, e>
        where P : IProblem<S, R>
        where S : VecSolution<R, e>, new()
        where R : IVector<e>, ICloneable<R>
    {
        public VarWWOAlg() : base()
        {
            Name = "VarWWO";
        }

        public VarWWOAlg(P problem, int seed = 0) : base(problem, seed)
        {
            Name = "VarWWO";
        }

        public override void SetParameters()
        {
            size = 50;
            SizeMin = 3;
            Alpha = 1.0026;
            HeightMax = 12;
            KLocal = 12;
        }

        public override void InitPop()
        {
            base.InitPop();
        }

        public override void ReduceSize()
        {
            if (pop.Length > SizeMin)
            {
                if (iWorst != pop.Length - 1)
                {
                    pop[iWorst] = pop[pop.Length - 1];
                    wavelengths[iWorst] = wavelengths[pop.Length - 1];
                }
                pop = pop.Take(pop.Length - 1).ToArray();
                this.UpdateWorst();
            }
        }

        public override void Evolve()
        {
            base.Evolve();
            int len = (int)Math.Round(size - ((nfes > 0) ? (double)(size - SizeMin) * nfe / nfes : (double)(size - SizeMin) * iter / iters));
            if (len < SizeMin)
                len = SizeMin;
            if (pop.Length > len)
                this.ReduceSize();
        }
    }
}
