_AgentType_ * Fabric<_AgentType_>:: newAgent() { // DEBUG : Avoid SIGSEV with unallocated fabric if( !storage_ ) throw myException( "Unallocate fabric", __FILE__, __LINE__ ) ; // Look for a dead agent from the last to the top for( unsigned i=highestIndexAgent; i<size_; ++i ) { if( ! storage_[i].isAlive() ) { highestIndexAgent = i ; return storage_+i ; } } // Look for a dead agent from the bottom to the last for( unsigned i=0; i<highestIndexAgent; ++i ) { if( ! storage_[i].isAlive() ) return storage_+i ; } // All agents are alive: out of memory throw myException( "Fabric is out of memory", __FILE__, __LINE__ ) ; //return NULL ; }
//************************************************************************************* void SinglyLinkedList::insertAtGivenPosition(int data, int position) { if(m_debug == true) { cout<<"In function insertAtGivenPosition with data "<< data; cout<<" and position "<<position<<endl; } if(m_length < position) { throw myException(m_error = "List is not that long"); } Node* newNode = new Node(data); if(!newNode) { throw myException(m_error = "No memory left for new node"); } Node* temp = m_head; for(int index = 1; index < (position - 1 ); index++) { temp = temp->link; } newNode->link = temp->link; temp->link = newNode; m_length++; }
int main(int argc, char **argv) { std::fstream fs; std::string line; Vm vm; int i; int set; i = 0; set = 0; std::cout << "*******************ABSTRACT VM START********************" << std::endl; try { if (argc >= 2) { fs.open(argv[1], std::ios::in); if (!fs) throw myException("File not found"); while (line.compare("exit") != 0 && !fs.eof()) { i = i + 1; getline(fs, line); if (vm.addline(line, i) == 1) return (0); } fs.close(); if (line.compare("exit") != 0) throw myException("no exit instruction"); } else if (argc < 2) { while (line.compare(";;") != 0) { getline(std::cin, line); if (line.compare("exit") == 0) { set = 1; continue; } if(set == 0) { i = i + 1; if (vm.addline(line, i) == 1) return (0); } } if (set == 0) throw myException("no exit instruction"); } vm.run(); } catch(const myException &e) { std::cerr << "Error :" << e.what() << std::endl; } return (0); }
ReturnFlag CRDEPopulation::evolve() { if(this->m_popsize<4){ throw myException("the population size cannot be smaller than 5@DEPopulation<TypeDEIndi>::evolve()"); } ReturnFlag rf=Return_Normal; for(int i=0;i<this->m_popsize;i++){ mutate(i); this->m_pop[i]->recombine(m_CR); rf=this->m_pop[i]->m_pu.evaluate(); if(rf!=Return_Normal) return rf; int idx=this->findNearest(i,0,0,1); if(this->m_pop[i]->m_pu>this->m_pop[idx]->self()){ this->m_pop[idx]->self()=this->m_pop[i]->m_pu; } this->updateBestArchive(DEIndividual(this->m_pop[i]->m_pu)); } this->m_iter++; return rf; }
void SinglyLinkedList::reverseListInPair() { if(m_debug == true) { cout<<"In function reverseListInPair"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* tempNode = m_head; while(tempNode != NULL) { if(tempNode->link != NULL) { swap(&(tempNode->data), &((tempNode->link)->data)); } else { return; } tempNode = tempNode->link; if(tempNode->link == NULL) { return; } tempNode = tempNode->link; } }
//************************************************************************************* void SinglyLinkedList::insertAtBeginning(int data) { Node* newNode = new Node(data); if(!newNode) { throw myException(m_error = "No memory for new node"); } if(m_debug == true) { cout<<"In function insertAtBeginning with data "<< data<<endl; cout<<"Node's data - " << newNode->data <<endl; } if(m_length == 0) // list is empty { m_head = newNode; } else // list has some nodes { newNode->link = m_head; m_head = newNode; } m_length++; }
//************************************************************************************* void SinglyLinkedList::insertNodeInSortList(int data) { if(m_debug == true) { cout<<"In function insertNodeInSortList"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* slowPtr = m_head; Node* fastPtr = m_head; fastPtr = fastPtr->link; while(fastPtr != NULL) { if(fastPtr->data < data) { fastPtr = fastPtr->link; slowPtr = slowPtr->link; } else { break; } } Node* tempNode = new Node(data); tempNode->link = slowPtr->link; slowPtr->link = tempNode; }
//************************************************************************************* void SinglyLinkedList::createCircle() { if(m_debug == true) { cout<<"In function createCircle"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* temp = m_head; Node* randomNode; int random = m_length/2; //random int index = 0; while(temp->link != NULL) { if(random == index) { randomNode = temp; } temp = temp->link; index++; } temp->link = randomNode; m_isCircleCreated = true; }
//************************************************************************************* int SinglyLinkedList::middleNode() { if(m_debug == true) { cout<<"In function middleNode"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* slowPtr = m_head; Node* fastPtr = m_head; while(fastPtr->link != NULL) { slowPtr = slowPtr->link; fastPtr = fastPtr->link; if(fastPtr->link == NULL) { break; } fastPtr = fastPtr->link; } return slowPtr->data; }
//************************************************************************************* void SinglyLinkedList::printCircle() // circle { if(m_debug == true) { cout<<"In function isListEmpty"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* tempNode = m_head; int index = 0; while(tempNode != NULL) { cout<<"|"; cout<< tempNode->data; cout<<"|"; if(tempNode->link != NULL) { cout<<" --> "; } index++; tempNode = tempNode->link; if(index > MAXLOOPINGLIMIT) { break; } } cout<<endl; }
//************************************************************************************* void SinglyLinkedList::printList() { if(m_debug == true) { cout<<"In function isListEmpty"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } if(isCircleExist()) { printCircle(); return; } Node* temp = m_head; int index = 1; cout<<endl; while(temp != NULL) { cout<<"|"; cout<< temp->data; cout<<"|"; if(temp->link != NULL) { cout<<" --> "; } index++; temp = temp->link; } cout<<endl; }
//************************************************************************************* int SinglyLinkedList::deletingAtGivenPosition(int position) { if(m_debug == true) { cout<<"In function deletingAtGivenPosition, with position "<<position<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } if(m_length < position) { //list is not that big } Node* atPositionPtr = m_head; Node* oneBeforeAtPositionPtr = m_head; atPositionPtr = atPositionPtr->link; for(int index = 1 ; index < (position - 1); index++) { oneBeforeAtPositionPtr = oneBeforeAtPositionPtr->link; atPositionPtr = atPositionPtr->link; } oneBeforeAtPositionPtr->link = atPositionPtr->link; int returnValue = atPositionPtr->data; delete atPositionPtr; return returnValue; }
//************************************************************************************* int SinglyLinkedList::deletingAtend() { if(m_debug == true) { cout<<"In function deletingAtend"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* lastNode = m_head; Node* secondLastNode = m_head; //increment last node before going to loop lastNode = lastNode->link; while(lastNode->link != NULL) { lastNode = lastNode->link; secondLastNode = secondLastNode->link; } int returnValue = lastNode->data; delete lastNode; secondLastNode->link = NULL; return returnValue; }
//************************************************************************************* OddEven SinglyLinkedList::lengthOfTheListInOddEven() { if(m_debug == true) { cout<<"In function lengthOfTheListInOddEven"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } Node* tempNode = m_head; while(tempNode != NULL) { if(tempNode->link == NULL) { break; } tempNode = tempNode->link; tempNode = tempNode->link; } if(tempNode == NULL) { return EVEN; } else { return ODD; } }
double ContinuousProblem::getDistance(const VirtualEncoding &ss1, const VirtualEncoding &ss2, DistanceMode mode){ const CodeVReal &s1=dynamic_cast<const CodeVReal&>(ss1); const CodeVReal &s2=dynamic_cast<const CodeVReal&>(ss2); double dis=0; switch(mode){ case DIS_EUCLIDEAN: { for(int i=0;i<m_numDim;i++){ dis+=(double)((s1[i]-s2[i])*(s1[i]-s2[i])); } return sqrt(dis); } case DIS_MANHATTAN: { for(int i=0;i<m_numDim;i++){ dis+=fabs((double)(s1[i]-s2[i])); } return dis; } case DIS_HAMMING: { for(int i=0;i<m_numDim;i++){ if(s1[i]!=s2[i]) dis+=1; } return dis; } default: throw myException("undefined distance mode @Problem::getDistance"); } }
//************************************************************************************* void SinglyLinkedList::insertAtend(int data) { if(m_debug == true) { cout<<"In function insertAtend with data "<< data<<endl; } Node* newNode = new Node(data); if(!newNode) { throw myException(m_error = "No memory for new node"); } if(m_length == 0) // list is empty { m_head = newNode; } else { Node* temp = m_head; while(temp->link != NULL) { temp = temp->link; } temp->link = newNode; } m_length++; }
void Fabric<_AgentType_>::defragmentFabric() { // DEBUG : Avoid SIGSEV with unallocated fabric if( !storage_ ) throw myException( "Unallocate fabric", __FILE__, __LINE__ ) ; if (firstRemoved == true) { // Look for a dead agent within the block of alive agents. unsigned i=0; //firstDead; while (i < highestIndexAgent) { //cout << i << " "; if( !storage_[i].isAlive() ) { while (!(storage_[highestIndexAgent].isAlive()) && highestIndexAgent > 0) { highestIndexAgent--; } if ( storage_[highestIndexAgent].isAlive() && i < highestIndexAgent) { // Use overloaded assignment operator to assign object. storage_[i] = storage_[highestIndexAgent]; storage_[highestIndexAgent].setStock(0.0); highestIndexAgent--; } } i++; } } }
void FWeierstrass::initialize(){ setOriginalGlobalOpt(); if(IS_PROBLEM_NAME(m_id,"FUN_Weierstrass")){ }else if(IS_PROBLEM_NAME(m_id,"FUN_R_Weierstrass")){ setConditionNumber(2); loadRotation(); }else if(IS_PROBLEM_NAME(m_id,"FUN_RS_Weierstrass")){ setConditionNumber(5); loadTranslation(); loadRotation(); }else if(IS_PROBLEM_NAME(m_id,"FUN_RS_Weierstrass_CEC05")){ setConditionNumber(5); loadTranslation(); loadRotation(); setBias(90); }else { throw myException("Error: please check the problem ID@FWeierstrass::initialize"); } setGlobalOpt(); setAccuracy(1.0e-2); }
int main(int ac, char **av) { void *handle; IGui *(*ext_ctor)(); Nibbler nibbler; try { if (ac != 4) throw myException((GamePart)42, "Usage: ./nibbler width height libXXX.so"); handle = dlopen(av[3], RTLD_LAZY); if (!handle) throw Nibbler::nibblerException(NIBBLER, dlerror(), DLERROR); ext_ctor = reinterpret_cast<IGui* (*)()>(dlsym(handle, "create")); if (!ext_ctor) throw Nibbler::nibblerException(NIBBLER, dlerror(), DLERROR); if (!nibbler.checkArg(av[1]) || !nibbler.checkArg(av[2])) throw Nibbler::nibblerException(NIBBLER, "Bad map values", BADARG); nibbler.setGui(ext_ctor); nibbler.execNibbler(nibbler.getnbr(av[1]), nibbler.getnbr(av[2])); nibbler.deleteGui(); nibbler.getCore()->printEndgame(); if (dlclose(handle) != 0) throw Nibbler::nibblerException(NIBBLER, dlerror(), DLERROR); } catch (myException const &ex) { nibbler.deleteGui(); std::cerr << ex.where() << " : " << ex.what() << std::endl; return (1); } return (0); }
void StartInstance::Launch() { isInit = true; VirtualMachine *vm = new VirtualMachine(); if (isInit) { Power *p = new Power(); std::cout << "V1" << std::endl; map->seeMap(); std::cout << "-----------" << std::endl; map = p->findPowerOnMap(map); std::cout << "V2" << std::endl; map->seeMap(); std::cout << "-----------" << std::endl; vm->setMap(map->getMap()); vm->Launch(*map->getMap()); } else { std::string error = "Error Initialisation"; throw myException(error); } }
bool DynamicProblem::setPeriod(const int rPeriod){ if(rPeriod>=0) m_period=rPeriod; else{ throw myException("period must be positive@ DynamicProblem::setPeriod"); } return true; }
char BooleanByteWrapper::setFlag(byte flag, char offset, bool value) { if (offset >= 8) throw myException("offset must be lesser than 8"); return (value ? (char)(flag | (1 << offset)) : (char)((flag & 255) - (1 << offset))); }
bool BooleanByteWrapper::getFlag(char flag, char offset) { if (offset >= 8) throw myException("offset must be lesser than 8"); return (flag & (char)(1 << offset)) != 0; }
void CompositionDBG::getComBoundary(const ProblemTag &f,double &l,double &u,const int rDimIdx)const{ switch(f){ case Sphere: l=mv_comBoundary[0][rDimIdx].m_lower; u=mv_comBoundary[0][rDimIdx].m_upper; break; case Rastrigin: l=mv_comBoundary[1][rDimIdx].m_lower; u=mv_comBoundary[1][rDimIdx].m_upper; break; case Weierstrass: l=mv_comBoundary[2][rDimIdx].m_lower; u=mv_comBoundary[2][rDimIdx].m_upper; break; case Griewank: l=mv_comBoundary[3][rDimIdx].m_lower; u=mv_comBoundary[3][rDimIdx].m_upper; break; case Ackley: l=mv_comBoundary[4][rDimIdx].m_lower; u=mv_comBoundary[4][rDimIdx].m_upper; break; default: throw myException("No the function in the basic component fs@CompositionDBG::getComBoundary"); break; } }
void FAckley::initialize(){ setOriginalGlobalOpt(); if(IS_PROBLEM_NAME(m_id,"FUN_Ackley")||IS_PROBLEM_NAME(m_id,"FUN_Ackley_Noisy")){ } else if(IS_PROBLEM_NAME(m_id,"FUN_S_Ackley")){ loadTranslation(); }else if(IS_PROBLEM_NAME(m_id,"FUN_R_Ackley")){ setConditionNumber(2); loadRotation(); }else if(IS_PROBLEM_NAME(m_id,"FUN_RS_Ackley")){ setConditionNumber(100); loadTranslation(); loadRotation(); }else if(IS_PROBLEM_NAME(m_id,"FUN_RS_Ackley_Bound_CEC05")){ setConditionNumber(100); loadTranslation(); loadRotation(); setBias(-140); }else if(IS_PROBLEM_NAME(m_id,"FUN_S_Ackley_CEC08")){ setBias(-140); loadTranslation(); }else{ throw myException("Error: please check the problem ID@FAckley::initialize()"); } setGlobalOpt(); }
void ContinuousProblem::initializePartSolution(VirtualEncoding &result,int begin,int end){ if(begin<0||end>m_numDim) throw myException("ContinuousProblem::initializeSolution@ContinuousProblem::initializePartSolution"); CodeVReal&s=dynamic_cast< CodeVReal&>(result); double l,u; for(int i=begin;i<end;i++){ m_searchRange.getSearchRange(l,u,i); s[i]= l+(u-l)*Global::msp_global->mp_uniformAlg->Next(); } }
/* My if len is odd go till middle of the list. else go till len/2 put second half of the list into stack pop out stack element and compare with the first half of the list. */ bool SinglyLinkedList::isListPalindrome() { if(m_debug == true) { cout<<"In function getData"<<endl; } if(isListEmpty()) { throw myException(m_error = "Empty list"); } if(m_length == 1) { return true; } Node* fastPtr = m_head; Node* slowPtr = m_head; if(m_length%2 == 0) { int temp = m_length/2; for(int index = 1; index <= temp; index++) { slowPtr = slowPtr->link; } } else { while(fastPtr->link != NULL) { fastPtr = fastPtr->link; if(fastPtr->link == NULL) { break; } fastPtr = fastPtr->link; slowPtr = slowPtr->link; } } //put second half of the list into stack stack<int> tempStack; while(slowPtr != NULL) { tempStack.push(slowPtr->data); slowPtr = slowPtr->link; } //compare data Node* tempNode = m_head; for(int index = 1; index <= tempStack.size(); index++) { if(tempNode->data != tempStack.pop()) { return false; } tempNode = tempNode->link; } return true; }
DTLZ::DTLZ(int ID, int numDim, const string &proName, int numObj) :BenchmarkFunction(ID, numDim, proName, numObj) { if (m_numObj > m_numDim) throw myException("the number of dim must be greater or eaqual to the number of obj for DTLZ pros"); setSearchRange(0.,1.); set<ProTag> p_tag = { MOP, CONT }; setProTag(p_tag); setOptType(MIN_OPT,-1); m_popInitialMode=POP_INIT_UNIFORM; generateAdLoadPF(); }
_CellType_& World<_CellType_>::operator() ( int iPos, int jPos ) const { /// DEBUG : Avoid out of range if( iPos >= engParamsPtr_->height_ ) { cout << "iPos = " << iPos << ", jPos = " << jPos<< endl; throw myException( "iPos out of range (movingGrids)", __FILE__, __LINE__ ) ; } else if (jPos >= engParamsPtr_->width_) { cout << "iPos = " << iPos << ", jPos = " << jPos<< endl; throw myException( "jPos out of range (movingGrids)", __FILE__, __LINE__ ) ; } return cells_[ iPos * engParamsPtr_->width_ + jPos ] ; }
void DTLZ::generateAdLoadPF() { const string problem_name[]= {"FUN_MOP_DTLZ1", "FUN_MOP_DTLZ2", "FUN_MOP_DTLZ3", "FUN_MOP_DTLZ4"}; stringstream os; os<<Global::g_arg[param_workingDir]<<"Problem/FunctionOpt/Data/PF_"<<Global::g_arg[param_proName]<<"("<<Global::g_arg[param_numObj]<<")"<<"_Opt.txt"; for (int i=0; i<4; i+=1) // problem { if(Global::g_arg[param_proName]!=problem_name[i]) continue; const int M[5] = {3, 5, 8, 10, 15}; for (int j=0; j<5; j+=1) // objectives { if(Global::g_arg[param_numObj]!=M[j]) continue; ifstream infile(os.str()); if(infile) { infile.close(); break; } ofstream ofile(os.str()); if (M[j] <= 5) // #objectives <= 5 { int p[2] = {12, 6}; // Check Section V, Table I in the original paper GeneratePF_OneLayer(ofile, problem_name[i], M[j], p[j]); } else { int p[3][2] = {{3, 2}, {3, 2}, {2, 1}}; // Check Section V, Table I in the original paper GeneratePF_TwoLayers(ofile, problem_name[i], M[j], p[j-2][0], p[j-2][1]); } ofile.close(); } } int numObj=Global::g_arg[param_numObj]; ifstream infile(os.str()); if(!infile) throw myException("please set your own pareto front @DTLZ::generatePF()"); string str; int line=0; while(getline(infile,str)) ++line; m_globalOpt.setNumOpts(line); m_originalGlobalOpt.setNumOpts(line); m_originalGlobalOpt.setFlagLocTrue(); infile.close(); infile.clear(); infile.open(os.str()); for(int i=0;i<line;i++) for(int j=0;j<numObj;j++) infile>>m_originalGlobalOpt[i].data().m_obj[j]; m_globalOpt=m_originalGlobalOpt; infile.close(); setObjSet(); }