Ejemplo n.º 1
0
bool menu(){

	int wybor;
	cout << "\t\tProjektowanie efektywnych algorytmow" << endl << endl;
	cout << "Implementacja i analiza efektywnosci algorytmu programowania dynamicznego dla problemu komiwojazera." << endl << endl;
	cout << "1. Wczytaj z pliku \n";
	cout << "2. Uzyj domyslnego pliku \n";
	cout << "3. Wyswietl macierz sasiedztwa \n";
	cout << "4. Algorytm programowania dynamicznego \n";
	cout << "5. Koniec \n";

	cin >> wybor;
	string sciezka;
	fstream fileWithCitiesMatrix;
	Path p;

	switch (wybor){

	case 1:
		cout << endl << endl;
		cout << "Podaj sciezke do pliku dla problemu komiwojazera:\n";
		cin.clear();
		cin >> sciezka;

		
		fileWithCitiesMatrix.open(sciezka, ios::in);
		salesman.loadPaths(fileWithCitiesMatrix);
		fileWithCitiesMatrix.close();
		return true;

	case 2:

		sciezka = "komiwojazer1.txt";
		fileWithCitiesMatrix.open(sciezka, ios::in);
		salesman.loadPaths(fileWithCitiesMatrix);
		fileWithCitiesMatrix.close();
		return true;
	
	case 3:
		salesman.printPaths(cout);
		return true;

	case 4:
		p = Dyn.DynamicProgrammingAlgorithm(salesman);

		cout << "path value = " << p.value << endl;
		cout << "path: "<<p.path<<endl;
		return true;
		
	case 5:
		return false;
	default:
		menu();
	}
	return true;

}
Ejemplo n.º 2
0
void Rotation(GAIndividual<CodeVInt> &indivl, int starts, int ends, int nlMax)
{
	int i, j, z;
	int numDim = indivl.getNumDim();
	vector<int> NL(nlMax), visited(numDim);
	GAIndividual<CodeVInt> indivll[2];
	int point[2] = { starts, ends };
	TravellingSalesman *ptr = dynamic_cast<TravellingSalesman*>(Global::msp_global->mp_problem.get());
	for (i = 0; i<2; i++)
	{
		for (j = 0; j<numDim; j++)
			visited[j] = 0;
		for (j = 0; j<nlMax; j++)
		{
			double min = DBL_MAX;
			int pos;
			for (z = 0; z<numDim; z++)
			{
				if (min>ptr->getCost()[point[i]][z] && z != point[i] && visited[z] == 0)
				{
					min = ptr->getCost()[point[i]][z];
					pos = z;
				}
			}
			NL[j] = pos;
			visited[pos] = 1;
		}
		int flag, temp;
		j = 0;
		do
		{
			flag = 0;
			temp = Global::msp_global->getRandInt(0, nlMax - 1 - j);
			for (z = 0; z<numDim; z++)
			{
				if (indivl.data().m_x[z] == point[i] && (indivl.data().m_x[(z + 1) % numDim] == NL[temp] || indivl.data().m_x[(z - 1 + numDim) % numDim] == NL[temp]))
				{
					flag = 1;
					break;
				}
			}
			if (flag)
			{
				NL[temp] = NL[nlMax - 1 - j];
				j++;
			}
		} while (flag);
		indivll[i] = indivl;
		inverse(point[i], NL[temp], indivll[i]);
		indivll[i].evaluate();
	}
	if (indivll[0] > indivll[1])
		indivl = indivll[0];
	else
		indivl = indivll[1];
}
Ejemplo n.º 3
0
void Ant::modifiedAnt(Individual<CodeVInt> &parent)
{
	if (m_iteIndivl.data().m_x[m_loc] != m_x[m_loc])
	{
		double obj = m_iteIndivl.data().m_obj[0];
		TravellingSalesman *ptr = dynamic_cast<TravellingSalesman*>(Global::msp_global->mp_problem.get());
		int pos = -1;
		for (int i = m_loc + 1; i < getNumDim(); i++)
		{
			if (m_iteIndivl.data().m_x[i] == m_x[m_loc])
			{
				pos = i;
				break;
			}
		}
		obj = obj - ptr->getCost()[m_iteIndivl.data().m_x[pos]][m_iteIndivl.data().m_x[pos - 1]] - ptr->getCost()[m_iteIndivl.data().m_x[pos]][m_iteIndivl.data().m_x[(pos + 1) % getNumDim()]]\
			- ptr->getCost()[m_iteIndivl.data().m_x[m_loc]][m_iteIndivl.data().m_x[m_loc - 1]];
		obj = obj + ptr->getCost()[m_iteIndivl.data().m_x[pos]][m_iteIndivl.data().m_x[m_loc - 1]] + ptr->getCost()[m_iteIndivl.data().m_x[pos]][m_iteIndivl.data().m_x[m_loc]]\
			+ ptr->getCost()[m_iteIndivl.data().m_x[pos - 1]][m_iteIndivl.data().m_x[(pos + 1) % getNumDim()]];
		m_iteIndivl.data().m_obj[0] = obj;
		for (int i = pos - 1; i >= m_loc; i--)
		{
			m_iteIndivl.data().m_x[i + 1] = m_iteIndivl.data().m_x[i];
		}
		m_iteIndivl.data().m_x[m_loc] = m_x[m_loc];
		if ((ptr->getOptType() == MIN_OPT && obj < parent.data().m_obj[0]) || (ptr->getOptType() == MAX_OPT && obj > parent.data().m_obj[0]))
		{
			parent.data().m_obj[0] = obj;
			for (int i = 0; i < getNumDim(); i++)
				parent.data().m_x[i] = m_iteIndivl.data().m_x[i];
			parent.setFlag(true);
		}
	}
}
Ejemplo n.º 4
0
void runSimulation(int numberOfTimes, int count) {
	std::fstream dataToSave;
	stringstream s;
	s <<"data/symulacja-" << count << ".txt";
	dataToSave.open(s.str(), ios::trunc|ios::out);
	int sum = 0;

	for(int i = 0; i < numberOfTimes; i++) {
		TravellingSalesman salesman;
		cout << i << endl;
		salesman.generateRandomPath(count);
		Timer timer;
		timer.startTimer();
//		sum += simmulation.runAlgorithm().value;
		Dyn.DynamicProgrammingAlgorithm(salesman);
		double time = timer.endTimer();
		dataToSave << time << " ";
	}
	dataToSave.close();
	//return (float)sum/numberOfTimes;
}
Ejemplo n.º 5
0
GAIndividual<CodeVInt> GSTM::crossover(const GAIndividual<CodeVInt> &indivl1, const GAIndividual<CodeVInt> &indivl2)
{
	if (indivl1 == indivl2)
		return indivl1;
	GAIndividual<CodeVInt> indivl;
	int i, j, n;
	vector<int> arr(m_numDim), flag(m_numDim+1);
	vector<vector<int>> matrix1(m_numDim), matrix2(m_numDim);
	for (i = 0; i < m_numDim; i++)
	{
		matrix1[i].resize(m_numDim);
		matrix2[i].resize(m_numDim);
	}
	map<int, vector<int> > frag;
	for (i = 0; i<m_numDim; i++)
		arr[i] = indivl1.data().m_x[i];
	for (i = 0; i<m_numDim + 1; i++)
		flag[i] = 0;
	for (i = 0; i<m_numDim; i++)
	{
		for (j = 0; j<m_numDim; j++)
		{
			matrix1[i][j] = 0;
			matrix2[i][j] = 0;
		}
	}
	for (i = 0; i<m_numDim; i++)
	{
		matrix1[arr[i]][arr[(i + 1) % m_numDim]] = 1;
		matrix1[arr[(i + 1) % m_numDim]][arr[i]] = 1;
		matrix2[indivl2.data().m_x[i]][indivl2.data().m_x[(i + 1) % m_numDim]] = 1;
		matrix2[indivl2.data().m_x[(i + 1) % m_numDim]][indivl2.data().m_x[i]] = 1;
	}
	for (i = 0; i<m_numDim; i++)
	{
		if (matrix2[arr[i]][arr[(i + 1) % m_numDim]] == 0)
			flag[i + 1] = 1;
	}
	flag[0] = flag[m_numDim];
	int m = 0;
	n = 0;
	j = 0;
	frag[n].push_back(arr[j++]);
	m++;
	while (flag[j] == 0 && m<m_numDim)
	{
		frag[n].push_back(arr[j++]);
		m++;
	}
	i = m_numDim;
	while (flag[i] == 0 && m<m_numDim)
	{
		frag[n].insert(frag[n].begin(), arr[i - 1]);
		i--;
		m++;
	}
	while (m<m_numDim)
	{
		if (flag[j] == 1) n++;
		frag[n].push_back(arr[j++]);
		m++;
		while (flag[j] == 0)
		{
			frag[n].push_back(arr[j++]);
			m++;
		}
	}
	vector<int> visited(m_numDim);
	map<int, int> mp;
	for (i = 0; i<m_numDim; i++)
		visited[i] = 0;
	m = 0;
	for (i = 0; i <= n; i++)
	{
		visited[m++] = frag[i].front();
		mp[visited[m - 1]] = i;
		if (frag[i].size()>1)
		{
			visited[m++] = frag[i].back();
			mp[visited[m - 1]] = i;
		}
	}
	int starts;
	i = Global::msp_global->getRandInt(0, m - 1);
	j = 0;
	arr[j++] = visited[i];
	starts = visited[i];
	if (frag[mp[visited[i]]].front() == starts)
	{
		for (size_t z = 1; z<frag[mp[visited[i]]].size(); z++)
			arr[j++] = frag[mp[visited[i]]][z];
		starts = frag[mp[visited[i]]].back();
		if (frag[mp[visited[i]]].size()>1)
		{
			int fg = 0;
			if (visited[m - 1] == visited[i]) fg = 1;
			for (int z = 0; z<m; z++)
			{
				if (visited[z] == frag[mp[visited[i]]].back())
				{
					visited[z] = visited[m - 1];
					m--;
					if (fg == 1) i = z;
					break;
				}
			}
		}
		visited[i] = visited[m - 1];
		m--;
	}
	else if (frag[mp[visited[i]]].back() == starts)
	{
		for (int z = frag[mp[visited[i]]].size() - 2; z >= 0; z--)
			arr[j++] = frag[mp[visited[i]]][z];
		starts = frag[mp[visited[i]]].front();
		if (frag[mp[visited[i]]].size()>1)
		{
			int fg = 0;
			if (visited[m - 1] == visited[i]) fg = 1;
			for (int z = 0; z<m; z++)
			{
				if (visited[z] == frag[mp[visited[i]]].front())
				{
					visited[z] = visited[m - 1];
					m--;
					if (fg == 1) i = z;
					break;
				}
			}
		}
		visited[i] = visited[m - 1];
		m--;
	}
	n--;
	for (i = 0; i <= n; i++)
	{
		double min = DBL_MAX;
		int temp;
		int pos = -1;
		TravellingSalesman * ptr = dynamic_cast<TravellingSalesman*>(Global::msp_global->mp_problem.get());
		for (int z = 0; z<m; z++)
		{
			if (min>ptr->getCost()[starts][visited[z]] && matrix1[starts][visited[z]] == 0 && matrix2[starts][visited[z]] == 0)
			{
				min = ptr->getCost()[starts][visited[z]];
				temp = visited[z];
				pos = z;
			}
		}
		if (pos == -1)
		{
			temp = visited[0];
			pos = 0;
		}
		arr[j++] = temp;
		if (frag[mp[temp]].front() == temp)
		{
			for (size_t z = 1; z<frag[mp[temp]].size(); z++)
				arr[j++] = frag[mp[temp]][z];
			starts = frag[mp[temp]].back();
			if (frag[mp[temp]].size()>1)
			{
				int fg = 0;
				if (visited[m - 1] == visited[pos]) fg = 1;
				for (int z = 0; z<m; z++)
				{
					if (visited[z] == frag[mp[temp]].back())
					{
						visited[z] = visited[m - 1];
						m--;
						if (fg == 1) pos = z;
						break;
					}
				}
			}
			visited[pos] = visited[m - 1];
			m--;
		}
		else if (frag[mp[temp]].back() == temp)
		{
			for (int z = frag[mp[temp]].size() - 2; z >= 0; z--)
				arr[j++] = frag[mp[temp]][z];
			starts = frag[mp[temp]].front();
			if (frag[mp[temp]].size()>1)
			{
				int fg = 0;
				if (visited[m - 1] == visited[pos]) fg = 1;
				for (int z = 0; z<m; z++)
				{
					if (visited[z] == frag[mp[temp]].front())
					{
						visited[z] = visited[m - 1];
						m--;
						if (fg == 1) pos = z;
						break;
					}
				}
			}
			visited[pos] = visited[m - 1];
			m--;
		}
	}
	for (i = 0; i<m_numDim; i++)
		indivl.data().m_x[i] = arr[i];
	return indivl;
}
Ejemplo n.º 6
0
void Reconnection(GAIndividual<CodeVInt> &indivl, int starts, int ends)
{
	vector<int> T, subTour;
	size_t i;
	int startPos;
	int numDim = indivl.getNumDim();
	for (int j = 0; j<numDim; j++)
	{
		if (indivl.data().m_x[j] == starts)
		{
			startPos = j;
			break;
		}
	}
	while (indivl.data().m_x[startPos] != ends)
	{
		subTour.push_back(indivl.data().m_x[startPos]);
		startPos = (startPos + 1) % numDim;
	}
	subTour.push_back(indivl.data().m_x[startPos]);
	startPos = (startPos + 1) % numDim;
	while (indivl.data().m_x[startPos] != starts)
	{
		T.push_back(indivl.data().m_x[startPos]);
		startPos = (startPos + 1) % numDim;
	}
	double len1 = 0, len2 = 0;
	TravellingSalesman *ptr = dynamic_cast<TravellingSalesman*>(Global::msp_global->mp_problem.get());
	for (i = 0; i<T.size(); i++)
		len1 += ptr->getCost()[T[i]][T[(i + 1) % T.size()]];
	for (i = 0; i<subTour.size(); i++)
		len2 += ptr->getCost()[subTour[i]][subTour[(i + 1) % subTour.size()]];
	bool flag;
	size_t pos;
	double min = DBL_MAX;
	for (i = 0; i<T.size(); i++)
	{
		double temp;
		temp = len1 + len2 + ptr->getCost()[T[i]][starts] + ptr->getCost()[ends][T[(i + 1) % T.size()]] - ptr->getCost()[T[i]][T[(i + 1) % T.size()]] - ptr->getCost()[starts][ends];
		if (min>temp)
		{
			min = temp;
			pos = i;
			flag = true;
		}
		temp = len1 + len2 + ptr->getCost()[T[i]][ends] + ptr->getCost()[starts][T[(i + 1) % T.size()]] - ptr->getCost()[T[i]][T[(i + 1) % T.size()]] - ptr->getCost()[starts][ends];
		if (min>temp)
		{
			min = temp;
			pos = i;
			flag = false;
		}
	}
	int j = 0;
	for (i = 0; i<T.size(); i++)
	{
		if (i == pos)
		{
			indivl.data().m_x[j++] = T[i];
			if (flag == true)
			{
				for (size_t z = 0; z<subTour.size(); z++)
					indivl.data().m_x[j++] = subTour[z];
			}
			else if (flag == false)
			{
				for (int z = subTour.size() - 1; z >= 0; z--)
					indivl.data().m_x[j++] = subTour[z];
			}
		}
		else
			indivl.data().m_x[j++] = T[i];
	}
}