Changeset 30


Ignore:
Timestamp:
08/24/09 23:00:02 (15 years ago)
Author:
mwajcht
Message:

Added mutation which deletes a production.

Location:
cpp/f8-to-f1
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/f8-to-f1/geno_f8.cpp

    r24 r30  
    2525{
    2626{"Genetics: f8",1,F8_OPERATION_COUNT,},
    27 {"f8_mut_chg_begin_arg", 0, 0, "Change beginning argument", "f 0 100 8", FIELD(operation[F8_CHANGE_BEGINNING_ARG]),"mutation: probability of changing a beginning argument", },
    28 {"f8_mut_chg_arg", 0, 0, "Change argument", "f 0 100 8", FIELD(operation[F8_CHANGE_ARG]),"mutation: probability of changing a production's argument", },
     27{"f8_mut_chg_begin_arg", 0, 0, "Change beginning argument", "f 0 100 7", FIELD(operation[F8_CHANGE_BEGINNING_ARG]),"mutation: probability of changing a beginning argument", },
     28{"f8_mut_chg_arg", 0, 0, "Change argument", "f 0 100 7", FIELD(operation[F8_CHANGE_ARG]),"mutation: probability of changing a production's argument", },
    2929{"f8_mut_del_comm", 0, 0, "Delete command", "f 0 100 8", FIELD(operation[F8_DELETE_COMMAND]),"mutation: probability of deleting a command", },
    30 {"f8_mut_insert_comm", 0, 0, "Insert commands", "f 0 100 9", FIELD(operation[F8_INSERT_COMMANDS]),"mutation: probability of inserting commands", },
    31 {"f8_mut_enc", 0, 0, "Encapsulate commands", "f 0 100 10",FIELD(operation[F8_ENCAPSULATE]),"mutation: probability of encapsulating commands", },
    32 {"f8_mut_chg_cond_sign", 0, 0, "Change condition sign", "f 0 100 8",FIELD(operation[F8_CHANGE_CONDITION_SIGN]),"mutation: probability of changing a condition sign", },
    33 {"f8_mut_add_param", 0, 0, "Add parameter", "f 0 100 9", FIELD(operation[F8_ADD_PARAMETER]),"mutation: probability of adding a parameter to the production", },
     30{"f8_mut_insert_comm", 0, 0, "Insert commands", "f 0 100 8", FIELD(operation[F8_INSERT_COMMANDS]),"mutation: probability of inserting commands", },
     31{"f8_mut_enc", 0, 0, "Encapsulate commands", "f 0 100 8",FIELD(operation[F8_ENCAPSULATE]),"mutation: probability of encapsulating commands", },
     32{"f8_mut_chg_cond_sign", 0, 0, "Change condition sign", "f 0 100 7",FIELD(operation[F8_CHANGE_CONDITION_SIGN]),"mutation: probability of changing a condition sign", },
     33{"f8_mut_add_param", 0, 0, "Add parameter", "f 0 100 8", FIELD(operation[F8_ADD_PARAMETER]),"mutation: probability of adding a parameter to the production", },
    3434{"f8_mut_add_cond", 0, 0, "Add condition", "f 0 100 8", FIELD(operation[F8_ADD_CONDITION]),"mutation: probability of ading a condition to the subproduction", },
    3535{"f8_mut_add_subprod", 0, 0, "Add subproduction", "f 0 100 8", FIELD(operation[F8_ADD_SUBPRODUCTION]),"mutation: probability of adding a subproduction", },
    36 {"f8_mut_chg_iter_number", 0, 0, "Change iteration number", "f 0 100 8", FIELD(operation[F8_CHANGE_ITERATIONS_NUMBER]),"mutation: probability of changing an iterations number", },
     36{"f8_mut_chg_iter_number", 0, 0, "Change iteration number", "f 0 100 7", FIELD(operation[F8_CHANGE_ITERATIONS_NUMBER]),"mutation: probability of changing an iterations number", },
    3737{"f8_mut_del_param", 0, 0, "Delete parameter", "f 0 100 8", FIELD(operation[F8_DELETE_PARAMETER]),"mutation: probability of deleting a parameter", },
    3838{"f8_mut_del_cond", 0, 0, "Delete condition", "f 0 100 8", FIELD(operation[F8_DELETE_CONDITION]),"mutation: probability of deleting a condition", },
    3939{"f8_mut_add_loop", 0, 0, "Add loop", "f 0 100 0", FIELD(operation[F8_ADD_LOOP]),"mutation: probability of ading a loop", },
    4040{"f8_mut_del_loop", 0, 0, "Delete loop", "f 0 100 0", FIELD(operation[F8_DELETE_LOOP]),"mutation: probability of deleting a loop", },
     41{"f8_mut_del_prod", 0, 0, "Delete production", "f 0 100 8", FIELD(operation[F8_DELETE_PRODUCTION]),"mutation: probability of deleting a production", },
    4142{0,},
    4243};
     
    555556}
    556557
     558SString Geno_f8::mutateDeleteProduction(SString &in, float& chg) {
     559        SString mutated;
     560       
     561        Lsystem *lsystem = this->converter->createLsystem(in);
     562        if (lsystem == NULL || lsystem->productions.size() == 0) {
     563                mutated += in;
     564                chg = -1.0;
     565                return mutated;
     566        }
     567       
     568        if (lsystem->productions.size() < 2) {
     569                mutated += in;
     570                chg = 0.0;
     571                return mutated;
     572        }
     573       
     574        int counter = 0;
     575        vector<SString> productionNamesSS = this->converter->readProductionNames(in);
     576        vector<string> productionNames;
     577        for (int ssI = 0; ssI < productionNamesSS.size(); ssI++) {
     578                productionNames.push_back(sstringToString(productionNamesSS[ssI]));
     579        }
     580        vector<string>::iterator prodNameIter = productionNames.begin();
     581        while (prodNameIter != productionNames.end()) {
     582                //delete first production from candidate list
     583                if ((*prodNameIter).compare(lsystem->firstProductionName) == 0) {
     584                        prodNameIter = productionNames.erase(prodNameIter);
     585                } else {
     586                        prodNameIter++;
     587                }
     588        }
     589        //choose production to delete
     590        string prodNameToDelete = productionNames[randomN(productionNames.size())];
     591        productionNames.push_back(lsystem->firstProductionName);
     592        prodNameIter = productionNames.begin();
     593        while (prodNameIter != productionNames.end()) {
     594                //delete prodNameToDelete from candidate list
     595                if ((*prodNameIter).compare(prodNameToDelete) == 0) {
     596                        prodNameIter = productionNames.erase(prodNameIter);
     597                } else {
     598                        prodNameIter++;
     599                }
     600        }
     601        //choose production to which we will append the contents of deleted production
     602        string prodNameToAppendTo = productionNames[randomN(productionNames.size())];
     603       
     604        int len = 0;
     605       
     606        Production* deletedProd = lsystem->productions[prodNameToDelete];
     607        Production* appendProd = lsystem->productions[prodNameToAppendTo];
     608        //add missing parameters
     609        for (int i = 1; i <= deletedProd->parameters.size(); i++) {
     610                SString paramName = deletedProd->parameters.getParameterName(i);
     611                if (appendProd->parameters.paramExist(paramName) == false) {
     612                        appendProd->parameters.addParameter(paramName);
     613                }
     614        }
     615       
     616        //copy subproductions
     617        for (vector<SubProduction>::iterator subprodIter = deletedProd->subproductions.begin();
     618                 subprodIter != deletedProd->subproductions.end(); subprodIter++) {
     619                SubProduction sp;
     620                //copy conditions
     621                for (vector<Condition>::iterator condIter = (*subprodIter).conditions.begin();
     622                         condIter != (*subprodIter).conditions.end(); condIter++) {
     623                        Condition *cFrom = &(*condIter);
     624                        Condition cTo;
     625                        cTo.parameter = SString(cFrom->parameter);
     626                        cTo.relation = cFrom->relation;
     627                        cTo.value = cFrom->value;
     628                        sp.conditions.push_back(cTo);
     629                }
     630                for (vector<ActionStrP>::iterator actionIter = (*subprodIter).actions.begin();
     631                         actionIter != (*subprodIter).actions.end(); actionIter++) {
     632                        ActionStrP aFrom = *actionIter;
     633                        ActionStrP aTo;
     634                        aTo.action = aFrom.action;
     635                        aTo.params = vector<SString>(aFrom.params);
     636                        sp.actions.push_back(aTo);
     637                }
     638                appendProd->subproductions.push_back(sp);
     639        }
     640        lsystem->productions.erase(string(prodNameToDelete));
     641       
     642        int paramCount = appendProd->parameters.size();
     643        for (map<string, Production*>::iterator prodIter = lsystem->productions.begin();
     644                 prodIter != lsystem->productions.end(); prodIter++) {
     645                for (vector<SubProduction>::iterator subprodIter = prodIter->second->subproductions.begin();
     646                         subprodIter != prodIter->second->subproductions.end(); subprodIter++) {
     647                        for (vector<ActionStrP>::iterator actionIter = (*subprodIter).actions.begin();
     648                                 actionIter != (*subprodIter).actions.end(); actionIter++) {
     649                                if ((*actionIter).action != NULL && (*actionIter).action->name != NULL) {
     650                                        if ((*actionIter).action->name == stringToSString(prodNameToDelete)) {
     651                                                (*actionIter).action = appendProd;
     652                                                (*actionIter).params = vector<SString>(paramCount, SString("0;"));
     653                                        }
     654                                        if ((*actionIter).action->name == stringToSString(prodNameToAppendTo)) {
     655                                                (*actionIter).action = appendProd;
     656                                                SString paramVal = "0;";
     657                                                for (int i = (*actionIter).params.size(); i < paramCount; i++) {
     658                                                        (*actionIter).params.push_back("0;");
     659                                                }
     660                                        }
     661                                       
     662                                }
     663                        }
     664                }
     665        }
     666        delete deletedProd;
     667               
     668       
     669        mutated = lsystem->toString();
     670       
     671        delete lsystem;
     672       
     673        chg = (float) len / (float) in.len();
     674        return mutated;
     675}
     676
    557677SString Geno_f8::mutateChangeConditionSign(SString &in, float& chg) {
    558678        SString mutated;
     
    10161136                case F8_DELETE_LOOP:
    10171137                        ssMutatedTmp = this->mutateDeleteLoop(in, chg);
     1138                        break;
     1139                case F8_DELETE_PRODUCTION:
     1140                        ssMutatedTmp = this->mutateDeleteProduction(in, chg);
    10181141                        break;
    10191142                default:
  • cpp/f8-to-f1/geno_f8.h

    r6 r30  
    3535#define F8_ADD_LOOP 12
    3636#define F8_DELETE_LOOP 13
    37 #define F8_OPERATION_COUNT 14
     37#define F8_DELETE_PRODUCTION 14
     38#define F8_OPERATION_COUNT 15
    3839
    3940class ProductionInfo {
     
    8384        //SString mutateInsertReplaceCommand(SString &in, float& chg, bool replace);
    8485        SString mutateEncapsulate(SString &in, float& chg);
     86        SString mutateDeleteProduction(SString &in, float& chg);
    8587        SString mutateChangeConditionSign(SString &in, float& chg);
    8688        SString mutateAddParameter(SString &in, float& chg);
Note: See TracChangeset for help on using the changeset viewer.