befehl shuffle funkioniert nicht mit einer list<list>>Tabelle in c++



  • Hallo ich möchte gerne in der funktion simulated annealing gleich über main die list<list<offer>> mit shuffle bearbeiten leider bekomme ich dabei einen auschnitt aus der standartbibiothek <algorithm angezeigt. Was ist hier der fehler?? lieben gruss

    #include <iostream>
    #include<fstream>
    #include<vector>
    #include<string>
    #include<cstdlib>
    #include<list>
    #include<map>
    #include<iterator>
    #include<cmath>
    #include<time.h>
    #include<algorithm>
    
    /*void showSequence(list<int> s, const char* abschluss ="\n", const char* trennzeichen = " ")
    {
        auto iter = s.begin();
        while(iter != s.end())
        {
            cout << *iter++ << trennzeichen;
        }
        cout << abschluss;
    }*/
    
    using namespace std;
    int L;
    double K=0.002;
    double A=25;
    double I=100;
    int N =10;
    int ACCEPT;
    double T0=300;
    double T=0;
    const double dex =0.001;
    double NS= T0/K;
    float CL= NS/(10*dex);
    unsigned long limit =(unsigned long)(CL);
    
    //Functionprototypes
    double fx(double x);      //Cooling function
    
    double probability(double len, double T);  //calculates probability of a list
    
    int dim, cap, length, st, fs;
    float bks;
    
    //depot
    float dx,dy;
    
    //customers [x,y,d]
    struct Customer
    {
    	float x,y;
    	int d;
    };
    
    vector<Customer> customers;
    double lenge (vector<Customer> z);
    
    //routes
    list <list <int> > routes;
    
    // structure of two nodes
    struct Nodes
    {
    	int c1, c2;
    };
    
    //list of savings
    multimap<double, Nodes> listSav;
    
    void readDataFile()
    {
    	string tmp;
    	ifstream dataFile("vrpnc1.txt");
    	float cx,cy;
    	int cd;
    
    	dataFile >> dim >> cap >> length >> st ;
    	dataFile >> dx >> dy;
    
    	for (int i = 0; i < dim; i++)
    	{
    		dataFile >> cx;
    		dataFile >> cy;
    		dataFile >> cd;
    		if (!dataFile.fail())
    		{
    			Customer c;
    			c.x = cx;
    			c.y = cy;
    			c.d = cd;
    			customers.push_back(c);
    		}
    	}
    	dataFile.close();
    }
    
    float dist(float x1, float y1, float x2, float y2)
    {
    	return (float) sqrt(pow(x1-x2, 2) + pow(y1-y2, 2)+st);
    }
    
    float dist(const Customer& c1, const Customer& c2)
    {
    	return dist(c1.x,c1.y,c2.x,c2.y);
    }
    
    void calcSavings()
    {
    	Nodes node;
    
    	for(int i = 0; i < dim; i++)
    	{
    		for(int j = i+1; j < dim; j++)
    		{
    			if(i != j)
    			{
    				node.c1 = i;
    				node.c2 = j;
    				listSav.insert(make_pair(-(dist(dx,dy,customers[i].x, customers[i].y) + dist(dx,dy,customers[j].x,customers[j].y) - dist(customers[i].x, customers[i].y, customers[j].x, customers[j].y)), node));
    			}
    		}
    	}
    }
    
    void initRoutes()
    {
    	//Initial routes
    	for(int i = 0; i < dim; i++)
    	{
    		list<int> route;
    		//route.push_back(0);
    		route.push_back(i);
    		//route.push_back(0);
    		routes.push_back(route);
    	}
    }
    
    void clearRoutes(int c)
    {
    	for(int i = 0; i < dim; i++)
    	{
    
    	}
    }
    
    //returns true if we can merge 2 routes
    bool capOk(list<int> route1, list<int> route2)
    {
    	int d = 0;
    	for(list<int>::iterator it = route1.begin(); it != route1.end(); it++)
    	{
    		d += customers[*it].d;
    	}
    	for(list<int>::iterator it = route2.begin(); it != route2.end(); it++)
    	{
    		d += customers[*it].d;
    	}
    	return d <= cap;
    }
    
    float calcRouteDist(list<int> r)
    {
    	list<int>::iterator it1 = r.begin();
    	list<int>::iterator it2 = r.begin();
    	float distance = 0;
    	it2++;
    	while(it2 != r.end())
    	{
    		distance += dist(customers[*it1], customers[*it2]);
    		it2++;
    		it1++;
    	}
    	distance += dist(customers[r.front()].x, customers[r.front()].y,dx,dy);
    	distance += dist(customers[r.back()].x, customers[r.back()].y,dx,dy);
    	return distance;
    }
    
    float calcRoutesDist(list<list<int> > r)
    {
    	float distance = 0;
    	for(list<list<int> >::iterator it = r.begin(); it != r.end(); it++)
    	{
    		distance += calcRouteDist(*it);
    	}
    	return distance;
    }
    
    bool RouteDistOk(list<list<int> > r)
    {
    	float distance = 0;
    	for(list<list<int> >::iterator it = r.begin(); it != r.end(); it++)
    	{
    		distance += calcRouteDist(*it);
    	}
    	return distance<=length;
    }
    
    void showRoutes(list<list<int> > l)
    {
        /*int d_row = l.size();
        int d_col = (*l.begin()).size();
    
        for(size_t r = 0; r < d_row; ++r)
        {
            for(size_t c = 0;c < d_col;++c)
            {
                cout << l[r][c] << " ";
            }
        }*/
    	/*for(list<list<int> >::iterator it = r.begin(); it != r.end(); it++)
    	{
    		cout << r[it*] << " ";
    	}*/
    	for(list<list<int> >::iterator it1 = l.begin(); it1 != l.end(); it1++)
    	{
    		cout << "0 ";
    		for(list<int>::iterator it2 = it1->begin(); it2 != it1->end(); it2++)
    		{
    			cout << *it2+1 << " ";
    		}
    		cout << "0" << endl;
    	}
    }
    
    //merge 2 routes starting with 0,cx ... and ending with ..,cy,0 if sum of demand is lower or equal the capacity
    void mergeRoutes(int cx, int cy)
    {
    	list<int> route1;
    	list<int> route2;
    	list<list<int> >::iterator it1;
    	list<list<int> >::iterator it2;
    	bool b1 = false, b2 = false;
    	for(list<list<int> >::iterator it = routes.begin(); it != routes.end(); it++)
    	{
    		if(!b1 && it->front() == cx)
    		{
    			it1 = it;
    			route1 = *it;
    			b1 = true;
    		} else if(!b1 && it->front() == cy)
    		{
    			it1 = it;
    			route1 = *it;
    			b1 = true;
    		} else if(!b2 && it->back() == cx)
    		{
    			it2 = it;
    			route2 = *it;
    			b2 = true;
    		} else if(!b2 && it->back() == cy)
    		{
    			it2 = it;
    			route2 = *it;
    			b2 = true;
    		}
    
    		if(b1 && b2)
    		{
    			//If we can not merge theses 2 routes we keep on searching
    
                cout << capOk(route1, route2) << ", " << RouteDistOk(routes)<< endl;
    
    			if(!capOk(route1, route2) || !RouteDistOk(routes))
    			{
    				b1 = b2 = false;
    				continue;
    			}
    			//delete the 2 routes
    			routes.erase(it1);
    			routes.erase(it2);
    
    			//add 1 merged route  (route1 - route2)
    			list<int> newRoute;
    			newRoute.insert(newRoute.end(), route2.begin(), route2.end());
    			newRoute.insert(newRoute.end(), route1.begin(), route1.end());
    			/*if(route1.size() > 1)
    			{
    				list<int>::iterator it = route1.begin();
    				it++;
    				while(it != route1.end() )
    				{
    					clearRoutes(*it);
    					it++;
    				}
    			}
    			if(route2.size() > 1)
    			{
    				list<int>::iterator it = route2.begin();
    				it++;
    				while(it != route2.end() )
    				{
    					clearRoutes(*it);
    					it++;
    				}
    			}
    			*/
    			routes.push_front(newRoute);
    			return;
    		}
    	}
    }
    
    void simulatedAnnealing()
    {
      srand(time(NULL));
        double xpos=0;
        double ypos=0;
        double rho=0;
        double r=0;
        string dummy;
        string verbose[10];
        double len=0;
        double aux=0;
        double x=0;
        int counter=0;
        int h,h1=0;
        vector<float>d;
        vector<float>e;
        //vector<float>angebot;
        list <list <int> > offer;
        vector<float>momentan;
        vector<float>start;
        vector<float>loesung;
    
          copy(routes.begin(),routes.end(),back_inserter(offer));
    
          cout << "original:" << endl;
          showRoutes(routes);
          cout << "kopie:" << endl;
          showRoutes(offer);
    
        for(x=0;x<dim-1;x+=dx)
        {
            T=fx(x);
            loesung.clear();
    }
    random_shuffle(offer.begin(),offer.end());
    cout<<"Nach zufallsgenerator";
    showRoutes(offer);
    }
    int main()
    {
    	float distance;
    	int demands;
    	int custs;
    	readDataFile();
    	calcSavings();
    	initRoutes();
    
    	//as long as there are arcs we did not consider
    
    	for(multimap<double, Nodes>::iterator it = listSav.begin(); it != listSav.end(); it++)
    	{
    		if(it->first < 0)
    		{
    			mergeRoutes(it->second.c1, it->second.c2);
    		}
    	}
    
    	distance = calcRoutesDist(routes);
    
    	cout << "nr of routes: " << routes.size() << endl;
    	cout << "distance: " << distance << endl;
    	cout << "routes: " << endl;
    
    	custs = 0;
    	for(list<list<int> >::iterator it1 = routes.begin(); it1 != routes.end(); it1++)
    	{
    		demands = 0;
    		cout << "0 ";
    		custs += it1->size();
    		for(list<int>::iterator it2 = it1->begin(); it2 != it1->end(); it2++)
    		{
    			cout << *it2+1 << " ";
    			demands += customers[*it2].d;
    		}
    		distance = calcRouteDist(*it1);
    		cout << "0; demands: " << demands << " distance: " << distance << endl;
    	}
    
    	cout << "number of customers in routes: " << custs << endl;;
    

    cout<<"retertoer+kgwg+rwowrttwetkoprokpeokeor"<<endl;
    return 0;
    }
    [code]



  • shuffel benötigt random access Iteratoren => Liste scheidet aus


Log in to reply