using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EvoBoat.EvoAlg.BBO;

namespace EvoBoat.EvoAlg.IntDivision
{
    public class IntDiv_EBOAlg<P,S> : EBOAlg<P, S, IntVector, int>
        where P : IntDivisionProblem<S>
        where S : VecSolution<IntVector, int>, new()
    {
        public IntDiv_EBOAlg() : base()
        { }

        public IntDiv_EBOAlg(P problem, int seed = 0) : base(problem, seed)
        {
            this.Factory = new IntDivisionSolutionFactory<P, S>(problem);
        }

        public override void SetFactory()
        {
            if (Problem == null)
                throw new ArgumentNullException("Please assign the problem instance before set the factory");
            this.Factory = new IntDivisionSolutionFactory<P, S>(Problem);
        }

        public override int Blend(S sol, int d, S sol1, S sol2)
        {
            return sol1.Content[d] + (int)Math.Round(Rand.NextDouble() * (sol2.Content[d] - sol.Content[d]));
        }

        public override void Repair(S sol, int index, int d, int olde)
        {
            if (sol.Content[d] < 0 || sol.Content[d] > Problem.Total)
                sol.Content[d] = Rand.Next(0, (int)Math.Round(2 * Problem.Avg) + 1);
            int change = sol.Content[d] - olde;
            if (change != 0)
                Problem.RepairAt(this, sol, d, change);
        }
    }
}
