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

namespace EvoBoat.EvoAlg.BBO
{
    public abstract class BlendBBOAlg<P, S, R, e> : BBOAlg<P, S, R, e>
        where P : IProblem<S, R>
        where S : VecSolution<R, e>, new()
        where R : IVector<e>, ICloneable<R>
    {
        public BlendBBOAlg() : base()
        {
            Name = "BlendBBO";
        }

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

        public abstract e Blend(S sol1, S sol2, int d);

        public virtual void Repair(S sol, int index, int d, e olde)
        { }

        public override S Migrate(S sol, int index)
        {
            S sol1 = new S() { Content = sol.Content.Clone(), Fit = sol.Fit };
            bool bChanged = false;
            for (int d = 0, sel; d < Problem.Dimension; d++)
            {
                if (Rand.NextDouble() < immrs[index])
                {
                    e olde = sol1.Content[d];
                    sel = RouletteSelect(pop, index, emrs);
                    sol1.Content[d] = Blend(sol, pop[sel], d);
                    Repair(sol1, index, d, olde);
                    if (!bChanged) bChanged = true;
                }
            }
            if (bChanged)
                this.Evaluate(sol1);
            return sol1;
        }
    }
}
