Exemple #1
0
/*---------------------------------------------------------------------*/
NOMAD::Cache_File_Point::Cache_File_Point(const NOMAD::Eval_Point &x)
    : _n(x.size()) ,
      _m(0) ,
      _m_def(0) ,
      _coords(NULL) ,
      _bbo_def(NULL) ,
      _bbo_index(NULL)
{
    int i;

    // eval_status:
    switch (x.get_eval_status())
    {
        case NOMAD::EVAL_FAIL:
            _eval_status = 0;
            break;
        case NOMAD::EVAL_OK:
            _eval_status = 1;
            break;
        case NOMAD::EVAL_IN_PROGRESS:
            _eval_status = 2;
            break;
        case NOMAD::UNDEFINED_STATUS:
            _eval_status = 3;
            break;
        case NOMAD::EVAL_USER_REJECT:
            _eval_status = 3;
            break;
    }

    // inputs:
    if (_n > 0)
    {
        _coords = new double [_n];
        for (i = 0 ; i < _n ; ++i)
            _coords[i] = x[i].value();
    }
    else
        _n = 0;

    // outputs:
    const NOMAD::Point &bbo = x.get_bb_outputs();
    _m = bbo.size();
    if (_m > 0)
    {

        std::vector<double> vd;
        std::vector<int>    vi;

        for (i = 0 ; i < _m ; ++i)
            if (bbo[i].is_defined())
            {
                vd.push_back(bbo[i].value());
                vi.push_back(i);
            }

        _m_def = static_cast<int>(vd.size());
        if (_m_def > 0)
        {
            _bbo_def   = new double [_m_def];
            _bbo_index = new int    [_m_def];
            for (i = 0 ; i < _m_def ; ++i)
            {
                _bbo_def  [i] = vd[i];
                _bbo_index[i] = vi[i];
            }
        }
    }
    else
        _m = 0;

#ifdef MEMORY_DEBUG
    ++NOMAD::Cache_File_Point::_cardinality;
    if (NOMAD::Cache_File_Point::_cardinality >
        NOMAD::Cache_File_Point::_max_cardinality)
        ++NOMAD::Cache_File_Point::_max_cardinality;
#endif
}
Exemple #2
0
    //Function + Constraint Evaluation
    bool eval_x(NOMAD::Eval_Point &x, const NOMAD::Double &h_max, bool &count_eval)
    {
        char errstr[1024];
        bool stop = false;
        int i, j, n = x.size(), status;
        double *xm, *fvals;
        mxLogical *sur;
        count_eval = true; //mexErrMsgTxt will kill MEX

        //Check for Ctrl-C
        if (utIsInterruptPending()) {
            utSetInterruptPending(false); /* clear Ctrl-C status */
            mexPrintf("\nCtrl-C Detected. Exiting NOMAD...\n\n");
            count_eval = false;
            raise(SIGINT);
            return false;
        }

        //Blackbox / Objective Evaluation
        fun->plhs[0] = NULL;
        xm = mxGetPr(fun->prhs[fun->xrhs]);
        for(i=0;i<n;i++)
            xm[i] = x[i].value();
        //Add Surrogate if present and requested
        if(hasSur) {
            sur=mxGetLogicals(fun->prhs[fun->xrhs+1]);
			(x.get_eval_type()==NOMAD::SGTE)? *sur=true:*sur=false;
        }
        //Call MATLAB Objective
        try {
            status = mexCallMATLAB(1, fun->plhs, fun->nrhs, fun->prhs, fun->f);   
        }
        //Note if these errors occur it is due to errors in MATLAB code, no way to recover?
        catch(exception &e) {
            sprintf(errstr,"Unrecoverable Error from Objective / Blackbox Callback:\n%sExiting NOMAD...\n\n",e.what());
            mexWarnMsgTxt(errstr);
            //Force exit
            raise(SIGINT);
            return false;
        }  
        catch(...) {            
            mexWarnMsgTxt("Unrecoverable Error from Objective / Blackbox Callback, Exiting NOMAD...\n\n");
            //Force exit
            raise(SIGINT);
            return false;
        }
        if(status) {
            mexWarnMsgTxt("Unrecoverable Error from Objective / Blackbox Callback, Exiting NOMAD...\n\n");
            raise(SIGINT);
            return false;
        }
        
        #ifdef OPTI_VERSION  
            //Check we got the correct number back
            if(mxGetM(fun->plhs[0]) != nobj)
                mexErrMsgTxt("Incorrect number of elements returned from the objective function");
            //Set Objective (Or multi-objective)
            fvals = mxGetPr(fun->plhs[0]);
            for(i=0;i<nobj;i++)
                x.set_bb_output(i,fvals[i]);

            //Constraint Evaluation
            if(ncon) {
                //Add Surrogate if present and requested
                if(hasSur) {
                    sur=mxGetLogicals(con->prhs[con->xrhs+1]);
                    (x.get_eval_type()==NOMAD::SGTE)? *sur=true:*sur=false;
                }
                con->plhs[0] = NULL;
                xm = mxGetPr(con->prhs[con->xrhs]);
                for(i=0;i<n;i++)
                    xm[i] = x[i].value();
                //Call MATLAB Constraint
                try {
                    status = mexCallMATLAB(1, con->plhs, con->nrhs, con->prhs, con->f);
                }
                catch(...)
                {
                    mexWarnMsgTxt("Unrecoverable Error from Constraint Callback, Exiting NOMAD...\n\n");
                    //Force exit
                    raise(SIGINT);
                    return false;
                }
                if(status) {
                    mexWarnMsgTxt("Unrecoverable Error from Constraint Callback, Exiting NOMAD...\n\n");
                    raise(SIGINT);
                    return false;
                }
                //Check we got the correct number back
                if(mxGetM(con->plhs[0]) != ncon)
                    mexErrMsgTxt("Incorrect number of elements returned from nonlinear constraint function");
                //Set Constraints
                double *cons = mxGetPr(con->plhs[0]);            
                for(i=0,j=nobj;i<ncon;i++,j++)
                    x.set_bb_output(j,cons[i] - con->nlrhs[i]); //subtract nlrhs        
                // Clean up LHS Ptr
                mxDestroyArray(con->plhs[0]); 
            }     
        #else //GERAD VERSION
            //Check we got the correct number of elements back
            if(mxGetNumberOfElements(fun->plhs[0]) > nobj+ncon)
                mexWarnMsgTxt("Black box returns more elements than required. Please provide a BB_OUTPUT_TYPE consistent with your black box function");
            else if(mxGetNumberOfElements(fun->plhs[0]) < nobj+ncon) {
                mexErrMsgTxt("Insufficient outputs provided by the black box function. Exiting NOMAD...\n\n");
                raise(SIGINT);
                return false;
            }
            //Assign bb output
            fvals = mxGetPr(fun->plhs[0]);
            for(i=0;i<(nobj+ncon);i++)
                x.set_bb_output(i,fvals[i]);
             
        #endif
                    
        //Iteration Callback
        if(iterF->enabled)
        {
            iterF->plhs[0] = NULL;
            memcpy(mxGetData(iterF->prhs[1]), &citer, sizeof(int));
            memcpy(mxGetPr(iterF->prhs[2]), fvals, sizeof(double));
            memcpy(mxGetPr(iterF->prhs[3]), xm, n * sizeof(double));
            try {
                status = mexCallMATLAB(1, iterF->plhs, 4, iterF->prhs, iterF->f);
            }
            catch (...)
            {
                mexWarnMsgTxt("Unrecoverable Error from Iteration Callback, Exiting NOMAD...\n\n");
                //Force exit
                raise(SIGINT);
                return false;
            }  
            if(status) {
                mexWarnMsgTxt("Unrecoverable Error from Iteration Callback, Exiting NOMAD...\n\n");
                raise(SIGINT);
                return false;
            }
            
            //Collect return argument
            stop = *(bool*)mxGetData(iterF->plhs[0]);
            //Clean up Ptr
            mxDestroyArray(iterF->plhs[0]);
        }              
        
        //Add Function Eval Counter
        citer++;
        
        // Clean up LHS Fun Ptr
        mxDestroyArray(fun->plhs[0]);
        
        //Check for iterfun stop
        if(stop)
        {
            mexPrintf("\nIterFun Called Stop. Exiting NOMAD...\n\n");
            count_eval = false;
            raise(SIGINT);
            return false;
        }
        else        
            return true;
    }    
