예제 #1
0
Particle *Optimizer::selectParticleUsingMinimumStates(
        const std::vector<Particle *>& results) {
    const vector<Word*>* wordSet = _wordsGenerator->getTrainingAllSet();
    std::vector<std::set<int>> stateCountVec;

    // For each result check how many states it uses.
    for (unsigned int i = 0; i < results.size(); i++) {
        Particle* result = results[i];
        const DFA* dfa = result->getBestDFA();

        std::set<int> s;

        unsigned int wordCount = wordSet->size();
        vector<int> stateVector(wordCount);
        // Pre compute all words. Save results in stateVector
        for(unsigned int i = 0; i < wordCount; i++){
            Word* word = (*wordSet)[i];
            stateVector[i] = dfa ->compute(*word);
            s.insert(dfa->compute(*word));
        }
        stateCountVec.push_back(s);
    }

    int minIndex = 0;
    unsigned int minCount = stateCountVec[minIndex].size();
    for (unsigned int i = 0; i < stateCountVec.size(); i++) {
        if (minCount > stateCountVec[i].size()) {
            minIndex = i;
            minCount = stateCountVec[i].size();
        }
    }

    return results[minIndex];
}
예제 #2
0
void Optimizer::computeRelation() {
    logger::log(Verbose(OPTIMIZER_V), "Computing Relation");

    const vector<Word*>* trainingSet = _wordsGenerator->getTrainingAllSet();
    unsigned int wordCount = trainingSet->size();

    vector<int> stateVector(wordCount);

    // Pre compute all words. Save results in stateVector
    for(unsigned int i = 0; i < wordCount; i++){
        Word* word = (*trainingSet)[i];
        stateVector[i] = tool->compute(*word);
    }

    // For all distinct pairs
    for(unsigned int i = 0; i < wordCount-1; i++){
        for(unsigned int j = i+1; j < wordCount; j++){
            bool inRelation = stateVector[i] == stateVector[j];
            int result = inRelation ? 1:0;
            _toolRelationResults.push_back(result);
        }
    }
}
예제 #3
0
double NLEQInterface::solve(const vector<double>& yin)
{
    if (yin.size() == 0)
    {
        return 0;
    }

    // Set up a dummy Jacobian, actual Jacobian is computed
    // by NLEQ using finite differences
    //    double* Jacobian = new double[1];

    ierr = 0;
    IWK[31 - 1] = maxIterations; // Max iterations

    // Set up default scaling factors
    for (int i = 0; i < n; i++)
    {
        XScal[i] = 1.0;
    }

    for (int i = 0; i < nOpts; i++)
    {
        iopt[i] = 0;
    }

    iopt[31 - 1] = 3; // Set for Highly nonlinear problem

    // Initialise all array elements to 0.0
    for (int i = 0; i < LIWK; i++)
    {
        IWK[i] = 0;
    }

    IWK[31 - 1] = maxIterations; // Max iterations
    for (int i = 0; i < LWRK; i++)
    {
        RWK[i] = 0.0;
    }

    RWK[22 - 1] = 1E-20; // Minimal allowed damping factor

    // For some reason NLEQ modifies the tolerance value, use a local copy instead
    double tmpTol = relativeTolerance;

    // set up the thread local variables, only this thread
    // access them.
    if (*threadModel)
    {
        throw(Exception("thread local storage model is set, this should never occur here."));
    }

    *threadModel = model;

    vector<double> stateVector(n);
    model->getStateVector(&stateVector[0]);

    NLEQ1(  &n,
            &ModelFunction,
            NULL,
            &stateVector[0],
            XScal,
            &tmpTol,
            iopt,
            &ierr,
            &LIWK,
            IWK,
            &LWRK,
            RWK);

    // done, clear it.
    *threadModel = 0;

    if (ierr == 2) // retry
    {
        for (int i = 0; i < nOpts; i++)
        {
            iopt[i] = 0;
        }

        iopt[31 - 1] = 3; // Set for Highly nonlinear problem
        iopt[0] = 1; // Try again but tell NLEQ not to reinitialize
        tmpTol = relativeTolerance;

    }

    if(ierr > 0 )
    {
        if (isWarning(ierr)) {
            Log(Logger::LOG_WARNING) << ErrorForStatus(ierr);
        } else {
            string err = ErrorForStatus(ierr);
            Log(Logger::LOG_ERROR)<<"Error :"<<err;
            throw NLEQException(err);
        }
    }

    return computeSumsOfSquares();
}