void Planet::findPosition() { Point2D a; for ( int j = 0; j <getPopulation(); j++) { for (int k = j + 1; k < getPopulation(); k++) { if (a.getDistance(population[j]->getPosition(), population[k]->getPosition()) < 5) { int random = rand() % 2 + 1; switch (random) { case 1: population[j]->Attack(*population[j]); break; case 2: addPopulation(entity, 1); } } else if (a.getDistance(population[j]->getPosition(), population[k]->getPosition()) == 0) { movePopulation(); } } } }
float EpidemicDataSet::getPopulation(std::vector<int> nodeIds) { float population = 0.; for(unsigned int i=0; i<nodeIds.size(); i++) { population += getPopulation(nodeIds[i]); } return population; }
void fill_scale_values() { scale_values.police = (target_population_per_police/populationPerPolice())-0.49f; scale_values.health = getNumberOfHospitals() > 0 ? 1.f : 0.f; scale_values.education = (target_population_per_school/populationPerSchool())-0.49f; scale_values.shopping = ((((float)getPopulation()/(float)target_population_per_shop) - (float)number_of_shops) <= 0) ? 1.f : 0.f; scale_values.power = 1-(reqired_power)/((float)power_avalible); scale_values.waste = wasteDisposalUtilisation(); if(getPolution() == 0) scale_values.polution = 1.f; else if(getPopulation() == 0) scale_values.polution = 1.f-((float)getPolution()/500.f); else scale_values.polution = 1.f-getPolutionPerPerson(); float total = 0; total += scale_values.police > 0.f ? scale_values.police > 1.f ? 1.f : scale_values.police : 0.f; total += scale_values.health > 0.f ? scale_values.health > 1.f ? 1.f : scale_values.health : 0.f; total += scale_values.education > 0.f ? scale_values.education > 1.f ? 1.f : scale_values.education : 0.f; total += scale_values.shopping > 0.f ? scale_values.shopping > 1.f ? 1.f : scale_values.shopping : 0.f; total += scale_values.power > 0.f ? scale_values.power > 1.f ? 1.f : scale_values.power : 0.f; total += scale_values.waste > 0.f ? scale_values.waste > 1.f ? 1.f : scale_values.waste : 0.f; total += scale_values.polution > 0.f ? scale_values.polution > 1.f ? 1.f : scale_values.polution : 0.f; scale_values.average = total/6.f; }
/** * Compute city population and city classification. * @see cityPop cityClass. */ void Micropolis::doPopNum() { Quad oldCityPop = cityPop; cityPop = getPopulation(); if (oldCityPop == -1) { oldCityPop = cityPop; } cityPopDelta = cityPop - oldCityPop; cityClass = getCityClass(cityPop); }
void draw_HUD(SDL_Renderer* ren) { //Draw the top bar SDL_SetRenderDrawColor(ren, 255, 255, 255, 0); top_bar.w = window_size_x; SDL_RenderFillRect(ren, &top_bar); draw_int(ren, font, top_bar_text_color, 20, 0, reqired_power, "Required: ", " MW"); draw_int(ren, font, top_bar_text_color, 20, 20, power_avalible, "Avalible: ", " MW"); draw_int(ren, font, top_bar_text_color, 200, 00, getBalance(), "£", "000"); draw_int(ren, font, top_bar_text_color, 200, 20, lastBalanceChange, "£", "000"); draw_int(ren, font, top_bar_text_color, 400, 0, getPopulation(), "Pop: ", ""); fill_scale_values(); public_happiness_scale.x = window_size_x-100-80; draw_scale(ren, &public_happiness_scale, scale_values.average); //Draw the side bar if(show_sidebar) { SDL_SetRenderDrawColor(ren, 255, 255, 255, 0); side_bar.x = window_size_x - side_bar.w; SDL_RenderFillRect(ren, &side_bar); SDL_Rect scaleBox = {window_size_x-100-5,0,100,fontSizeLarge+2}; int item_y = side_bar.y+5; draw_string(ren, fontLarge, top_bar_text_color, side_bar.x+5, item_y, &_binary_VALUE_TEXT_POLICE_start); scaleBox.y = item_y; draw_scale(ren, &scaleBox, scale_values.police); item_y += fontSizeLarge+2; draw_string(ren, fontLarge, top_bar_text_color, side_bar.x+5, item_y, &_binary_VALUE_TEXT_HEALTH_start); setColorGoodBad(ren, scale_values.health); scaleBox.y = item_y; SDL_RenderFillRect(ren, &scaleBox); item_y += fontSizeLarge+2; draw_string(ren, fontLarge, top_bar_text_color, side_bar.x+5, item_y, &_binary_VALUE_TEXT_EDUCATION_start); setColorGoodBad(ren, populationPerSchool() < target_population_per_school); scaleBox.y = item_y; draw_scale(ren, &scaleBox, scale_values.education); item_y += fontSizeLarge+2; draw_string(ren, fontLarge, top_bar_text_color, side_bar.x+5, item_y, &_binary_VALUE_TEXT_SHOPPING_start); setColorGoodBad(ren, scale_values.shopping); scaleBox.y = item_y; SDL_RenderFillRect(ren, &scaleBox); item_y += fontSizeLarge+2; draw_string(ren, fontLarge, top_bar_text_color, side_bar.x+5, item_y, &_binary_VALUE_TEXT_POWER_start); scaleBox.y = item_y; draw_scale(ren, &scaleBox, scale_values.power); item_y += fontSizeLarge+2; draw_string(ren, fontLarge, top_bar_text_color, side_bar.x+5, item_y, &_binary_VALUE_TEXT_WASTE_start); scaleBox.y = item_y; draw_scale(ren, &scaleBox, scale_values.waste); item_y += fontSizeLarge+2; draw_string(ren, fontLarge, top_bar_text_color, side_bar.x+5, item_y, &_binary_VALUE_TEXT_POLUTION_start); scaleBox.y = item_y; draw_scale(ren, &scaleBox, scale_values.polution); } SDL_SetRenderDrawColor(ren, 0, 0, 0, 0); SDL_Rect open_menu_button = {window_size_x-30,0,30,30}; SDL_RenderFillRect(ren, &open_menu_button); char* modeText = NULL; switch(getMode()) { case MODE_BUILD_RESIDENTIAL_1: modeText = &_binary_MODE_TEXT_RESIDENTIAL_1_start; break; case MODE_BUILD_RESIDENTIAL_2: modeText = &_binary_MODE_TEXT_RESIDENTIAL_2_start; break; case MODE_BUILD_ROAD: modeText = &_binary_MODE_TEXT_BUILD_ROAD_start; break; case MODE_BUILD_POWER_GAS: modeText = &_binary_MODE_TEXT_BUILD_POWER_GAS_start; break; case MODE_BUILD_DESTROY: modeText = &_binary_MODE_TEXT_DESTROY_start; break; case MODE_BUILD_RETAIL: modeText = &_binary_MODE_TEXT_BUILD_RETAIL_start; break; case MODE_BUILD_HOSPITAL: modeText = &_binary_MODE_TEXT_BUILD_HOSPITAL_start; break; case MODE_BUILD_POWER_SOLAR: modeText = &_binary_MODE_TEXT_BUILD_POWER_SOLAR_start; break; default: break; } if(modeText != NULL) { draw_string(ren, font, top_bar_text_color, 5, window_size_y-15, modeText); } if(ready_to_place) { SDL_SetRenderDrawColor(ren, 255, 255, 255, 0); SDL_RenderFillRect(ren, &planned_cost_box); draw_int(ren, font, top_bar_text_color, 200, 35, costOfPlannedBuild(), "(-£", "000)"); } SDL_SetRenderDrawColor(ren, 0, 0, 0, 0); draw_menu(ren); if(active_emergency()) { Point p = {window_size_x-16, 80}; drawTile(ren, &p, getClip(SPRITE_BUILD_BACKGROUND)); drawTile(ren, &p, getClip(SPRITE_BUILD_SERVICES_POLICE)); } }
bool MuSlashRhoLambdaES::doNextGeneration() { /* QFile file ("evolution.log"); file.open(QIODevice::WriteOnly | QIODevice::Append); QTextStream out(&file); out << "\n\n--------------------------\nNew generation:\n"; */ bool ok = true; QList<ESIndividual*> offsprings; QList<double> bestParentsFitness; // generate offsprings for(int i = 0; i < mInformation.lambda; i++) { // select parents which should be used to generate current offspring QList<ESIndividual*> curParents = mMarriage->doMarriage(getPopulation(), mInformation.rho, mInformation); // check if an error occured if(curParents.size() <= 0){ return false; } // get fitness of the best parent qSort(curParents.begin(), curParents.end(), MuSlashRhoLambdaES::descendingOrder); bestParentsFitness.append(curParents.first()->getFitness()); /* out << "\tParents:\n"; for(int parentIndex = 0; parentIndex < curParents.size(); parentIndex++) { out << "\t\t" << curParents.at(parentIndex)->toString() << "\n"; } */ // recombine offspring strategy parameters from parents QList<OptimizationDouble> recombinedStratParas = mStrategyParametersRecombination->doRecombination( curParents, ESRecombination::STRATEGY, mInformation); // check if an error occured if(recombinedStratParas.size() <= 0){ return false; } // recombine offspring object parameters from parents QList<OptimizationDouble> recombinedObjParas = mObjectParametersRecombination->doRecombination( curParents, ESRecombination::OBJECT, mInformation); // check if an error occured if(recombinedObjParas.size() <= 0){ return false; } // mutate the offspring strategy parameters QList<OptimizationDouble> mutatedStratParas = mStrategyParametersMutation->doMutation( recombinedStratParas, mInformation); // check if an error occured if(mutatedStratParas.size() <= 0){ return false; } // mutate the offspring object parameters with help of is mutated strategy parameters QList<OptimizationDouble> mutatedObjParas = mObjectParametersMutation->doMutation( recombinedObjParas, mutatedStratParas, mInformation); // check if an error occured if(mutatedObjParas.size() <= 0){ return false; } // create new Individual and add it to offspring list offsprings.append( new ESIndividual(mutatedObjParas, mutatedStratParas, 0.0) ); /* out << "\t--> Offspring:\n"; out << "\t\t" << offsprings.last()->toString(); out << "\n\n"; */ } //calculate fitness values of offsprings ok &= mFitnessEvaluation->doEvaluation(offsprings, mInformation); if(ok == false){ return false; } // calculate sum of offsprings which have a better fitness then their best parent mInformation.numberOfLastSuccessfulIndividuals = 0; for(int i = 0; i < offsprings.size(); i++) { if(offsprings.at(i)->getFitness() > bestParentsFitness.at(i)){ mInformation.numberOfLastSuccessfulIndividuals++; } } bestParentsFitness.clear(); // sort offspring accordingly their fitness qSort(offsprings.begin(), offsprings.end(), MuSlashRhoLambdaES::descendingOrder); // get list with new population QList<ESIndividual*> selectionList = mSelection->doSelection(mPopulation, offsprings, mInformation.mu, mInformation); // check if an error occured if(selectionList.size() <= 0){ return false; } // delete old parents which are not in the selection deletePopulation(selectionList); // use selection as new population mPopulation = selectionList; // update statisticvalues in mInformation calculateStatistics(); mInformation.currentGeneration++; /* file.flush(); file.close(); */ return ok; }