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

namespace EvoBoat.EvoAlg.WWO
{
    /// <summary>
    /// General framework of simplified water wave optimization
    /// </summary>
    public abstract class SimWWOAlg<P, S, R, e> : VarWWOAlg<P, S, R, e>
        where P : IProblem<S, R>
        where S : VecSolution<R, e>, new()
        where R : IVector<e>, ICloneable<R>
    {
        public SimWWOAlg() : base()
        {
            Name = "SimWWO";
        }

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

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

        public override S Reflect(S sol, int index)
        {
            throw new NotImplementedException();
        }

        public override void Evolve()
        {
            for (int i = 0; i < pop.Length; i++)
            {
                S sol = this.Propagate(pop[i], i);
                if (sol.Fit < pop[i].Fit)
                {
                    pop[i] = sol;
                    if (sol.Fit < best.Fit)
                    {
                        wavelengths[i] /= (best.Fit + epsilon);
                        sol = Break(sol, i, (sol.Fit > 0) ? (int)(KLocal * (best.Fit + epsilon) / (sol.Fit + epsilon)) : Problem.Dimension / 2);
                        //sol = Break(sol, i, KLocal);
                        best = pop[i];
                        iBest = i;
                        wavelengths[i] *= (best.Fit + epsilon);
                    }
                }
            }
            this.UpdateWorst();
            iter++;
            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();
        }
    }
}
