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

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

        public IntDiv_NWWOAlg(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 MoveP(S sol, int index, int d)
        {
            int dep, oldv = sol.Content[d];
            if (sol.Content[d] > 0)
            {
                dep = Rand.Next(1, sol.Content[d] + 1);
                if (sol.Content[d] > Problem.Total / 2 || Rand.Next() % 2 == 0)
                    dep = -dep;
            }
            else
                dep = Rand.Next(1, (int)Math.Round(Problem.Avg * 2) + 1);
            sol.Content[d] += dep;
            Problem.RepairAt(this, sol, d, sol.Content[d] - oldv);
            return 0;
        }
    }
}
