Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef GENETIC_H
- #define GENETIC_H
- #include <ctime>
- #include "container.h"
- #include <string.h>
- class Genetic
- {
- private:
- double capacity;
- vector< vector<Container> > primaryPopulation;
- Container *currentContainer;
- vector<Container> containers;
- vector<double> missedElements;
- vector<int> indexBinsWithMissedElements;
- int indexPopulationBestCostFunction;
- double getCostFunction(vector<Container> vv) {
- double s = 0;
- cout << "------------- " << endl;
- for(int j = 0; j < vv.size(); j++) {
- s = s + (vv[j].getWorkload() / capacity)*(vv[j].getWorkload() / capacity);
- }
- return s / vv.size();
- }
- public:
- Genetic(vector<double> initialObj, double binCapacity) {
- capacity = binCapacity;
- srand(time(0));
- //genera init population
- for(int i = 0; i < 10; i++) {
- generateInitPopulation(initialObj);
- containers.clear();
- }
- for(int j = 0; j < 9; j++) {
- Crossover(primaryPopulation[j], primaryPopulation[j+1]);
- missedElements.clear();
- indexBinsWithMissedElements.clear();
- }
- // cout << endl << "after crossover " << endl;
- // cout << " size of new population = " << primaryPopulation.size() << endl;
- findBestCostFunction();
- }
- void findBestCostFunction() {
- double valueCostFunction = getCostFunction(primaryPopulation[0]);
- indexPopulationBestCostFunction = 0;
- for(int i = 1; i < primaryPopulation.size(); i++) {
- double currentCostF = getCostFunction(primaryPopulation[i]);
- if(currentCostF > valueCostFunction) {
- indexPopulationBestCostFunction = i;
- valueCostFunction = currentCostF;
- }
- }
- // showContainers(100000, primaryPopulation[indexPopulationBestCostFunction]);
- }
- int getBestCountOfContainers(){
- return primaryPopulation[indexPopulationBestCostFunction].size();
- }
- void someFunction(vector<Container> vv, vector<Container> vv2) {
- cout << "some function " << endl;
- showContainers(9, vv);
- showContainers(11, vv);
- }
- void Crossover(vector<Container> parent1, vector<Container> parent2) {
- int point1_1 = rand() % (parent1.size()-1) + 1;
- int point1_2 = rand() % (parent1.size()-1) + 1;
- int point2_1 = rand() % (parent2.size()-1) + 1;
- int point2_2 = rand() % (parent2.size()-1) + 1;
- if(point1_1 > point1_2) swap(point1_1, point1_2);
- if(point2_1 > point2_2) swap(point2_1, point2_2);
- cout << endl;
- cout << "point1_1: " << point1_1 << " point1_2: " << point1_2 << endl;
- cout << "point2_1: " << point2_1 << " point2_2: " << point2_2 << endl;
- //Push random bins into chromose1
- int previousSize = parent1.size();
- for(int i = 0; i < parent2.size(); i++) {
- if(i >= point2_1 && i <= point2_2) {
- parent1.push_back(parent2[i]);
- }
- }
- for(int i = previousSize; i < parent1.size(); i++) {
- for(int k = 0; k < parent1[i].getCountOfElements(); k++){
- Item itemFromNewBin(parent1[i].getElementByID(k));
- for(int j = 0; j < previousSize; j++){
- //цикл по элементам
- for(int m = 0; m < parent1[j].getCountOfElements(); m++) {
- Item item2(parent1[j].getElementByID(m));
- if(itemFromNewBin.getWeight() == item2.getWeight()) {
- parent1[j].DeleteElementFromContainer(item2);
- indexBinsWithMissedElements.push_back(j);
- //move to next element in part2
- goto metka;
- }
- }
- }
- metka:
- cout << endl;
- }
- }
- sort( indexBinsWithMissedElements.begin(), indexBinsWithMissedElements.end() );
- indexBinsWithMissedElements.erase( unique( indexBinsWithMissedElements.begin(), indexBinsWithMissedElements.end() ), indexBinsWithMissedElements.end() );
- for(int z = 0; z < indexBinsWithMissedElements.size(); z++) {
- cout << indexBinsWithMissedElements[z] << " ";
- }
- cout << endl << "---------------------------------------------- " << endl;
- //Получить упущенные элементы
- for(int i = 0; i < indexBinsWithMissedElements.size(); i++) {
- for(int k = 0; k < parent1[indexBinsWithMissedElements[i]].getCountOfElements(); k++)
- {
- //Add to new array
- Item item(parent1[indexBinsWithMissedElements[i]].getElementByID(k));
- missedElements.push_back(item.getWeight());
- }
- }
- cout << "missed elements " << endl;
- for(int i = 0; i < missedElements.size(); i++) {
- cout << missedElements[i] << " ";
- }
- cout << endl << "---------------------------------------------- " << endl;
- //Delete empty containers
- for(int i = 0; i < indexBinsWithMissedElements.size(); i++) {
- parent1.erase(parent1.begin() + indexBinsWithMissedElements[i]);
- for(int j = 0; j < indexBinsWithMissedElements.size(); j++) {
- indexBinsWithMissedElements[j] -= 1;
- }
- }
- cout << endl << "before FFD (empty containers has been deleted" << endl;
- showContainers(5, parent1);
- bool execute;
- while(missedElements.size() > 0) {
- execute = false;
- m2:
- for(int i = 0; i < missedElements.size(); i++) {
- Item item(missedElements[i]);
- for(int j = 0; j < parent1.size(); j++) {
- if(parent1[j].IsEnoughSpace(item)) {
- parent1[j].PutElementInContainer(item);
- missedElements.erase(missedElements.begin() + i);
- cout << "move " << endl;
- execute = true;
- goto m2;
- } else {
- //swap elements
- for(int m = 0; m < parent1[j].getCountOfElements(); m++) {
- Item item2( parent1[j].getElementByID(m));
- if(item.getWeight() > item2.getWeight()) {
- if(PossibleToExchange(parent1[j], item2.getWeight(), item.getWeight())) {
- parent1[j].DeleteElementFromContainer(item2);
- parent1[j].PutElementInContainer(item);
- missedElements.erase(missedElements.begin() + i);
- missedElements.push_back(item2.getWeight());
- cout << "swap " << endl;
- execute = true;
- goto m2;
- }
- }
- }
- }
- }
- }
- if(execute == false) break;
- }
- cout << endl << "after FFD " << endl;
- showContainers(7, parent1);
- cout << "missed elements " << endl;
- for(int i = 0; i < missedElements.size(); i++) {
- cout << missedElements[i] << " ";
- }
- //add missed element to parent1
- if(missedElements.size() > 0) {
- cout << "missed elements exist!!!!!" << endl;
- currentContainer = new Container(capacity);
- for(int i = 0; i < missedElements.size(); i++) {
- Item item(missedElements[i]);
- if(currentContainer->IsEnoughSpace(item)) {
- currentContainer->PutElementInContainer(item);
- } else {
- //save current container
- parent1.push_back(*currentContainer);
- currentContainer = new Container(capacity);
- }
- }
- if (currentContainer->getWorkload() > 0) {
- parent1.push_back(*currentContainer);
- }
- }
- showContainers(9, parent1);
- primaryPopulation.push_back(parent1);
- DeleteWorstSetOfBins();
- }
- bool PossibleToExchange(Container c, Item itemInContainer, Item item2) {
- c.DeleteElementFromContainer(itemInContainer);
- if(c.PutElementInContainer(item2) == true) {
- return true;
- } else {
- return false;
- }
- }
- template <typename T>
- void swap(T &a, T &b) {
- T temp = a;
- a = b;
- b = temp;
- }
- void DeleteWorstSetOfBins() {
- double costF = getCostFunction(primaryPopulation[0]);
- int index = 0;
- for(int i = 1; i < primaryPopulation.size(); i++) {
- double cf = getCostFunction(primaryPopulation[i]);
- if(cf < costF) {
- index = i;
- }
- }
- primaryPopulation.erase(primaryPopulation.begin() + index);
- }
- void generateInitPopulation(vector<double> initialObj) {
- int index;
- currentContainer = new Container(capacity);
- while (initialObj.size() > 0) {
- if (initialObj.size() > 1)
- index = rand() % (initialObj.size() - 1);
- else index = 0;
- Item item(initialObj[index]);
- if (currentContainer->IsEnoughSpace(item)) {
- currentContainer->PutElementInContainer(item);
- initialObj.erase(initialObj.begin() + index);
- }
- else {
- //save current container
- containers.push_back(*currentContainer);
- //add new container
- currentContainer = new Container(capacity);
- }
- }
- if (currentContainer->getWorkload() > 0) {
- containers.push_back(*currentContainer);
- }
- primaryPopulation.push_back(containers);
- // showContainers(primaryPopulation.size()-1, containers);
- }
- void showContainers(int nCont, vector<Container> v) {
- cout << "container# " << nCont << endl;
- for (int i = 0; i < v.size(); i++) {
- cout << v[i].getAmountOfFreeSpace() << " "; v[i].showObjects(); cout << endl;
- }
- cout << "Count of containers " << v.size() << endl;
- cout << "Cost function " << getCostFunction(v) << endl;
- }
- void showR() {
- showResult(primaryPopulation[indexPopulationBestCostFunction]);
- }
- void showResult(vector<Container> v) {
- cout << endl;
- for(int i = 0; i < v.size(); i++) {
- v[i].showObjects();
- }
- }
- };
- #endif // GENETIC_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement