Exemplo n.º 1
0
///////////////////////////////  INTERFACE  ////////////////////////////////
void GeneticClass::Interface(){
	lc = 10000;             
	int algorithmIteration = 30;
	float crossoverRatio = 0.9;
	int mutationIteration = 1000;

	int score = 0, parent1, parent2;

	DrawingPopulation();
	Rating();
	cout << "Rodzice: " << bestScoreInAll << endl;
	checksRepeatsInSet();

	////////////// SELEKCJA I KRZYZOWANIE I MUTACJA///////////////////////////////
	for (int z = 0; z<algorithmIteration; z++){
		int P = -1;		                            // licznik nowej populacji
		vector<bool> crossed(lc,false);
		int crossoverIterations = (lc - 2) * crossoverRatio;
		#pragma omp parallel for
		for (P = -1; P < crossoverIterations; P += 2)
		{
			parent1 = TournamentSelection(10);
			do
				parent2 = TournamentSelection(10);
			while (parent1 == parent2);
			crossed[parent1] = true;
			crossed[parent2] = true;
			Crossover(parent1, parent2, children[P + 1], children[P + 2]);
		}

		for (int i = 0 ; i < lc; i++) {					//Dopisuje do children osobniki ktore nie braly udzialu w krzyzowaniu
			if (crossoverIterations != lc) {
				if (crossed[i] == false) {
					children[crossoverIterations++] = chromosom[i];
				}
			}
			else {
				break;
			}
		}

		chromosom.swap(children);						
		
		for (int i = 0; i<mutationIteration; i++){
			int target = TournamentSelection(100);
			Mutation(chromosom[target]);			//	przy zakomentowanym krzyzowaniu wpisalem tu chromosom zamiast children 
		}

		checksRepeatsInSet();
	
		score = Rating();
		cout << "Populacja_" << z << " = " << score << endl;
	}
	showBest();
}
Exemplo n.º 2
0
void solve(void)
{
	FILE *gp;
	char *map = "map.txt";
	FILE *dp;
	char *dest = "dest.txt";
	gp = fopen(map, "r");

	int i, j;
	fscanf(gp, "%d %d", &n, &m);
	
	nextGo = malloc((n + 1) * sizeof(int));
	nextTrans = malloc((n + 1) * sizeof(int *));
	for (i = 0; i < n; i++) nextTrans[i] = malloc((n + 1) * sizeof(int));

	queueS = malloc((n + 1) * sizeof(int));
	queueT = malloc((n + 1) * sizeof(int));
	queueC = malloc((n + 1) * sizeof(int));

	timeLeave = malloc((n + 1) * sizeof(int *));
	timeArrive = malloc((n + 1) * sizeof(int *));

	stationName = malloc((n + 1) * sizeof(char *));
	for (i = 0; i < n; i++) stationName[i] = malloc(100 * sizeof(char));
	trainName = malloc((n + 1) * sizeof(char *));
	for (i = 0; i < n; i++) trainName[i] = malloc(100 * sizeof(char));

	int nextGoLast = 0;
	for (i = 0; i < m; i++) {
		char get[100];
		fscanf(gp, "%s", get);
		int nn;
		fscanf(gp, "%d", &nn);
		for (j = 0; j < nn; j++) {
			char station[100];
			fscanf(gp, "%s", station);

			if (j != 0) nextGo[nextGoLast - 1] = nextGoLast;
			if (j == nn - 1) nextGo[nextGoLast] = - 1; //次がない駅は-1

			//文字列と関連付けておくcode追加する
			strcpy(stationName[nextGoLast], station);
			strcpy(trainName[nextGoLast], get);

			nextGoLast++;
		}
	}


	//同じ駅名を接続したいので、そのコード
	//nextTransに追加していく
	nextTransLast = malloc((n + 1) * sizeof(int));
	for (i = 0; i < n; i++) nextTransLast[i] = 0;
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			if (!strcmp(stationName[i],  stationName[j])) {
				nextTrans[i][nextTransLast[i]] = j;
				nextTransLast[i]++;
			}
		}
	}
	timeDataSize = malloc((n + 1) * sizeof(int));
	for (i = 0; i < n; i++) {
		int data;
		fscanf(gp, "%d", &data);
		
		timeLeave[i] = malloc((data + 1) * sizeof(int));
		timeArrive[i] = malloc((data + 1) * sizeof(int));
		timeDataSize[i] = data;
		for (j = 0; j < data; j++) {
			int getTimeLeave;
			int getTimeArrive;
			fscanf(gp, "%d %d", &getTimeLeave, &getTimeArrive);
			timeLeave[i][j] = getTimeLeave;
			timeArrive[i][j] = getTimeArrive;
		}
	}
	fclose(gp);

	way = malloc((n + 1) * sizeof(int *));
	for (i = 0; i < n; i++) way[i] = malloc((n + 1) * sizeof(int));
	wayTime = malloc((n + 1) * sizeof(int *));
	for (i = 0; i < n; i++) wayTime[i] = malloc((n + 1) * sizeof(int));
	wayLast = malloc((n + 1) * sizeof(int));
	for (i = 0; i < n; i++) wayLast[i] = 0;

	dp = fopen(dest, "r");

	char getStation[100];
	char getGoal[100];
	fscanf(dp, "%s %d %s", getStation, &startTime, getGoal);
	for (i = 0; i < n; i++) {
		if (!strcmp(getStation, stationName[i])) startStation = i;
		if (!strcmp(getGoal, stationName[i])) goal = i;
	}
	fclose(dp);
	bfsFast();
	// bfsEasiest();

	showBest(getGoal);
}