/* Function: solve * * Computes eigenvectors of the standard eigenvalue problem Ax = l x. * * Parameters: * A - Left-hand side matrix. * ev - Array of eigenvectors. * work - Workspace array. * lwork - Size of the input workspace array. * communicator - MPI communicator for selecting the threshold criterion. */ void solve(K* const& A, K**& ev, K* const& work, int& lwork, const MPI_Comm& communicator) { int info; K* tau = work + lwork; underlying_type<K>* d = reinterpret_cast<underlying_type<K>*>(tau + Eigensolver<K>::_n); underlying_type<K>* e = d + Eigensolver<K>::_n; trd("L", &(Eigensolver<K>::_n), A, &(Eigensolver<K>::_n), d, e, tau, work, &lwork, &info); underlying_type<K> vl = -1.0 / HPDDM_EPS; underlying_type<K> vu = Eigensolver<K>::_threshold; int iu = Eigensolver<K>::_nu; int nsplit; underlying_type<K>* evr = e + Eigensolver<K>::_n - 1; int* iblock = new int[5 * Eigensolver<K>::_n]; int* isplit = iblock + Eigensolver<K>::_n; int* iwork = isplit + Eigensolver<K>::_n; char range = Eigensolver<K>::_threshold > 0.0 ? 'V' : 'I'; stebz(&range, "B", &(Eigensolver<K>::_n), &vl, &vu, &i__1, &iu, &(Eigensolver<K>::_tol), d, e, &(Eigensolver<K>::_nu), &nsplit, evr, iblock, isplit, reinterpret_cast<underlying_type<K>*>(work), iwork, &info); if(Eigensolver<K>::_nu) { ev = new K*[Eigensolver<K>::_nu]; *ev = new K[Eigensolver<K>::_n * Eigensolver<K>::_nu]; for(unsigned short i = 1; i < Eigensolver<K>::_nu; ++i) ev[i] = *ev + i * Eigensolver<K>::_n; int* ifailv = new int[Eigensolver<K>::_nu]; stein(&(Eigensolver<K>::_n), d, e, &(Eigensolver<K>::_nu), evr, iblock, isplit, *ev, &(Eigensolver<K>::_n), reinterpret_cast<underlying_type<K>*>(work), iwork, ifailv, &info); delete [] ifailv; mtr("L", "L", "N", &(Eigensolver<K>::_n), &(Eigensolver<K>::_nu), A, &(Eigensolver<K>::_n), tau, *ev, &(Eigensolver<K>::_n), work, &lwork, &info); if(!Wrapper<K>::is_complex) lwork += 3 * Eigensolver<K>::_n - 1; else lwork += 4 * Eigensolver<K>::_n - 1; } delete [] iblock; }
bool Board::undo(BoardTransaction &tr) { MutableBoardTransaction mtr(tr); if (!mtr.isAccepted()) { return false; // transaction was not accepted or already undone } if (mtr.getTransactionNumber() != nextTransaction - 1) { return false; // not the least recently issued transaction } std::vector<BoardOp::Operation*> &ops = mtr.getBoardOps(); bool result = true; size_t i = ops.size(); if (i == 0) { mtr.setAccepted(false); // undo done, invalidate transaction nextTransaction = mtr.getTransactionNumber(); return true; // empty transaction? strange, but undo is done ... } do { i--; // undo -> replay backwards result &= ops[i]->undo(*this); } while (result && i != 0); if (result) { mtr.setAccepted(false); // undo done, invalidate transaction ops.clear(); // especially: clear all ops nextTransaction = mtr.getTransactionNumber(); return true; // all went well, leave } // else: something has gone wrong, undoing undo to restore previous state i++; // last operation failed, increment by one to reach the first undone operation result = true; for (; result && i < ops.size(); i++) { result &= ops[i]->apply(*this); } if (!result) { // undo rollback has failed, too ... something is broken utterly // we don't bother about invalidating the transaction, this is a major // problem which can not be resolved throw std::logic_error("Undo operation failed to recover from failure"); } return false; // rollback went well, don't invalidate transaction }
int main(int argc,char*argv[]) { if(argc<2){ LOG("usage: "<<argv[0]<<" data.csv"); return -1; } if(argc==3) print_only_best=true; Concrete::CData cdata(argv[1]); cdata.init(); Concrete::CData bt_cdata=cdata;//.bootstrap(resize_data,0.05); Tree::FunctionDB* fdb=Tree::std_functions_db(); fdb->add_variables(bt_cdata.x_count()); //for(double i=-10.0;i<10.0;i+=0.5) // fdb->add_constant(i); Tree::Generator*gnrt=new Tree::Generator(fdb,max_depth); Tree::Crossover*crossover=new Tree::Crossover; Tree::Mutator mtr(gnrt,crossover); GpGenerator *dg=new GpGenerator(gnrt); //bin_dna_generator *dg=new GpGeneratorHist(fdb,max_depth); selector *sel_r=new rnd_selector; GpMutator *gp_mtn=new GpMutator(gnrt,crossover,fdb); GpCrossover*c=new GpCrossover(fdb,crossover); //std_ga* sg=new hist_gp(sel_r,gp_mtn,c,dg,10); std_ga* sg=new std_ga(sel_r,gp_mtn,c,dg); GpFitness*ftn=new GpFitness(percent_to_learn,&bt_cdata,fdb); sg->set_params(make_params(mtn_raiting,0.4,psize)); sg->setFitness(ftn); sg->init(); solution sln=sg->getSolution(max_steps,target_value,true,print_only_best); LOG("results: "<<sln.first); ftn->check_solution(sln.second,&cdata); }
bool Board::apply(BoardTransaction &tr) { MutableBoardTransaction mtr(tr); if (mtr.isAccepted()) { return false; // transaction has already been accepted/applied } mtr.setTransactionNumber(nextTransaction); // assign preliminary transaction number if (mtr.getBoardOps().size() == 0) { // empty transaction, apply rules if (!inRange(mtr.getFrom()) || !inRange(mtr.getTo())) { throw std::out_of_range("Source or target coordinates are outside of board range"); } if (mtr.getFrom() == mtr.getTo()) { mtr.setStateCode(bsc_SourceAndDestinationAreEqual); return false; } Figure *fig = board[coordsToIndex(mtr.getFrom())]; if (fig == NULL) { return false; // no figure which could do something is there } if (!fig->apply(mtr)) { mtr.setAccepted(false); mtr.getBoardOps().clear(); return false; // failed to prepare transaction } } std::vector<BoardOp::Operation*> &ops = mtr.getBoardOps(); bool result = true; size_t i = 0; for (; result && i < ops.size(); i++) { result &= ops[i]->apply(*this); } if (result) { mtr.setAccepted(true); nextTransaction++; // Increment for next transaction return true; // transaction has been applied successfully } // else: transaction failed, rollback result = true; do { i--; result &= ops[i]->undo(*this); } while (result && i != 0); if (!result) { // rollback has failed, too ... something is broken utterly // we don't bother about invalidating the transaction, this is a major // problem which can not be resolved throw std::logic_error("Undo operation failed to recover from failure"); } mtr.setAccepted(false); return false; }
int main(int argc,char*argv[]) { if(argc<2){ LOG("usage: "<<argv[0]<<" data.csv"); return -1; } if(argc==3) print_only_best=true; Concrete::CData cdata(argv[1]); cdata.init(); Concrete::CData bt_cdata=cdata.bootstrap(resize_data,0.05); // Создаем список случайных индексов для обучения системы ivector numbers=Concrete::make_learn_indexes(cdata.size(),percent_to_learn); // Этап 1 - натройка с помощью ГА LOG("Этап 1"); fitness *sugeno_ftn=new bin_sugeno(&bt_cdata,numbers); bin_crossover *b_crv=new bin_crossover(true); bin_mutator *b_mtn=new bin_mutator(mtn_bit_count); bin_dna_generator *b_dg=new bin_dna_generator(0.0,1.0,sugeno_ftn->size()); LOG("Длина ДНК: "<<sugeno_ftn->size()); selector* sel_r=new rnd_selector; std_ga* sg=new std_ga(sel_r,b_mtn,b_crv,b_dg); sg->set_params(make_params(0.99,0.5,30)); sg->setFitness(sugeno_ftn); sg->init(); solution sln=sg->getSolution(max_steps_ga,10.0,true); // Этап 2 - натройка с помощью ГП LOG("Этап 2"); fuzzy::MinMax minmax=fuzzy::find_min_max(bt_cdata,numbers); fuzzy::MyuFunctions mf_old=fuzzy::make_myu_functions(bt_cdata,numbers,minmax); fuzzy::MyuFunctions mf={fuzzy::fvector(0),mf_old.y_function}; // Построение функций принадлжености const int x_count=bt_cdata.x_count(); int i=0; for(int k=0;k<bt_cdata.size();++k) for(int x=0;x<x_count;++x){ fuzzy::ExpFunction*ff=new fuzzy::ExpFunction(sln.second->get(i),sln.second->get(i+1)); mf.x_funcs.push_back(ff); i+=2; } fuzzy::rule_vector rules=fuzzy::make_rules(bt_cdata,mf,numbers); LOG("Количество функций для X: "<<mf.x_funcs.size()); // Настраиваем базу функций ГП dvector y(rules.size()); for(int i=0;i<rules.size();++i){ y[i]=rules[i].y(); } Tree::FuzzyFDB *fuzzy_fdb=Tree::fuzzy_function_db(y,cdata.x_count()); Tree::FunctionDB* fdb=fuzzy_fdb->fdb; for(int i=0;i<rules.size();++i){ int j=0; for(fuzzy::fvector::const_iterator pos=rules[i].begin(); pos!=rules[i].end();++pos,j++){ std::string func_name="f_"+boost::lexical_cast<std::string>(i); std::string var_name="x-"+boost::lexical_cast<std::string>(j); NewFuzzyFunction ff(*pos); fdb->add_function(new Tree::VarFuncNode(func_name,var_name,1,ff)); } } // sugeno_out+ // количество_if(количество правил) + // количество акцедентов. max_depth=1+rules.size()+cdata.x_count(); Tree::Generator*gnrt=new Tree::RootGenerator(fdb,max_depth,fuzzy_fdb->root_number,fuzzy_fdb->second_layer); ivector limits; limits<<fuzzy_fdb->root_number<<fuzzy_fdb->second_layer; Tree::Crossover*crossover=new Tree::LimitCrossover(limits); Tree::Mutator mtr(gnrt,crossover); GpGenerator *dg=new GpGenerator(gnrt); GpMutator *gp_mtn=new GpMutator(gnrt,crossover,fdb); GpCrossover*c=new GpCrossover(fdb,crossover); std_ga* gp_sg=new std_ga(sel_r,gp_mtn,c,dg); GpFitness*gp_ftn=new GpFitness(percent_to_learn,&bt_cdata,fdb); gp_ftn->set_numbers(numbers); gp_sg->set_params(make_params(mtn_raiting,0.4,psize)); gp_sg->setFitness(gp_ftn); gp_sg->init(); solution gp_sln=sg->getSolution(max_steps,target_value,true,print_only_best); LOG("results: "<<gp_sln.first); gp_ftn->check_solution(gp_sln.second,&cdata); }