📄 userdefinables.cpp
字号:
case NoConstraints: case Tournament: // no extra parameters break; case Penalty: { // penalty function if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { printf("Using linear penalty be default\n"); globalSetup->penaltyFunction = Linear; } else if (strcmp("Linear", pToken) == 0) { globalSetup->penaltyFunction = Linear; } else if (strcmp("Quadratic", pToken) == 0) { globalSetup->penaltyFunction = Quadratic; } else { fclose(fInput); printf("Error! valid penalty function methods are: Linear and Quadratic\n"); exit(1); } } break; default: { fclose(fInput); printf("Error! invalid constraint-handling method parameter\n"); exit(1); } break; } // local search method if ((pToken = readOneLine(caBuf, ciBufSize, fInput)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } if(strcmp("default", pToken) == 0) { printf("Using no local search by default\n"); globalSetup->localSearchMethod = NoLocalSearch; } // local search method else if (strcmp("NoLocalSearch", pToken) == 0) { globalSetup->localSearchMethod = NoLocalSearch; } else if (strcmp("SimplexSearch", pToken) == 0) { globalSetup->localSearchMethod = SimplexSearch; } else { fclose(fInput); printf("Error! valid local search methods are: NoLocalSearch and SimplexSearch\n"); exit(1); } // check local search method if ((globalSetup->localSearchMethod != NoLocalSearch) && (globalSetup->gaType == NSGA)) { fclose(fInput); printf("Error! cannot use local search with NSGA\n"); exit(1); } switch(globalSetup->localSearchMethod) { case NoLocalSearch: // no extra parameters break; case SimplexSearch: if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { printf("Using default tolerance of 0.001\n"); globalSetup->maxLocalTolerance = 1.0E-3; } else globalSetup->maxLocalTolerance = atof(pToken); if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { printf("Setting maximum local search evaluations to 20\n"); globalSetup->maxLocalEvaluations = 20; } else globalSetup->maxLocalEvaluations = atoi(pToken); if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { printf("Using default local penalty parameter of 1.0\n"); globalSetup->initialLocalPenaltyParameter = 1.0; } else globalSetup->initialLocalPenaltyParameter = atof(pToken); if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { printf("Using default local update parameter of 2.0\n"); globalSetup->localUpdateParameter = 2.0; } else globalSetup->localUpdateParameter = atof(pToken); if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { printf("Using default Lamarckian probability of 0.15\n"); globalSetup->lamarckianProbability = 0.15; } else globalSetup->lamarckianProbability = atof(pToken); if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { printf("Using default overall local search probability of 0.5\n"); globalSetup->localSearchProbability = 0.5; } else globalSetup->localSearchProbability = atof(pToken); break; default: { fclose(fInput); printf("Error! invalid local search parameters\n"); exit(1); } break; } // stopping criteria // noOfStoppingCriterias if ((pToken = readOneLine(caBuf, ciBufSize, fInput)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } if(strcmp("default", pToken) == 0) { printf("Using no extra stopping criterias by default.\n"); globalSetup->noOfStoppingCriterias = 0; } // the number can't be less than zero else if ((globalSetup->noOfStoppingCriterias = atoi(pToken)) < 0) { fclose(fInput); printf("Error! number of stopping criterias must be > 0\n"); exit(1); } else if (globalSetup->noOfStoppingCriterias == 0) { if ((strlen(pToken) != 1) || (*pToken != '0')) { fclose(fInput); printf("Error! number of stopping criterias must be a number!\n"); exit(1); } } // allocate memory if(globalSetup->noOfStoppingCriterias > 0) { // number of generation window if ((pToken = readOneLine(caBuf, ciBufSize, fInput)) == NULL) { printf("The default window size used in stopping critiria is: 5.\n"); globalSetup->genNumWindow = 5; } else globalSetup->genNumWindow = atoi(pToken); // the number should be greater than zero if (globalSetup->genNumWindow <= 0) { fclose(fInput); printf("Error! window size used in stopping critieria must be > 0\n"); exit(1); } globalSetup->otherStoppingCriteria = new StoppingCriterias[globalSetup->noOfStoppingCriterias]; globalSetup->stoppingParameter = new double[globalSetup->noOfStoppingCriterias]; for (ii = 0 ; ii < globalSetup->noOfStoppingCriterias ; ii++) { // read a line if ((pToken = readOneLine(caBuf, ciBufSize, fInput)) == NULL) { fclose(fInput); printf("Error in the input file, Please see the documentation.\n"); exit(1); } // stopping criterion type if (strcmp("NoOfEvaluations", pToken) == 0) { globalSetup->otherStoppingCriteria[ii] = NoOfEvaluations; } else if (strcmp("FitnessVariance", pToken) == 0) { globalSetup->otherStoppingCriteria[ii] = FitnessVariance; } else if (strcmp("AverageFitness", pToken) == 0) { globalSetup->otherStoppingCriteria[ii] = AverageFitness; } else if (strcmp("AverageObjective", pToken) == 0) { globalSetup->otherStoppingCriteria[ii] = AverageObjective; } else if (strcmp("ChangeInBestFitness", pToken) == 0) { globalSetup->otherStoppingCriteria[ii] = ChangeInBestFitness; } else if (strcmp("ChangeInAvgFitness", pToken) == 0) { globalSetup->otherStoppingCriteria[ii] = ChangeInAvgFitness; } else if (strcmp("ChangeInFitnessVar", pToken) == 0) { globalSetup->otherStoppingCriteria[ii] = ChangeInFitnessVar; } else if (strcmp("ChangeInBestObjective", pToken) == 0) { globalSetup->otherStoppingCriteria[ii] = ChangeInBestObjective; } else if (strcmp("ChangeInAvgObjective", pToken) == 0) { globalSetup->otherStoppingCriteria[ii] = ChangeInAvgObjective; } else if (strcmp("NoOfFronts", pToken) == 0) { globalSetup->otherStoppingCriteria[ii] = NoOfFronts; } else if (strcmp("NoOfGuysInFirstFront", pToken) == 0) { globalSetup->otherStoppingCriteria[ii] = NoOfGuysInFirstFront; } else if (strcmp("ChangeInNoOfFronts", pToken) == 0) { globalSetup->otherStoppingCriteria[ii] = ChangeInNoOfFronts; } else if (strcmp("BestFitness", pToken) == 0) { globalSetup->otherStoppingCriteria[ii] = BestFitness; } else { fclose(fInput); printf("Error! invalid stopping criteria parameter\n"); exit(1); } // check criteria switch(globalSetup->otherStoppingCriteria[ii]) { case NoOfEvaluations: case FitnessVariance: // 1 - nothing to check break; case AverageFitness: case AverageObjective: case ChangeInBestFitness: case ChangeInAvgFitness: case ChangeInFitnessVar: case ChangeInBestObjective: case ChangeInAvgObjective: case BestFitness: // 2 - SGA w/o MM only if ((globalSetup->gaType != SGA) || (globalSetup->nichingType != NoNiching)) { fclose(fInput); printf("Cannot use the following stopping critier with NSGA or when Niching is used:\n"); printf("\t AverageFitness\n\t AverageObjective\n"); printf("\t ChangeInBestFitness\n\t ChangeInAvgFitness\n"); printf("\t ChangeInFitnessVar\n\t ChangeInBestObjective\n"); printf("\t ChangeInAvgObjective\n\t BestFitness\n"); printf("Error! invalid choice of stopping criteria"); exit(1); } break; case NoOfFronts: case NoOfGuysInFirstFront: case ChangeInNoOfFronts: // 3 - NSGA only if (globalSetup->gaType != NSGA) { fclose(fInput); printf("Error! following stopping criteria can be used only with NSGA:\n"); printf("\t NoOfFronts\n\t NoOfGuysInFirstFront\n\t ChangeInNoOfFronts\n"); exit(1); } break; default: { fclose(fInput); printf("Error! invalid stopping criteria\n"); exit(1); } break; } // parameter if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { fclose(fInput); printf("Error! invalid stopping criteria parameter\n"); exit(1); } ((double*)globalSetup->stoppingParameter)[ii] = atof(pToken); } } // Initial population generation // Load population if ((pToken = readOneLine(caBuf, ciBufSize, fInput)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } if(strcmp("default", pToken) == 0) { printf("Using random initialization by default.\n"); globalSetup->loadPopulation = false; globalSetup->evaluateAgain = true; } // Check if the load population is 0 or 1 else { // load the population from a file if(atoi(pToken) == 1) { globalSetup->loadPopulation = true; globalSetup->populationFileName = new char[256]; // Read the name of the file to load the population from if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { fclose(fInput); printf("Error! invalid file name to load the population from, see documentation for more details.\n"); exit(1); } strcpy(globalSetup->populationFileName, pToken); // Evaluate the population or not if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { printf("Evaluating the initial population by default.\n"); globalSetup->evaluateAgain = true; } else if(atoi(pToken) == 0) { globalSetup->evaluateAgain = false; } else globalSetup->evaluateAgain = true; } // Use random initialization else { globalSetup->loadPopulation = false; globalSetup->evaluateAgain = true; } } // Save population if ((pToken = readOneLine(caBuf, ciBufSize, fInput)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } if(strcmp("default", pToken) == 0) { printf("Saving the evaluated individuals to a file by default.\n"); globalSetup->savePopulation = true; globalSetup->saveEvalSolutions = new char[256]; printf("Enter the filename you want to save the population to.\n"); fflush(stdout); scanf("%s", globalSetup->saveEvalSolutions); } // Check if the save population is 0 or 1 else { fflush(stdout); // Save the population to a file if(atoi(pToken) == 1) { globalSetup->savePopulation = true; // Filename to save the evaluated solutions to. globalSetup->saveEvalSolutions = new char[256]; if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } strcpy(globalSetup->saveEvalSolutions, pToken); } } fclose(fInput); fflush(stdout); if(globalSetup->savePopulation) { fOutput = fopen(globalSetup->saveEvalSolutions, "w"); fprintf(fOutput, "%% "); for(ii = 0; ii < globalSetup->noOfDecisionVariables; ii++) { fprintf(fOutput, "var #%d\t", ii); } for(ii = 0; ii < globalSetup->finalNoOfObjectives; ii++) { fprintf(fOutput, "obj #%d\t", ii); } if(globalSetup->finalNoOfConstraints > 0) { for(ii = 0; ii < globalSetup->finalNoOfConstraints; ii++) { fprintf(fOutput, "const #%d\t", ii); } fprintf(fOutput, "penalty"); } fprintf(fOutput, "\n"); fclose(fOutput); } GA ga; if (globalSetup->gaType == SGA) { while(ga.generate()); } else { while(ga.nsgaGenerate()); } delete [](globalSetup->penaltyWeights); delete [](globalSetup->variableTypes); for(ii = 0; ii < globalSetup->noOfDecisionVariables; ii++) { delete [](globalSetup->variableRanges[ii]); } delete [](globalSetup->variableRanges); delete [](globalSetup->typeOfOptimizations); if((globalSetup->selectionType == TournamentWR)||(globalSetup->selectionType == TournamentWOR)||(globalSetup->selectionType == Truncation)) { delete [](int*)(globalSetup->selectionParameters); } if((globalSetup->xOverType==Uniform)||(globalSetup->xOverType == SBX)) delete [](double*)(globalSetup->xOverParameters); if(globalSetup->mutationType == Polynomial) delete [](int*)(globalSetup->mutationParameters); else if(globalSetup->mutationType == Genewise) delete [](double*)(globalSetup->mutationParameters); if(globalSetup->nichingType == RTS) delete [](int*)(globalSetup->nichingParameters); if(globalSetup->nichingType == Sharing) delete [](double*)(globalSetup->nichingParameters); if(globalSetup->scalingMethod == SigmaScaling) delete [](double*)(globalSetup->scalingParameters); if(globalSetup->noOfStoppingCriterias > 0) { delete [] globalSetup->otherStoppingCriteria; delete [](double*)(globalSetup->stoppingParameter); } if(globalSetup->loadPopulation) delete [](globalSetup->populationFileName); if(globalSetup->savePopulation) delete [](globalSetup->saveEvalSolutions); return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -