/* * Katie Lewis * Spring 2004 * Note: In codeing this I referred to code examples * from Professor Kuenning and Matt Gnaizda. */ #include #include #include #include #include #include #include "location.hh" #include "node.hh" #include "plant.hh" #include "plantiterator.hh" const int LOCATION_OF_INPUT = 1; /* Table Of Contents: Methods in this file: **********************************/ int main(int, char*); Plant& createRandomPlant(Sun suntype, LeafShape shape, LeafFunction leafFunction, StructFunction structFunction, int minBranching, int maxBranching); int findNull(Plant**& plantset, int& numplants); int findPlant(Plant**& plantset, int& numplants); void evolution(Plant**& plantset, int numplants, int numEvoIterations, int popCullAt, int popCullTo); void orderFitness(Plant** plantset, int numplants, int sizePlantArray); void print(Plant** plantset, const int& numplants); /* Main function ***********************************************************/ int main(int, char*) { // Set parameters Sun sunType = DOWN; LeafShape shape = EASY; LeafFunction leafFunction = A; StructFunction structFunction = X; int minBranching = 0; int maxBranching = 10; int numEvoIterations = 100; int popCullAt = 100; int popCullTo = 50; int initNumPlants = popCullTo; // Read in input: Does the user want to set any of the inputs /* ifstream inputStream(argv[LOCATION_OF_INPUT]); if (inputStream) { int i = 0; inputStream >> i; if (i == 1) inputStream >> sunType; inputStream >> i; if (i == 1) inputStream >> shape; inputStream >> i; if (i == 1) inputStream >> leafFunction; inputStream >> i; if (i == 1) inputStream >> structFunction; inputStream >> i; if (i == 1) inputStream >> minBranching; inputStream >> i; if (i == 1) inputStream >> maxBranching; inputStream >> i; if (i == 1) inputStream >> NumEvoIterations; inputStream >> i; if (i == 1) inputStream >> popCullAt; inputStream >> i; if (i == 1) inputStream >> popCullTo; } */ /* Testing that everything works --------------------------------------------*/ cout << "Create Plant" << endl; Plant* Z = &createRandomPlant( sunType, shape, leafFunction, structFunction, minBranching, maxBranching); cout << "Print Stats" << endl; Z->printStatistics(); cout << "Print Pov" << endl << endl; Z->printPovray(); delete Z; return 0; cout << "---Exit-(when testing)--------------------- " << endl; /* Create initial plant set -------------------------------------------------*/ Plant** plantset = new Plant*[popCullAt]; int numplants = 0; for (numplants = 0; numplants < initNumPlants; ++numplants) { plantset[numplants] = &createRandomPlant( sunType, shape, leafFunction, structFunction, minBranching, maxBranching); } /* Copy the initial set for comparison purposes -----------------------------*/ Plant** initialplantset = new Plant*[popCullAt]; for (int i = 0; i < initNumPlants; ++i) { initialplantset[i] = new Plant(*plantset[i]); } /* Run evolution ------------------------------------------------------------*/ evolution(plantset, numplants, numEvoIterations, popCullAt, popCullTo); /* Return results -----------------------------------------------------------*/ print(plantset, numplants); delete[] initialplantset; delete[] plantset; return 0; } /* Methods *******************************************************************/ Plant& createRandomPlant(Sun suntype, LeafShape shape, LeafFunction leafFunction, StructFunction structFunction, int minBranching, int maxBranching) { cout << "Entering create random plant" << endl; Plant* plant = new Plant(suntype, shape, leafFunction, structFunction, minBranching, maxBranching); cout << "Plant created" << endl; int i = rand()%1000; for (int j =0; j < i; ++j) { cout << j << " " << endl; plant->randomAddAnywhere(); } return *plant; } int findNull(Plant**& plantset, int& numplants) { for (int i = 0; i < numplants; ++i) { if (plantset[i] == NULL) return i; } return 0; } int findPlant(Plant**& plantset, int& numplants) { int z1 = rand()%numplants; int w1 = 0; for (int i; w1 < numplants; ++i) { if (plantset[i] != NULL) { if (w1 == z1) return i; ++w1; } } return 0; } void evolution(Plant**& plantset, int numplants, int numEvoIterations, int popCullAt, int popCullTo) { /* Generate plants */ while (numplants < popCullAt) { int j =rand()%3; switch(j) { case 1: // mutate1 plantset[findNull(plantset, numplants)] = &(plantset[findPlant(plantset, numplants)]->mutate1()); break; case 2: plantset[findPlant(plantset, numplants)]-> randomAddAnywhere(); // case 3: // plantset[findNull(plantset, numplants)] = // &(plantset[findPlant(plantset, numplants)]-> // removeRandomAnywhere()); default: // cross plantset[findNull(plantset, numplants)] = &(plantset[findPlant(plantset, numplants)]-> cross(*plantset[findPlant(plantset, numplants)])); } } /* Cull */ while (numplants > popCullTo) { //note: rather than cull the whole thing, might take some random subset and // cull that. This would allow unuusal things to have some nonzero chance // of living longer. orderFitness (plantset, numplants, popCullAt); for (int i = popCullTo; i < popCullAt; ++i) { if (plantset[i] != NULL) { delete plantset[i]; plantset[i] = NULL; --numplants; } } } if (numEvoIterations > 0) evolution(plantset, numplants, numEvoIterations -1, popCullAt, popCullTo); } void orderFitness(Plant** plantset, int numplants, int sizePlantArray) { double* orderfitness = new double[numplants]; for (int i = 0; i < numplants; ++i) { if (plantset[i] != NULL) orderfitness[i] = plantset[i]->findFitness(); } for (int j = 0; j+1 < numplants; ++j) { for (int i = 0; i+1 < numplants; ++i) { if (plantset[i] == NULL || (plantset[i+1] != NULL && orderfitness[i] < orderfitness[i+1])) { double tempOfit = orderfitness[i+1]; Plant* tempplant = plantset[i+1]; orderfitness[i+1] = orderfitness[i]; plantset[i+1] = plantset[i]; orderfitness[i] = tempOfit; plantset[i] = tempplant; } } } } void print(Plant** plantset, const int& numplants) { int j = 0; for (int i = 0; j < numplants; ++i) { if (plantset[i] != NULL) { plantset[i]->printStatistics(); ++j; } } }