Exemple #3
0
/*--------------------------------------------*/
bool NOMAD::TGP_Model::predict(NOMAD::Eval_Point &x , bool pred_outside_bnds)
{
    if (!_error_str.empty())
        return false;

    if (!_model_computed)
    {
        _error_str = "NOMAD::TGP_Model::compute() has not been called";
        return false;
    }

    int i , i0 , ix , m = x.get_m() , nx = x.size();

    // reset point outputs:
    x.set_eval_status(NOMAD::EVAL_FAIL);
    for (i = 0 ; i < m ; ++i)
        x.set_bb_output(i , NOMAD::Double());

    // check dimensions:
    if (m != static_cast<int>(_bbot.size()) ||
        (nx != _n0 && nx != _n))
    {
        _error_str = "predict error: bad x dimensions";
        return false;
    }

    double ZZ , * XX = new double[_n];

    // set the coordinates and check the bounds:
    for (i = 0 ; i < _n ; ++i)
    {

        ix = (nx == _n0) ? _fv_index[i] : i;

        if (!pred_outside_bnds)
        {
            i0 = _fv_index[i];
            if (x[ix] < _lb[i0] || x[ix] > _ub[i0])
            {
                delete [] XX;
                return false; // this is not an error
            }
        }

        XX[i] = x[ix].value();
    }

    // predictions (one for each output):
    for (i = 0 ; i < m ; ++i)
        if (_tgp_models[i])
        {
            if (!_tgp_models[i]->predict(XX        ,
                                         _n        ,
                                         ZZ        ,
                                         _tgp_rect))
            {
                std::ostringstream oss;
                oss << "predict error: problem with model #" << i;
                _error_str = oss.str();
                break;
            }
            x.set_bb_output(i , ZZ);
        }

    delete [] XX;

    if (!_error_str.empty())
    {
        x.set_eval_status(NOMAD::EVAL_FAIL);
        return false;
    }

    x.set_eval_status(NOMAD::EVAL_OK);
    return true;
}