Esempio n. 1
0
DifferentialEvolution::DifferentialEvolution()
    : PopulationBasedOptimisationAlgorithm() {
    setInitialisingFunctions({{
            [this](
                const arma::uword numberOfDimensions_,
                const arma::mat& initialParameters_) {
                population_ = initialParameters_;

                return initialParameters_;
            },
            "Population initialisation"
        },
        {   [this](
                const arma::uword numberOfDimensions_,
                const arma::mat& initialParameters_) {
                localBestObjectiveValues_.set_size(populationSize_);
                localBestObjectiveValues_.fill(std::numeric_limits<double>::infinity());

                return initialParameters_;
            },
            "Local best objective values initialisation"
        }
    });

    setNextParametersFunctions({{
            [this](
                const arma::uword numberOfDimensions_,
                const arma::mat& parameters_,
                const arma::rowvec& objectiveValues_,
                const arma::rowvec& differences_) {
                for (arma::uword n = 0; n < populationSize_; ++n) {
                    if (objectiveValues_(n) < localBestObjectiveValues_(n)) {
                        population_.col(n) = parameters_.col(n);
                    }
                }

                arma::mat populationCandidates(arma::size(population_));
                for (arma::uword n = 0; n < populationSize_; ++n) {
                    arma::uvec randomIndices = randomPermutationVector(populationSize_, 3);
                    populationCandidates.col(n) = population_.col(randomIndices(0)) + scalingFactor_ * (population_.col(randomIndices(1)) - population_.col(randomIndices(2)));
                }

                return populationCandidates;
            },
            "Differential evolution"
        }
    });

    setScalingFactor(0.5);
}
Esempio n. 2
0
// -------------------------------------------------------------------------
void MultiMDDAGLearner::parallelRollout(const nor_utils::Args& args, InputData* pData, const string fname, int rsize, GenericClassificationBasedPolicy* policy, PolicyResult* result, const int weakLearnerPostion)
{
    vector<AlphaReal> policyError(_shypIter);
    vector<InputData*> rollouts(_shypIter,NULL);

    // generate rollout
    if (_randomNPercent>0)
    {
        vector<int> randomIndices(_shypIter);
        for( int si = 0; si < _shypIter; ++si ) randomIndices[si]=si;
        random_shuffle(randomIndices.begin(), randomIndices.end());

        int ig = static_cast<int>(static_cast<float>(_shypIter * _randomNPercent) / 100.0);
        for( int si = 0; si < ig; ++si )
        {
            stringstream ss(fname);
            //			if (si>0)
            //			{
            //				ss << fname << "_" << si;
            //			} else {
            //				ss << fname;
            //			}


            MDDAGLearner::parallelRollout(args, pData, ss.str(), rsize, policy, result, randomIndices[si]);
            InputData* rolloutTrainingData = getRolloutData( args, ss.str() );

            if (_verbose)
                cout << "---> Rollout size("<< randomIndices[si] << ")" << rolloutTrainingData->getNumExamples() << endl;

            rollouts[randomIndices[si]] = rolloutTrainingData;
        }

    } else {
        for( int si = 0; si < _shypIter; ++si )
        {
            stringstream ss(fname);
            //			if (si>0)
            //			{
            //				ss << fname << "_" << si;
            //			} else {
            //				ss << fname;
            //			}


            MDDAGLearner::parallelRollout(args, pData, ss.str(), rsize, policy, result, si);
            InputData* rolloutTrainingData = getRolloutData( args, ss.str() );

            if (_verbose)
                cout << "---> Rollout size("<< si << ")" << rolloutTrainingData->getNumExamples() << endl;

            rollouts[si] = rolloutTrainingData;
        }
    }
    // update policy
    int numOfUpdatedPolicy = 0;
    for( int si = 0; si < _shypIter; ++si )
    {
        if ((rollouts[si]==NULL) || (rollouts[si]->getNumExamples()<=2)) continue;
        policyError[si] = _policy->trainpolicy( rollouts[si], _baseLearnerName, _trainingIter, si );

        if (_verbose)
            cout << "--> Policy error: pos: " << si << "\t error:\t" << setprecision (4) << policyError[si] << endl;

        numOfUpdatedPolicy++;
    }

    if (_verbose)
        cout << "--> Number of updated policy" << numOfUpdatedPolicy << endl << flush;

    //release rolouts
    for( int si = 0; si < _shypIter; ++si )
    {
        if (rollouts[si]) delete rollouts[si];
    }
}