Beispiel #1
0
        public void Eval(TOOTargetFunction Function)
        {
            double value = Function(_point);

            DoubleTypeValue typeValue;

            typeValue = CheckDouble.GetTypeValue(value);

            if (typeValue != DoubleTypeValue.Valid)
            {
                throw new InvalidValueFunctionException($"An invalid value ({typeValue.ToString()}) of the function at point.", _point);
            }

            _objs[0] = value;
        }
        protected void CalculateDistances(Func <Agent, Agent, double> Distance)
        {
            _denumForProbKahanSum.SumResest();

            // Calculate distance between all points.
            for (int i = 0; i < _matrixOfDistances.RowCount; i++)
            {
                for (int j = i + 1; j < _matrixOfDistances.ColumnCount; j++)
                {
                    _matrixOfDistances[i, j] = Distance(_weightedAgents[i].Agent, _weightedAgents[j].Agent);
                }
            }

            for (int ii = 0; ii < _matrixOfDistances.RowCount; ii++)
            {
                _distKahanSum.SumResest();

                for (int j = 0; j < _matrixOfDistances.ColumnCount; j++)
                {
                    _distKahanSum.Add(_matrixOfDistances[ii, j]);
                }

                _weightedAgents[ii].Weight = _distKahanSum.Sum;

                _denumForProbKahanSum.Add(_distKahanSum.Sum);
            }

            // Probability of explosion.
            for (int jj = 0; jj < _weightedAgents.Count; jj++)
            {
                _weightedAgents[jj].Weight /= _denumForProbKahanSum.Sum;

                if (CheckDouble.GetTypeValue(_weightedAgents[jj].Weight) != DoubleTypeValue.Valid)
                {
                    for (int k = 0; k < _weightedAgents.Count; k++)
                    {
                        _weightedAgents[jj].Weight = 1.0 / _matrixOfDistances.ColumnCount;
                    }

                    break;
                }
            }
        }
Beispiel #3
0
        public void Eval(TMOTargetFunction Function)
        {
            int position = 0;

            DoubleTypeValue typeValue;

            foreach (double value in Function(_point))
            {
                typeValue = CheckDouble.GetTypeValue(value);

                if (typeValue != DoubleTypeValue.Valid)
                {
                    throw new InvalidValueFunctionException($"An invalid value ({typeValue.ToString()}) of the {position}th function at point", _point);
                }

                _objs[position] = value;

                position++;
            }
        }
        private void GenerateShrapnelesForGrenade(int WhichGrenade, int Dimension)
        {
            double p = Math.Max(1.0 / Dimension,
                                Math.Log10(_radiusGrenade / _radiusExplosion) / Math.Log10(_parameters.Pts));

            if (CheckDouble.GetTypeValue(p) != DoubleTypeValue.Valid)
            {
                p = 0.5;
            }

            double randomValue1, randomValue2;

            bool isShrapnelAdd = true;

            PointND tempPoint = null;

            // Generating shrapnels.
            for (int i = 0; i < _parameters.NShrapnel; i++)
            {
                // Reuse an allocated memory, if 'tempPoint' was not added to the array, otherwise
                // allocate a new memory.
                if (isShrapnelAdd)
                {
                    tempPoint = new PointND(0.0, Dimension);
                }

                randomValue1 = _uniformRand.URandVal(0, 1);

                // Random search direction.
                for (int w = 0; w < _drnd.Count; w++)
                {
                    _drnd[w] = _normalRand.NRandVal(0, 1);
                }

                _drnd.MultiplyByInplace(1 / _drnd.Norm());

                // If exist OSD.
                if (_dosd != null)
                {
                    randomValue2 = _uniformRand.URandVal(0, 1);

                    for (int coordIndex = 0; coordIndex < _dgrs.Count; coordIndex++)
                    {
                        _dgrs[coordIndex] = _mosd * randomValue1 * _dosd[coordIndex] + (1 - _mosd) * randomValue2 * _drnd[coordIndex];
                    }

                    _dgrs.MultiplyByInplace(1 / _dgrs.Norm());

                    randomValue1 = Math.Pow(randomValue1, p) * _radiusExplosion;

                    for (int coordIndex = 0; coordIndex < _dgrs.Count; coordIndex++)
                    {
                        tempPoint[coordIndex] = _grenades[WhichGrenade].Point[coordIndex] + randomValue1 * _dgrs[coordIndex];
                    }
                }
                else
                {
                    randomValue1 = Math.Pow(randomValue1, p) * _radiusExplosion;

                    for (int coordIndex = 0; coordIndex < _dgrs.Count; coordIndex++)
                    {
                        tempPoint[coordIndex] = _grenades[WhichGrenade].Point[coordIndex] + randomValue1 * _drnd[coordIndex];
                    }
                }

                double randNum = 0.0, largeComp = 0.0;

                // Out of range [-1; 1]^n.
                for (int j = 0; j < Dimension; j++)
                {
                    if (tempPoint[j] < -1 || tempPoint[j] > 1)
                    {
                        randNum = _uniformRand.URandVal(0, 1);

                        largeComp = tempPoint.Max(num => Math.Abs(num));

                        double normPos = 0.0;

                        for (int coordIndex = 0; coordIndex < tempPoint.Count; coordIndex++)
                        {
                            normPos = tempPoint[coordIndex] / largeComp;

                            tempPoint[coordIndex] = randNum * (normPos - _grenades[WhichGrenade].Point[coordIndex]) + _grenades[WhichGrenade].Point[coordIndex];
                        }

                        break;
                    }
                }

                double dist = 0.0;

                // Calculate distance to grenades.
                for (int idxGren = 0; idxGren < _parameters.NGrenade; idxGren++)
                {
                    if (idxGren != WhichGrenade)
                    {
                        dist = PointND.Distance(tempPoint, _grenades[idxGren].Point);

                        // Shrapnel does not accept (it is too near to other grenades).
                        if (dist <= _radiusGrenade)
                        {
                            isShrapnelAdd = false;
                            break;
                        }
                    }
                }

                if (isShrapnelAdd)
                {
                    _shrapnels[WhichGrenade].AddLast(new Agent(tempPoint, new PointND(0.0, 1)));
                }
            }
        }