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

namespace EvoBoat.EvoAlg.IntDivision
{
    public class IntDiv_DEAlg<P,S> : DEAlg<P, S, IntVector, int>
        where P : IntDivisionProblem<S>
        where S : VecSolution<IntVector, int>, new()
    {
        public IntDiv_DEAlg(int seed = 0) : base(seed)
        { }

        public IntDiv_DEAlg(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 double Difference(int pos1, int pos2)
        {
            return pos1 - pos2;
        }

        public override int Increase(int pos, double v)
        {
            return pos + (int)Math.Round(v);
        }

        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);
        }
    }
}
