?? userdefinables.cpp
字號:
/***************************************************************/* Single & Multi-Objective Real-Coded Genetic Algorithms Code *//* Author: Kumara Sastry *//* Illinois Genetic Algorithms Laboratory (IlliGAL) *//* Deparment of General Engineering *//* University of Illinois at Urbana-Champaign *//* 104 S. Mathews Ave, Urbana, IL 61801 *//***************************************************************/#include <iostream>#include <iomanip>#include "ga.hpp"#include "random.hpp"#include "globalSetup.hpp"#include <math.h>#define BLANK_STR " \t\n\r"using namespace std;GlobalSetup *globalSetup;Random myRandom;/* Objective function and constraints go here */void globalEvaluate(double *x, double *objArray, double *constraintViolation, double *penalty, int *noOfViolations){ int ii; FILE *outEvals; for(ii = 0; ii < globalSetup->finalNoOfObjectives; ii++) objArray[ii] = 0.0; if(globalSetup->gaType == SGA) { *objArray = (x[0]*x[0] + x[1] - 11.0)*(x[0]*x[0] + x[1] - 11.0) + (x[0] + x[1]*x[1] - 7.0)*(x[0] + x[1]*x[1] - 7.0); constraintViolation[0] = (x[0]-5.0)*(x[0]-5.0) + x[1]*x[1] - 26.0; if(constraintViolation[0] <= 0.0) constraintViolation[0] = 0.0; constraintViolation[1] = 4*x[1] + x[2] - 20.0; if(constraintViolation[1] <= 0.0) constraintViolation[1] = 0.0; } else { objArray[0] = -10*exp(-0.2*sqrt(x[0]*x[0] + x[1]*x[1])) - 10*exp(-0.2*sqrt(x[1]*x[1] + x[2]*x[2])); objArray[1] = pow(fabs(x[0]),0.8) + pow(fabs(x[1]),0.8) + pow(fabs(x[2]),0.8) + 5.0*sin(x[0]*x[0]*x[0]) + 5.0*sin(x[1]*x[1]*x[1]) + 5.0*sin(x[2]*x[2]*x[2]); } *penalty = 0.0; *noOfViolations = 0; for(ii = 0; ii < globalSetup->finalNoOfConstraints; ii++) { if(constraintViolation[ii]) ++(*noOfViolations); if(globalSetup->constraintMethod == Penalty) { if(globalSetup->penaltyFunction == Linear) *penalty += globalSetup->penaltyWeights[ii]*fabs(constraintViolation[ii]); else if(globalSetup->penaltyFunction == Quadratic) *penalty += globalSetup->penaltyWeights[ii]* (constraintViolation[ii]*constraintViolation[ii]); } else *penalty += fabs(constraintViolation[ii]); } if(globalSetup->savePopulation) { outEvals = fopen(globalSetup->saveEvalSolutions, "a"); for(ii = 0; ii < globalSetup->noOfDecisionVariables; ii++) { fprintf(outEvals, "%f\t", x[ii]); } for(ii = 0; ii < globalSetup->finalNoOfObjectives; ii++) { fprintf(outEvals, "%f\t", objArray[ii]); } if(globalSetup->finalNoOfConstraints > 0) { for(ii = 0; ii < globalSetup->finalNoOfConstraints; ii++) { fprintf(outEvals, "%f\t", constraintViolation[ii]); } fprintf(outEvals, "%f", *penalty); } fprintf(outEvals, "\n"); fflush(outEvals); fclose(outEvals); }}/* Read a non-comment, non-blank line from the specified file stream At EOF, it returns NULL. Otherwise, it returns the pointer to the first token. (The string just read in is altered by strtok().)*/static char* readOneLine(char *pcBuf, int iMaxSize, FILE *fStream){ char *pToken; do { pToken = NULL; *pcBuf = '\0'; fgets(pcBuf, iMaxSize, fStream); if (feof(fStream)) break; // get the first token pToken = strtok(pcBuf, BLANK_STR); // if there is no first token, it is a blank line. // if the first token starts with '#', it is a comment line. } while ((pToken == NULL) || (*pToken == '#')); return (pToken);} int main(int argc, char *argv[]) { int ii; const int ciBufSize = 1024; char *pToken, caBuf[ciBufSize]; FILE *fInput, *fOutput; if(argc != 2) { printf("Error! Usage is GAtbx inputfile\n"); exit(1); } fInput = fopen(argv[1], "r"); if (fInput == NULL) { printf("Error! opening file %s\n", argv[1]); exit(1); } globalSetup = new GlobalSetup; if ((pToken = readOneLine(caBuf, ciBufSize, fInput)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } //GA type if (strcmp("SGA", pToken) == 0) { globalSetup->gaType = SGA; } else if (strcmp("NSGA", pToken) == 0) { globalSetup->gaType = NSGA; } else { fclose(fInput); printf("Unknown parameter! It should be either SGA or NSGA\n"); exit(1); } // decision variables // read the number of decision variables if ((pToken = readOneLine(caBuf, ciBufSize, fInput)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } // the number can't be less than or equal to zero if ((globalSetup->noOfDecisionVariables = atoi(pToken)) <= 0) { fclose(fInput); printf("Error! number of decision variables should be > 0\n"); exit(1); } globalSetup->variableTypes = new VariableType[globalSetup->noOfDecisionVariables]; globalSetup->variableRanges = new double*[(globalSetup->noOfDecisionVariables)]; for(ii = 0; ii < globalSetup->noOfDecisionVariables; ii++) { // read a line if ((pToken = readOneLine(caBuf, ciBufSize, fInput)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } // variable type if (strcmp("double", pToken) == 0) { globalSetup->variableTypes[ii] = Real; } else if (strcmp("int", pToken) == 0) { globalSetup->variableTypes[ii] = Integer; } else { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } // variable ranges // allocate memory globalSetup->variableRanges[ii] = new double[2]; // lower bound if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } globalSetup->variableRanges[ii][0] = atof(pToken); // upper bound if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } globalSetup->variableRanges[ii][1] = atof(pToken); if(globalSetup->variableRanges[ii][1] <= globalSetup->variableRanges[ii][0]) { fclose(fInput); printf("Error! lower bound, %f, must be lower than the upper bound, %f\n", globalSetup->variableRanges[ii][0], globalSetup->variableRanges[ii][1]); exit(1); } } // objectives // read the number of objectives if ((pToken = readOneLine(caBuf, ciBufSize, fInput)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } // the number can't be less than or equal to zero if ((globalSetup->noOfRawObjectives = atoi(pToken)) <= 0) { fclose(fInput); printf("Error! number of objectives should be > 0\n"); exit(1); } globalSetup->finalNoOfObjectives = globalSetup->noOfRawObjectives; globalSetup->noOfLinearObjectiveCombinations = 0; globalSetup->typeOfOptimizations = new OptimType[globalSetup->finalNoOfObjectives]; for(ii = 0; ii < globalSetup->finalNoOfObjectives; ii++) { // read a line if ((pToken = readOneLine(caBuf, ciBufSize, fInput)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } // optimization type if (strcmp("Min", pToken) == 0) { globalSetup->typeOfOptimizations[ii] = Minimization; } else if (strcmp("Max", pToken) == 0) { globalSetup->typeOfOptimizations[ii] = Maximization; } else { fclose(fInput); printf("Error! optimization type can either be Min or Max\n"); exit(1); } } // constrained variables // read the number of constrained variables if ((pToken = readOneLine(caBuf, ciBufSize, fInput)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } // the number can't be less than zero if ((globalSetup->noOfRawConstraints = atoi(pToken)) < 0) { fclose(fInput); printf("Error! number of constraints should be >= 0\n"); exit(1); } else if (globalSetup->noOfRawConstraints == 0) { if ((strlen(pToken) != 1) || (*pToken != '0')) { fclose(fInput); printf("Error! number of constraints should be >= 0\n"); exit(1); } } globalSetup->noOfLinearConstraintCombinations = 0; globalSetup->finalNoOfConstraints = globalSetup->noOfRawConstraints; // penalty weights globalSetup->penaltyWeights = new double[globalSetup->finalNoOfConstraints]; for(ii = 0; ii < globalSetup->finalNoOfConstraints; ii++) { // read a line if ((pToken = readOneLine(caBuf, ciBufSize, fInput)) == NULL) { fclose(fInput); printf("Error in the input file, please refer to the documentation\n"); exit(1); } globalSetup->penaltyWeights[ii] = atof(pToken); } // general parameters // population size 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) { // Use a default value of 30*ell*log(ell); printf("Using default population-sizing thumbrule: n = 30*ell*log(ell)\n"); // Take care of small problem sizes where log(ell) < 1 if(globalSetup->noOfDecisionVariables > 2) globalSetup->populationSize = (int)(30*(globalSetup->noOfDecisionVariables)*log((double)(globalSetup->noOfDecisionVariables))); else globalSetup->populationSize = (int)(30*(globalSetup->noOfDecisionVariables)); //Round it to next nearest tenth number if((globalSetup->populationSize)%10) globalSetup->populationSize += (globalSetup->populationSize)%10; printf("The population size used is: %d\n", globalSetup->populationSize); } // the number can't be less than or equal to zero else if ((globalSetup->populationSize = atoi(pToken)) <= 0) { fclose(fInput); printf("The population size must be > 0\n"); exit(1); } else if ((globalSetup->populationSize % 2) != 0) { // the number can't be an odd number fclose(fInput); printf("Error! population size must be an even number\n"); exit(1); } // maximum generations // the number can't be less than or equal to zero 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) { // Use a default value of 6*ell; printf("Using default convergence-time thumbrule: tc = 6*ell\n"); globalSetup->maxGenerations = 6*(globalSetup->noOfDecisionVariables); if((globalSetup->maxGenerations)%10) globalSetup->maxGenerations += 10-(globalSetup->maxGenerations)%10; printf("The maximum number of generations set is: %d\n", globalSetup->maxGenerations); } else if ((globalSetup->maxGenerations = atoi(pToken)) <= 0) { fclose(fInput); printf("Error! maximum number of generations must be > 0\n"); exit(1); } // replace proportion // the number should be in (0.0, 1.0] 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) { // Setting a default value of 0.9 printf("Using default replacement proportion of 0.9\n"); globalSetup->replaceProportion = 0.9; } else if (((globalSetup->replaceProportion = atof(pToken)) <= 0.0) || (globalSetup->replaceProportion > 1.0)) { fclose(fInput); printf("Error! proportion of parent population that should be replaced must be > 0 and <= 1\n"); exit(1); } // niching (multimodal handling) 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) { // Using NoNiching by default printf("No niching method is used by default\n"); globalSetup->nichingType = NoNiching; } // niching type else if (strcmp("NoNiching", pToken) == 0) { globalSetup->nichingType = NoNiching; globalSetup->nichingParameters = NULL; } else if (strcmp("Sharing", pToken) == 0) { globalSetup->nichingType = Sharing; } else if (strcmp("RTS", pToken) == 0) { globalSetup->nichingType = RTS; } else if (strcmp("DeterministicCrowding", pToken) == 0) { globalSetup->nichingType = DeterministicCrowding; } else { fclose(fInput); printf("Error! valid niching types are: NoNiching, Sharing, RTS, and DeterministicCrowding\n"); exit(1); } // check niching type if ((globalSetup->gaType == NSGA) && (globalSetup->nichingType != NoNiching)) { fclose(fInput); printf("Error! valid choice for niching types with NSGA is: NoNiching\n"); exit(1); } // read niching parameters switch(globalSetup->nichingType) { case NoNiching: case DeterministicCrowding: // no extra parameters break; case Sharing: { globalSetup->nichingParameters = new double[2]; if ((pToken = strtok(NULL, BLANK_STR)) == NULL) { printf("Using default sharing radius of 4.24\n"); ((double *)(globalSetup->nichingParameters))[0] = 4.24; } else ((double *)globalSetup->nichingParameters)[0] = atof(pToken); if (((double*)globalSetup->nichingParameters)[0] <= 0.0) { fclose(fInput); printf("Error! niching radius must be > 0\n");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -