void Individual::update(Individual& source) { if (!this->isLocked()) return; if (Util::isBetterSolution(source.getTotalCost(), this->getTotalCost())) { this->setGene(source.getGene()); this->setRoutes(source.getRoutesConst()); this->updateParameters(true); } }
int fitnessCalcPGA::getTaskGene(int task, Individual ind) { int i, j, k, gene; for(i = 0; i < nAgents; i++) { for(k = 0; k < nLevels; k++) { gene = ind.getGene(i * (nTasks * nLevels) + task * nLevels + k); if (gene) return (i * (nTasks * nLevels) + task * nLevels + k); } } cout << "Task " << task << " not assigned"<< endl; return -1; }
double fitnessCalcPGA::getFOPower(Individual individual) { double power = 0.0; int i, j, k; for(i = 0; i < nAgents; i++) { for(j = 0; j < nTasks; j++) { for(k = 0; k < nLevels; k++) { power += ((alpha * (LCM / period[j]) * cycles[i][j] * (voltage[i][k] * voltage[i][k])) + (LCM * (1.0 - (cycles[i][j] / (frequency[i][k]) ) / period[j]) * Pidle)) * individual.getGene(i * (nTasks * nLevels) + j * (nLevels) + k); } } } if (power >= 0.0 && power <= 0.0) power = std::numeric_limits<double>::max(); return power; }
Individual Individual::evolve() { // //If it is locked // if (this->isLocked()) // return Individual(); this->setLocked(true); Individual offspring = this->copy(); if (this->getGene().size() == 0) { printf("Subpop = %d => Ind vazio!\n", this->getDepot()); Individual offspring = Individual(this->getProblem(), this->getConfig(), this->getDepot(), this->getId()); offspring.create(); } //cout << "Offspring = " << offspring.getTotalCost() << endl; bool mutate = false; //printf("Offspring = D: %d - Id: %d => PM: %.2f / PLS: %.2f / RS: %d\n", this->getDepot(), // this->getId(), this->getMutationRatePM(), this->getMutationRatePLS(), this->isRestartMoves()); if (Random::randFloat() <= this->getMutationRatePM()) { offspring.mutate(); offspring.evaluate(true); mutate = true; } else { if (offspring.getRoutes().size() == 0) offspring.evaluate(true); } //offspring.printSolution(); if (Random::randFloat() <= this->getMutationRatePLS()) { //Individual orig = offspring; //offspring.printSolution(true); offspring.localSearch(); offspring.routesToGenes(); //offspring.printSolution(true); if (offspring.getGene().size() == 0) { cout << "Mutate = " << mutate << endl; this->print(); this->print(true); this->printSolution(); offspring.print(); offspring.print(true); offspring.printSolution(); // orig.print(); // orig.print(true); // orig.printSolution(); } } if (this->getRoutes().size() == 0) { //cout << "Wait\n\n"; //cout << "Old: " << this->getTotalCost() << endl; this->evaluate(true); //cout << "New: " << this->getTotalCost() << endl; } if (Util::isBetterSolution(offspring.getTotalCost(), this->getTotalCost())) { //this->setGene(offspring.getGene()); //this->setRoutes(offspring.getRoutesConst()); //this->evaluate(true); this->updateParameters(true); } else { this->updateParameters(false); } //this->print(true); //this->setLocked(false); return offspring; }
bool fitnessCalcPGA::isIndividualValid(Individual ind) { struct runInfo runtime; double sp; bool ret; int s, i, j, k; vector <class Task> tasks; IloNumArray4 dec(env, 1); runtime.setVerbose(false); runtime.setList(false); for (s = 0; s < 1; s++) { dec[s] = IloNumArray3(env, nAgents); for (i = 0; i < nAgents; i++) { dec[s][i] = IloNumArray2(env, nTasks); for (j = 0; j < nTasks; j++) { dec[s][i][j] = IloNumArray(env, nLevels, 0, 1, ILOINT); for (k = 0; k < nLevels; k++) { dec[s][i][j][k] = ind.getGene(i * (nTasks * nLevels) + j * nLevels + k); } } } } tasks.clear(); for (j = 0; j < nTasks; j++) { Task t(env); t.setPriority(priority[j]); t.setPeriod(period[j]); t.setDeadline(Deadline[j]); t.setIp(0.0); /* do not touch for now */ t.setIb(0.0);/* do not touch for now */ t.setIa(0.0); /* do not touch for now */ t.setIj(0.0); /* do not touch for now */ for (i = 0; i < nAgents; i++) for (k = 0; k < nLevels; k++) if (dec[0][i][j][k]) t.setWcec(cycles[i][j]); tasks.push_back(t); } SchedulabilityAnalysis sched(env, runtime, nTasks, 0, /* nresources */ 0.0, /* Lp */ frequency, voltage, tasks, dec); sched.computeAnalysis(); ret = sched.evaluateResponse(sp); tasks.clear(); for (s = 0; s < 1; s++) { for (i = 0; i < nAgents; i++) { for (j = 0; j < nTasks; j++) { dec[s][i][j].end(); } dec[s][i].end(); } dec[s].end(); } dec.end(); return ret; }