int main() { double result; double bestsofar = -999; double bestA[10]; double ss[10]; int i; while(result < 9 && numeval <= NUMEVAL) { srand(time(NULL)); result = geneticAlgorithm(ss); if(result > bestsofar) { bestsofar = result; memcpy(bestA,ss,sizeof(double)*10); } printf("\nBestSoFarGeneral: %lf No. Eval: %d \n", bestsofar,numeval); } while(result < 10 && numeval <= NUMEVAL) { srand(time(NULL)); result = simulatedAnnealing(300000,ss); if(result > bestsofar) { bestsofar = result; memcpy(bestA,ss,sizeof(double)*10); } for(i = 0; i < 10; i++){ printf("\nBSF[%d]: %lf",i,bestA[i]); } printf("\nBestSoFarGeneral: %lf No. Eval: %d \n", bestsofar,numeval); } }
int main(int argc, char *argv[]) { int algorithm, times_to_execute=1; do { printf ("Number of queens (1<=nqueens<%d): ", MAXQ); scanf ("%d", &nqueens); } while ((nqueens < 1) || (nqueens > MAXQ)); do { printf ("Algorithm: (1) Random search (2) Hill climbing "); printf ("(3) Simulated Annealing (4) Genetic Algorithm: "); scanf ("%d", &algorithm); } while ((algorithm < 1) || (algorithm > 4)); if (algorithm != 4) { do { printf("How many times to execute? (1<n<10000):"); scanf("%d", ×_to_execute); } while ((times_to_execute < 1 || times_to_execute > 10000)); } initializeRandomGenerator(); int i=0; switch (algorithm) { case 1: randomSearch(); break; case 2: solutions_found = 0; for (i=0; i<times_to_execute; i++) { initiateQueens(1); hillClimbing(); } printf("Algorithm finished. Found a solution in %d out of %d executions.\n", solutions_found, i); break; case 3: solutions_found = 0; for(i = 0; i < times_to_execute; i++) { initiateQueens(1); simulatedAnnealing(); } printf("Algorithm finished. Found a solution in %d out of %d executions.\n", solutions_found, i); break; case 4: initiateQueens(1); geneticAlgorithm(); } return 0; }
int main(int argc, char **argv) { char key[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; FILE *fin = stdin; /* * Get command line arguments. Setup key and input location */ switch (argc) { case 2: fin = fopen(argv[1], "r"); if (fin == NULL) { printf("Invalid file: %s. Unable to open for reading.\n", argv[3]); return -1; } case 1: if (!removeLetter(key, 'J')) { printf("Could not remove letter J from cipher key.\n"); return -1; } break; case 4: fin = fopen(argv[3], "r"); if (fin == NULL) { printf("Invalid file: %s. Unable to open for reading.\n", argv[3]); return -1; } case 3: if (strcmp(argv[1], "-r")) { printf("Optional parameter '-r' not found. '%s' found instead.\n", argv[1]); return -1; } if(!removeLetter(key, argv[2][0])) { printf("Could not remove letter %c from cipher key.\n", argv[2][0]); return -1; } break; default: printf("Invalid usage. See below for proper usage.\n"); printf("\t./%s [ -r <character_to_remove> ] [ ciphertext_filepath ]\n", argv[0]); return -1; } /* * Input cipher and ensure it is valid */ char *ciphertext, *plaintext; int messageLen; ciphertext = readCipher(fin, INPUT_STEP_SIZE); messageLen = strlen(ciphertext); if (validateText(ciphertext, &messageLen) != 0) { free(ciphertext); return -1; } ciphertext = realloc(ciphertext, sizeof(*ciphertext) * (messageLen + 1)); ciphertext[messageLen] = '\0'; plaintext = calloc(messageLen + 1, sizeof(*plaintext)); strcpy(plaintext, ciphertext); // close the file as long as it is not stdin if (fin != stdin) fclose(fin); // Output relevant information for error checking printf("Attempting to crack the following ciphertext with key: %s\n", key); printf("%s\n", ciphertext); int iter = 0; double score = -DBL_MAX, maxScore = -DBL_MAX; srand(time(NULL)); // randomize seed // Run until max iteration met while (iter < MAX_ITERATIONS) { iter++; score = simulatedAnnealing(key, ciphertext, plaintext, messageLen); if (score > maxScore) { maxScore = score; decipher(key, ciphertext, plaintext, messageLen); printf("\nPossible Plaintext found using key:\n"); outputKey(key); printf("%s\n\n", plaintext); } } free(plaintext); free(ciphertext); return 0; }
void runSample() { // Getting random numbers int n = 100; uint64_t* nums = malloc(sizeof(uint64_t) * n); for (int i = 0; i < n; i++) { nums[i] = getNum(); } // Generate sequence and partition (same result) int* s = sequenceGenerate(n); int* p = partitionGenerate(n); // Results and times uint64_t results[9]; struct timeval times[10]; gettimeofday(×[0], NULL); results[0] = kk(nums, n); gettimeofday(×[1], NULL); results[1] = sequenceResidue(nums, s, n); gettimeofday(×[2], NULL); results[2] = partitionResidue(nums, p, n); gettimeofday(×[3], NULL); results[3] = repeatedRandom(nums, s, n, 1); gettimeofday(×[4], NULL); results[4] = repeatedRandom(nums, p, n, 0); gettimeofday(×[5], NULL); results[5] = hillClimbing(nums, s, n, 1); gettimeofday(×[6], NULL); results[6] = hillClimbing(nums, p, n, 0); gettimeofday(×[7], NULL); results[7] = simulatedAnnealing(nums, s, n, 1); gettimeofday(×[8], NULL); results[8] = simulatedAnnealing(nums, p, n, 0); gettimeofday(×[9], NULL); // Difference between times int diff[9]; for (int i = 0; i < 9; i++) { diff[i] = (int) ((1000000 * (times[i + 1].tv_sec - times[i].tv_sec) + (times[i + 1].tv_usec - times[i].tv_usec)) / 1000); } // Writing data and times to file FILE* f = fopen("data/data.csv", "a"); if (f == NULL) { printf("Error opening file\n"); return; } fprintf(f, "%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld,%lld\n", results[0], results[1], results[2], results[3], results[4], results[5], results[6], results[7], results[8]); fclose(f); f = fopen("data/times.csv", "a"); if (f == NULL) { printf("Error opening file\n"); return; } fprintf(f, "%i,%i,%i,%i,%i,%i,%i,%i,%i\n", diff[0], diff[1], diff[2], diff[3], diff[4], diff[5], diff[6], diff[7], diff[8]); fclose(f); // Free all allocated pointers free(nums); free(s); free(p); }
int main(int argc, char* argv[]) { if (argc != 5) { cout << "\t./p5exe N K A directory\n"; exit(1); } // Data structures hash_map<string, int> wordMap; hash_map<string, int> index; vector <pair<string, vector<bool> > > bitVectors; vector<pair<int, pair<string,string> > > clust; vector<string> files; vector<vector<string> > clusters; vector<vector<int> > matrix(26, vector<int> (26, 0)); int numElts = 0, algorithm = atoi(argv[3]); // Read files in directory and create bitVectors readDir(atoi(argv[1]), argv[4], wordMap, numElts, bitVectors); removeUnused(numElts, bitVectors); // Print similarity matrix simMatrix(numElts, bitVectors, clust, index, matrix, files); greedy(atoi(argv[2]), bitVectors, clust, index, matrix, clusters); if (algorithm == 2) { simulatedAnnealing(clusters, matrix, index); } if (algorithm == 3) // i don't consider if the solution returns less than/greater than k clusters { vector<int> diameters; int maxDiam = 0; for (int i = 0; i < clusters.size(); i++) { int diam = 0; for (int j = 0; j < clusters.at(i).size(); j++) { for (int k = j; k < clusters.at(i).size(); k++) { hash_map <string, int>::iterator It1 = index.find(clusters.at(i).at(j)); hash_map <string, int>::iterator It2 = index.find(clusters.at(i).at(k)); int tmpDist = matrix.at(It1 -> second).at(It2 -> second); if (tmpDist > diam) { diam = tmpDist; } } } diameters.push_back(diam); if (diam > maxDiam) { maxDiam = diam; } } vector<vector<string> > empty; int curDiam = 0; branchAndBound(files, clusters, empty, maxDiam, curDiam, matrix, index, atoi(argv[2])); } sortClustering(clusters); printClusteredMatrix(clusters, matrix, index); return 0; }
// Mutacion // Simulated Annealing + Alguna Mejora void mutation(){ simulatedAnnealing(); }