void MMKP_BBA::globalSearch(std::vector<MMKPBatSolution>& population){ //already in sorted order in this implementation. //MMKP_BBA::quickSort(population,0,(population.size()-1)); std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> dib(0, 1); MMKPBatSolution bestBat = population[0]; ReactiveLocalSearch RLS(dataSet); bestBat.solution = RLS(bestBat.solution); this->currentFuncEvals += RLS.getFuncEvals(); for(int i=0;i<population.size();i++){ MMKPBatSolution currentBat = population[i]; //equation 1 float B = dib(gen); float fTemp = currentBat.fmax + ((currentBat.fmax -currentBat.fmin)*B); //equation 2 float averageV = 0.0; int averageVSize = 0; for(int j=0;j<currentBat.solution.size();j++){ for(int k=0;k<currentBat.solution[j].size();k++){ this->currentFuncEvals++; float vTemp = currentBat.v + ((currentBat.solution[j][k] -bestBat.solution[j][k])*fTemp); averageV += vTemp; averageVSize++; //equation "3"--binary uses 7 and 8 float normalizedV = 1/(1+(exp(-(vTemp)))); float r = dib(gen); if(r <= normalizedV){ currentBat.solution[j][k] = 1; }else{ currentBat.solution[j][k] = 0; } } } currentBat.v = (averageV/averageVSize); MMKP_MetaHeuristic::makeFeasible(currentBat.solution); if(dib(gen) > currentBat.r){ int localSolutionIndex = rand() % 10; MMKPBatSolution bestBat = population[localSolutionIndex]; MMKP_BBA::localSearch(bestBat,currentBat); }else{ MMKP_BBA::Mutate(currentBat); } //MMKP_BBA::randomSearch(currentBat); MMKP_BBA::competitiveUpdateSol(population[i],currentBat); } }
double Potential_JS::R_L(double Lz,double r){ // Can pass initial guess for speed RL_st RLS(Lz,this); root_find RF(1e-6,200); double up = 1e5,down=1e-5; if(r>0.){up=10.*r;down=0.1*r;} return RF.findroot(&RL_fn,down,up,&RLS); }
void TestREstimate() { EstimateARMASpectrum(sAc, autoCorr, aCoeff, spectrumEstimate, 28, 3, errorHistory); EstimateAutoCorrelationValues(sAc, rVal, 4, 3); A[0][0] = 1;A[0][1] = -1;A[0][2] = 2;A[0][3] = -1;A[0][4] = 3; A[1][0] = 1;A[1][1] = 1;A[1][2] = 1;A[1][3] = 1;A[1][4] = 1; A[2][0] = -1;A[2][1] = 2;A[2][2] = 4;A[2][3] = -1;A[2][4] = 0; A[3][0] = 0;A[3][1] = 2;A[3][2] = 0;A[3][3] = 1;A[3][4] = 2; A[4][0] = 3;A[4][1] = 4;A[4][2] = 1;A[4][3] = 0;A[4][4] = 2; c[0] = 16; c[1] = 15; c[2] = 11; c[3] = 18; c[4] = 24; float TotalError = RLS(A, c, ap, 5); }
int main(int argc, char* argv[]){ std::string folder = "orlib_data"; std::string file = "I01"; int problem = 1; int popSize = 30; int genSize = 40; unsigned int seed = 1234; std::string mods = ""; if(argc==7){ folder = argv[1]; file = argv[2]; problem = atoi(argv[3]); mods = argv[4]; popSize = atoi(argv[5]); genSize = atoi(argv[6]); }else if(argc==8){ folder = argv[1]; file = argv[2]; problem = atoi(argv[3]); mods = argv[4]; popSize = atoi(argv[5]); genSize = atoi(argv[6]); seed = atoi(argv[7]); }else{ std::cout<<"usage: filename <folder><name><number><popSize><genSize>\n"; return 0; } std::ifstream fileStream; MMKPDataSet dataSet; clock_t t1,t2; float runtime; fileStream.open(folder+std::string("/")+file); /* READ INPUT */ if(fileStream.is_open()){ //build MMKPDataSet object if(folder=="orlib_data"){ OrLib_Read readInput; dataSet = readInput(fileStream); }else if(folder=="HiremathHill_data"){ HiremathHill_Read readInput; dataSet = readInput(fileStream,problem); }else{ std::cout<<"Error, unrecognized folder name."<<std::endl; } }else{ std::cerr<<std::string("File ")+file+std::string(" failed to open.") <<std::endl; } fileStream.close(); //generate initial population GenerateRandomizedPopulationNoDups generatePopulation; std::vector<MMKPSolution> population = generatePopulation(dataSet,popSize); //algs used MMKP_TLBO tlbo(dataSet); MMKP_COA coa(dataSet); MMKP_GA ga(dataSet); tlbo.quickSort(population,0,population.size()-1); MMKPSolution optimalSolution; for(int i=0;i<population.size();i++){ if(dataSet.isFeasible(population[i])){ optimalSolution = population[i]; break; } } MMKPSolution initialTeacher = optimalSolution; t1=clock(); std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(0, 2); int eliteSolutionSize = 5; std::vector<MMKPSolution> eliteSolutions; int count = 0; for(int i=0;i<population.size();i++){ dataSet.updateSolution(population[i]); if(dataSet.isFeasible(population[i])){ eliteSolutions.push_back(population[i]); count++; } if(count > eliteSolutionSize){ break; } } int convergenceGen = 0; std::vector<std::tuple<int, float> > convData; int functionEvalCounter = 0; for(int i=0;i<genSize;i++){ //divide population by distribution std::vector<MMKPSolution> pop1; std::vector<MMKPSolution> pop2; std::vector<MMKPSolution> pop3; for(int j=0;j<population.size();j++){ int p = dis(gen); switch(p){ case 0: pop1.push_back(population[j]); break; case 1: pop2.push_back(population[j]); break; case 2: pop3.push_back(population[j]); break; } } //replace worse 3 solutions of each population with //elite solutions tlbo.quickSort(pop1,0,pop1.size()-1); tlbo.quickSort(pop2,0,pop2.size()-1); tlbo.quickSort(pop3,0,pop3.size()-1); for(int j=0;j<eliteSolutions.size();j++){ pop1[((pop1.size()-1)-j)] = eliteSolutions[j]; pop2[((pop2.size()-1)-j)] = eliteSolutions[j]; pop3[((pop3.size()-1)-j)] = eliteSolutions[j]; } pop1 = tlbo.runOneGeneration(pop1); functionEvalCounter += pop1.size()*2; pop2 = coa.runOneGeneration(pop2); functionEvalCounter += pop2.size()*2; pop3 = ga.runOneGeneration(pop3); functionEvalCounter += pop3.size(); population.clear(); population.reserve(pop1.size() + pop2.size() + pop3.size()); population.insert(population.end(), pop1.begin(), pop1.end()); population.insert(population.end(), pop2.begin(), pop2.end()); population.insert(population.end(), pop3.begin(), pop3.end()); //get and save best solution //and update our elite list count = 0; tlbo.quickSort(population,0,population.size()-1); for(int j=0;j<population.size();j++){ dataSet.updateSolution(population[j]); if(dataSet.isFeasible(population[j])){ if(population[j].getProfit() > optimalSolution.getProfit()){ optimalSolution = population[j]; convergenceGen = i+1; } if(!(doesContain(eliteSolutions,population[j]))){ eliteSolutions[count] = population[j]; count++; } if(count > eliteSolutionSize){ break; } } } ReactiveLocalSearch RLS(dataSet); //run local search on elite solutions eliteSolutions = RLS(eliteSolutions); //update conv. vector std::tuple<int, float> temp(functionEvalCounter,optimalSolution.getProfit()); convData.push_back(temp); } t2 = clock(); runtime = ((float)t2-(float)t1)/(double) CLOCKS_PER_SEC; std::cout<<"Problem: "<<std::endl; std::cout<<folder<<std::string("/")<<file<<std::endl; std::cout<<"Problem Number:"<<std::endl; std::cout<<problem<<std::endl; std::cout<<"Initial Profit:"<<std::endl; std::cout<<initialTeacher.getProfit()<<std::endl; std::cout<<"Profit:"<<std::endl; if(dataSet.isFeasible(optimalSolution)){ std::cout<<optimalSolution.getProfit()<<std::endl; }else{ std::cout<<0<<std::endl; } std::cout<<"Runtime:"<<std::endl; std::cout<<runtime<<std::endl; std::cout<<"Sol Found in _ Iterations"<<std::endl; std::cout<<convergenceGen<<std::endl; std::cout<<"Convergence Count:"<<std::endl; std::cout<<convData.size()<<std::endl; for(int i=0;i<convData.size();i++){ std::cout<<std::get<0>(convData[i])<<std::endl; std::cout<<std::get<1>(convData[i])<<std::endl; } std::cout<<"Num of Classes:"<<std::endl; std::cout<<optimalSolution.size()<<std::endl; //num of classes for(int i=0;i<optimalSolution.size();i++){ for(int j=0;j<optimalSolution[i].size();j++){ std::cout<<optimalSolution[i][j]<<" "; } std::cout<<std::endl; } std::cout<<std::endl; return 0; }