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

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

        public IntDiv_PSOAlg(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 oldv)
        {
            int maxv = Math.Max(oldv, Math.Max(best.Content[d], pbests[index].Content[d]));
            if (sol.Content[d] < 0)
            {
                sol.Content[d] = 0;
                velocities[index][d] = 0;
            }
            else if (sol.Content[d] > maxv)
            {
                sol.Content[d] = maxv;
                velocities[index][d] = 0;
            }
            int change = sol.Content[d] - oldv;
            if (change != 0)
                Problem.RepairAt(this, sol, d, change);
        }        
    }
}
