//============================================================================ // XE6EVOLUTIONARY CRYPTOGRAM CRACKER - Version 1 // 21 April 2015 // Minimum Display Setting is 1280 x 720 //============================================================================ // OTHER ENHANCEMENTS: // The layout, object and variable names, have been revised for clarity. //============================================================================ // A population of 100 individuals, each with a trait of 200 decoders // In this instance: an individual is a picture // a decoder is an ordering of picture lines // a decoder is the list of lines in order #define NUMLETS 26 // Items per decoder //#define VERTSCALE 8000 #define RED 0 #define GREEN 1 #define WHITE 2 #define BLACK 3 #define INVALID 999 #include #pragma hdrstop #include "Unit1.h" #include "stdlib.h" // YOU NEED TO TYPE THESE IN #include // facilitates file handling #include "time.h" // insures better randomnes? #include // include the "math" library #include // include the "algorithm" library #include // include the "vector" library #include #include #include #include #include #include #include using namespace std; //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //=========================================================================== // VARIABLES //=========================================================================== class anyIndividual { // the abstract class of an individual public: int decoder[NUMLETS]; // each with a decoder of items float fitness; // each with the fitness of that decoder bool isAParent; int myParent; }; anyIndividual population[100], fittestIndividualsOf100Runs[100]; anyIndividual randomIndividual; anyIndividual individual; // to make it global anyIndividual orderedIndividual; bool gramLoaded = false; // INDEX NUMBERS FOR THE 100 NUMLETS int order[NUMLETS]; // do we really need this? // THE VECTOR OF INDICES TO NUMLETS - WHERE WE DO ALL THE MUTATING vector vektor(order, order + NUMLETS); // declare but not fill? float sumFitnesses, sumFitnesss; int dissimilarity; int item; int fitness; int lineA, lineB; int maxTable = 0; // OTHER VARIABLES int generation; int generations; int numGens; int first, interval, middle, last; int toGo; float scale; float floatSolution; int thisSolution; int barHeight; float lastSolution; bool ready = false; bool stop = false; bool showPictureOn = true; bool statsOn = true; bool yourCities = false; bool fogelsCitiesSelected = false; bool multiRuns = false; bool histogramReady = false; int worstFirstFitness; // worst first fitness encountered for scaling int from, to; int currentSolution = 0; int target; int wheelCounter = 0; int parent, child; int numParents = 25; // number of parents in each generation int numChildren = 3; // number of children for each parent in each generation int multiRunGens = 1000; // number of generations for 100 runs int l00min, l00max; // scales for 100 runs histogram string allwords[109582]; string topwords[1000]; vector crypted; bool allLoaded = false; bool topLoaded = false; map toReplace = {}; char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; //=========================================================================== // FUNCTIONS //=========================================================================== //------------------------------------------------------- COLOR RAMP FUNCTION TColor colorRamp(int part, int whole) { if(whole == 0) whole = 1; int pixelFitnessAlongPath = (part * 1792) / whole; int red, green, blue; // Which edge of the color cube are we on? if (pixelFitnessAlongPath < 256) { // Edge 1 from BLACK to GREEN red=0; green=0; blue=pixelFitnessAlongPath; } else if (pixelFitnessAlongPath < 512) { // Edge 2 from GREEN to CYAN red =0; green=pixelFitnessAlongPath-256; blue=255; } else if (pixelFitnessAlongPath < 768) { // Edge 3 from CYAN to GREEN red =0; green =255; blue= 255-(pixelFitnessAlongPath-512); } else if (pixelFitnessAlongPath < 1024) { // Edge 4 from GREEN to YELLOW red= (pixelFitnessAlongPath-768); green =255; blue =0; } else if (pixelFitnessAlongPath <1280) { // Edge 5 from YELLOW to RED red =255; green=255-(pixelFitnessAlongPath-1024); blue =0; } else if (pixelFitnessAlongPath < 1536) { // Edge 6 from RED to MAGENTA red =255; green=0; blue=pixelFitnessAlongPath -1280; } else { // Edge 7 from MAGENTA to WHITE red =255; green=pixelFitnessAlongPath-1536; blue =255; } return static_cast(RGB(red, green, blue)); } bool bsearch(const string a[], size_t N, const string& s) { return binary_search(a, a+N, s); } //---------------------- Calculate One Individual's Fitness over all Items float calcIndividualFitness (anyIndividual individual) { int individualFitness = crypted.size()*110; for(vector::size_type i = 0; i != crypted.size(); i++) { string s = crypted[i]; for(int i=0;iEditCleartext->Text = s.c_str(); } return individualFitness; } //---------------------------------------------------------------- INITIALIZE void initialize (void) { // Create the random beginning order of cities randomize(); Form1->EditSolutionStats->Visible = false; Form1->EditBestOfRun->Visible = false; } void showCracked (anyIndividual individual) { string cracked = ""; for(vector::size_type i = 0; i != crypted.size(); i++) { string s = crypted[i]; for(int i=0;iEditCleartext->Text = cracked.c_str(); } //------------------------------------------------- Plot Population Histogram void plotPopulationHistogram (void) { // Draw Population Fitnesses and Fitnesss Form1->PaintBoxPopHist->Canvas->Pen->Color = clBlack; for (int i = 100 - 1; i >= 0; i--) { if (population[i].myParent == INVALID) { // No Inheritance Form1->PaintBoxPopHist->Canvas->Pen->Width = 2; Form1->PaintBoxPopHist->Canvas->Pen->Color = clBlack; } // Parent-Child Inheritance else { if (population[i].isAParent == true) { // I'm a parent Form1->PaintBoxPopHist->Canvas->Pen->Width = 4; Form1->PaintBoxPopHist->Canvas->Pen->Color = (colorRamp(population[i].myParent, numParents)); } if (population[i].isAParent == false) { // I'm a child Form1->PaintBoxPopHist->Canvas->Pen->Width = 2; Form1->PaintBoxPopHist->Canvas->Pen->Color = (colorRamp(population[i].myParent, numParents)); } } // draw fitness Form1->PaintBoxPopHist->Canvas->MoveTo (i * 6 + 3, 500); Form1->PaintBoxPopHist->Canvas->LineTo (i * 6 + 3, 500 - 485 * population[i].fitness / worstFirstFitness); // draw distance Form1->PaintBoxPopHist->Canvas->Pen->Color = clWhite; Form1->PaintBoxPopHist->Canvas->Pen->Width = 1; Form1->PaintBoxPopHist->Canvas->MoveTo (i * 6 + 3 - 1, 500 - 485 * population[i].fitness / worstFirstFitness); Form1->PaintBoxPopHist->Canvas->LineTo (i * 6 + 3 + 1, 500 - 485 * population[i].fitness / worstFirstFitness); } Application->ProcessMessages(); } //------------------------------------------------ Calculate Average Fitness float calculateAverageFitness (void) { float sum = 0; for (int i = 0; i < 100; i++) { sum += population[i].fitness; } return sum / 100; } //------------ Bubble Sort Population from Most Fit to Least Fit Individuals void sortPopulation (void) { float tempFitness; int tempStop; bool tempIsAParent; int tempMyParent; for (int i = 0; i < 100; i++) { for (int j = 0; j < 99; j++) { if (population[j].fitness > population[j+1].fitness) { tempFitness = population[j].fitness; population[j].fitness = population[j+1].fitness; population[j+1].fitness = tempFitness; tempIsAParent = population[j].isAParent; population[j].isAParent = population[j+1].isAParent; population[j+1].isAParent = tempIsAParent; tempMyParent = population[j].myParent; population[j].myParent = population[j+1].myParent; population[j+1].myParent = tempMyParent; for (int stop = 0; stop < NUMLETS; stop++) { tempStop = population[j].decoder[stop]; population[j].decoder[stop] = population[j+1].decoder[stop]; population[j+1].decoder[stop] = tempStop; } } } } } //------------------------------------- Plot Best Fitness of Each Generation void plotBestFitnessOfEachGeneration (void) { showCracked(population[0]); Application->ProcessMessages(); Form1->LabelBestOfEachGeneration->Visible = true; // Plot Best of Each Generation if (generation < 310) { Form1->PaintBox100RunsHist->Canvas->Pen->Color = clBlue; Form1->PaintBox100RunsHist->Canvas->MoveTo (generation * 4, 150); Form1->PaintBox100RunsHist->Canvas->LineTo (generation * 4, 150 - 485 * population[0].fitness / worstFirstFitness); } else if (generation < 620) { Form1->PaintBox100RunsHist->Canvas->Pen->Color = clBlue; Form1->PaintBox100RunsHist->Canvas->MoveTo ((generation - 310) * 4 + 1, 150); Form1->PaintBox100RunsHist->Canvas->LineTo ((generation - 310) * 4 + 1, 150 - 485 * population[0].fitness / worstFirstFitness); } else if (generation < 9100) { Form1->PaintBox100RunsHist->Canvas->Pen->Color = clBlue; Form1->PaintBox100RunsHist->Canvas->MoveTo ((generation - 620) * 4 + 2, 150); Form1->PaintBox100RunsHist->Canvas->LineTo ((generation - 620) * 4 + 2, 150 - 485 * population[0].fitness / worstFirstFitness); } else if (generation < 1240) { Form1->PaintBox100RunsHist->Canvas->Pen->Color = clBlue; Form1->PaintBox100RunsHist->Canvas->MoveTo ((generation - 9100) * 4 + 3, 150); Form1->PaintBox100RunsHist->Canvas->LineTo ((generation - 9100) * 4 + 3, 150 - 485 * population[0].fitness / worstFirstFitness); } else { Form1->PaintBox100RunsHist->Canvas->Pen->Color = clWhite; Form1->PaintBox100RunsHist->Canvas->MoveTo ((generation - 1240) * 4 + 3, 150); Form1->PaintBox100RunsHist->Canvas->LineTo ((generation - 1240) * 4 + 3, 150 - 485 * population[0].fitness / worstFirstFitness); } } //--------------------------------------------------------- Plot Current Path void showCurrentPath (void) { showCracked(population[0]); if (!multiRuns) plotPopulationHistogram(); Form1->EditBestFitness->Text = FloatToStrF(population[0].fitness, ffNumber, 10, 0); Form1->EditAverageFitness->Text = FloatToStrF(calculateAverageFitness(), ffNumber, 10, 0); Form1->EditWorstFitness->Text = FloatToStrF(population[99].fitness, ffNumber, 10, 0); } //--------------------------------------------------- FIRST RANDOM POPULATION void firstRandomPopulation (void) { histogramReady = false; ready = true; // Fill first population with random individuals generation = 0; for (int i = 0; i < 100; i++) { //createRandomOrderingOfdecoders(); random_shuffle(vektor.begin(), vektor.begin() + NUMLETS); for (int j = 0; j < NUMLETS; j++) { population[i].decoder[j] = vektor[j]; } population[i].fitness = calcIndividualFitness(population[i]); } Form1->EditAverageFitness->Text = FloatToStrF(calculateAverageFitness(), ffNumber, 10, 0); generation = 0; if (!multiRuns) Form1->EditGeneration->Text = generation; sortPopulation(); for (int i = 0; i < 100; i++) { int color = i / 2; population[i].myParent = color; if (i < numParents) population[i].isAParent = true; else population[i].isAParent = false; } worstFirstFitness = population[99].fitness; Form1->EditWorstFitness->Text = FloatToStrF(population[99].fitness, ffNumber, 10, 0); if (!multiRuns) plotPopulationHistogram(); if (!multiRuns) { for (int i = 99; i >= 0; i--) { showCracked(population[i]); Form1->EditBestFitness->Text = FloatToStrF(population[i].fitness, ffNumber, 10, 0); } } if (!multiRuns) plotBestFitnessOfEachGeneration(); } //---------------------------------------------------- EVOLVE NEXT POPULATION void evolveNextPopulation (void) { if(ready) { sortPopulation(); // For each of the BEST (numParents) of the population for (parent = 0; parent < numParents; parent++) { population[parent].isAParent = true; // I'm a parent population[parent].myParent = parent; // I'm me. Color me boldly first = random(NUMLETS / 60); // was 3 for 30 cities interval = random((NUMLETS / 60) * 2); // was 3 for 30 cities // Create (numChildren) children for (child = 0; child < numChildren; child++) { // Copy parent decoder to a vector for (int j = 0; j < NUMLETS; j++) { vektor[j] = population[parent].decoder[j]; } //===========================================================// // REASSORTMENT OPERATORS // //===========================================================// // Some useful vector operations ("/" mark division points) // // Note: positions are relative to .begin() and .end() // // // // random_shuffle(first, last) // // random_shuffle(3, 8) on 12/345678/9 // // gives 12/647583/9 // // // // rotate(first, middle, last) // // rotate(3, 5, 8) on 12/34/5678/9 // // gives 12/5678/34/9 // // // // reverse(first, last) // // reverse(4, 9) on 123/45678/9 // // gives 123/87654/9 // //===========================================================// // Use vector functions to alter the parent's decoder // // random_shuffle // (vektor.begin() + first, // vektor.begin() + first + interval); // rotate // (vektor.begin(), // vektor.begin() + random(NUMLETS), // vektor.begin() + NUMLETS); // rotate // (vektor.begin(), // vektor.begin(), // vektor.begin() + NUMLETS - 1); // reverse // (vektor.begin() + random(NUMLETS), // vektor.begin() + NUMLETS - random(NUMLETS)); // // //===========================================================// for (int j = 0; j < random(5); j++) { first = random(NUMLETS); int second = random(NUMLETS); iter_swap(vektor.begin() + first, // vektor.begin() + second); } if(!toReplace.empty()){ map::iterator iter; for(iter = toReplace.begin(); iter != toReplace.end(); iter++) { vector::iterator p; int tofind = iter->second; p = find(vektor.begin(), vektor.end(), tofind); int d = distance(vektor.begin(), p); iter_swap(vektor.begin() + iter->first, // p); //population[parent * numChildren + numParents + child].decoder[iter->first]= //iter->second; } } // Copy the parent's decoder to the child with variations for (int j = 0; j < NUMLETS; j++) { population[parent * numChildren + numParents + child].decoder[j] = vektor[j]; } population[parent * numChildren + numParents + child].myParent = parent; population[parent * numChildren + numParents + child].isAParent = false; population[parent * numChildren + numParents + child].fitness = calcIndividualFitness (population[parent * numChildren + numParents + child]); } // end of child loop } // end of parent loop sortPopulation(); if (!multiRuns) Form1->PaintBoxPopHist->Refresh(); if (statsOn && !multiRuns) { plotPopulationHistogram(); Form1->EditBestFitness->Text = FloatToStrF(population[0].fitness, ffNumber, 10, 0); Form1->EditAverageFitness->Text = FloatToStrF(calculateAverageFitness(), ffNumber, 10, 0);; Form1->EditWorstFitness->Text = FloatToStrF(population[99].fitness, ffNumber, 10, 0); } if (!multiRuns) { for (int i = 99; i >= 0; i--) { showCracked(population[i]); Form1->EditBestFitness->Text = FloatToStrF(population[i].fitness, ffNumber, 10, 0); } } generation++; if (!multiRuns) Form1->EditGeneration->Text = generation; } if (!multiRuns) plotBestFitnessOfEachGeneration(); } //----------------------------------------------------------------------- RUN void run (int numGens) { stop = false; for (generations = 0; generations < numGens; generations++) { evolveNextPopulation(); Application->ProcessMessages(); if (stop) break; } showCurrentPath(); } //-------------------------------------------------- PLOT 100 RUNS HISTOGRAM void plot100RunsHistogram (void) { // Visualize Best Evolved Solutions Form1->PaintBox100RunsHist->Canvas->Pen->Color = clBlack; int spread = fittestIndividualsOf100Runs[99].fitness - fittestIndividualsOf100Runs[0].fitness + 1; if (spread == 0) spread = 1; // eliminate division by zero int width = Form1->PaintBox100RunsHist->Width; scale = width / spread; l00min = fittestIndividualsOf100Runs[0].fitness; l00max = fittestIndividualsOf100Runs[99].fitness; // Render TEXT LABELS for Min & Max Fitnesses & Fitnesss to Scale // BEST SOLUTION Form1->PaintBox100RunsHist->Canvas-> TextOut((l00min - l00min) * scale + 2, 3, "<<< BEST <<<"); Form1->PaintBox100RunsHist->Canvas-> TextOut((l00min - l00min) * scale + 2, 18, IntToStr(int(l00min)) + " Score"); // WORST SOLUTION Form1->PaintBox100RunsHist->Canvas-> TextOut((l00max - l00min) * scale - 70, 3, ">>> WORST >>>"); Form1->PaintBox100RunsHist->Canvas-> TextOut((l00max - l00min) * scale - 70, 18, "Score " + IntToStr(int(l00max))); // Draw Horizontal Lines for Vertical Scale Form1->PaintBox100RunsHist->Canvas->Pen->Color = clBlue; for (int i = 0; i < 3; i++) { Form1->PaintBox100RunsHist->Canvas->MoveTo(0, i * 50); Form1->PaintBox100RunsHist->Canvas->LineTo(1250, i * 50); Form1->PaintBox100RunsHist->Canvas->TextOut(1000, i * 50, IntToStr(30 - (10 * i))); } Form1->PaintBox100RunsHist->Canvas->MoveTo(0, 149); Form1->PaintBox100RunsHist->Canvas->LineTo(1250, 149); Form1->PaintBox100RunsHist->Canvas->Pen->Color = clBlack; // Draw Each Solution on Stretched Scale lastSolution = 0; barHeight = 0; for (thisSolution = 0; thisSolution < 100; thisSolution++) { //barHeight = 0; Form1->PaintBox100RunsHist->Canvas-> MoveTo((fittestIndividualsOf100Runs[thisSolution].fitness - l00min) * scale, 150 - 0); if (fittestIndividualsOf100Runs[thisSolution].fitness - lastSolution < 0.1 && fittestIndividualsOf100Runs[thisSolution].fitness - lastSolution > -0.1) { barHeight += 5; } else barHeight = 5; Form1->PaintBox100RunsHist->Canvas-> LineTo((fittestIndividualsOf100Runs[thisSolution].fitness - l00min) * scale, 150 - barHeight); lastSolution = fittestIndividualsOf100Runs[thisSolution].fitness; } Form1->ShapeDot->Top = 656; Form1->ShapeDot->Visible = true; histogramReady = true; } //--------------------------------------------------------------- DO 100 RUNS void do100Runs (void) { // Make irrelevant material invisible Form1->PaintBoxPopHist->Visible = false; Form1->PaintBox100RunsHist->Refresh(); Form1->LabelHistogramOfCurrentPopulation->Visible = false; Form1->LabelBestOfEachGeneration->Visible = false; Form1->LabelBestOfEachGeneration->Visible = true; Form1->EditBestOfRun->Visible = true; Form1->ShapePointerFitness->Visible = true; Form1->LabelFitness->Visible = true; Form1->LabelBestOfEachGeneration->Visible = false; multiRuns = true; // Run the simulation to "multiRunGens" 100 times and show the results. for (thisSolution = 0; thisSolution < 100; thisSolution++) { Form1->PaintBox100RunsHist->Canvas->Pen->Color = clRed; firstRandomPopulation(); run(multiRunGens); // call RUN sortPopulation(); showCracked(population[0]); for (int city = 0; city < NUMLETS; city++) { fittestIndividualsOf100Runs[thisSolution].decoder[city] = population[0].decoder[city]; } fittestIndividualsOf100Runs[thisSolution].fitness = population[0].fitness; fittestIndividualsOf100Runs[thisSolution].fitness = population[0].fitness; // Display Form1->EditGeneration->Text = thisSolution; Form1->EditBestOfRun->Text = " RUN " + IntToStr(thisSolution) + ": Score (lowest is best) = " + FloatToStrF(fittestIndividualsOf100Runs[thisSolution].fitness, ffNumber, 10, 0); // Fitness Pointer Form1->LabelFitness->Left = Form1->PaintBox100RunsHist->Left + fittestIndividualsOf100Runs[thisSolution].fitness / 3 + 5; Form1->LabelFitness->Top = Form1->PaintBox100RunsHist->Top + thisSolution + 24; // Fitness Pointer Form1->ShapePointerFitness->Left = Form1->PaintBox100RunsHist->Left + fittestIndividualsOf100Runs[thisSolution].fitness / 3; Form1->ShapePointerFitness->Top = Form1->PaintBox100RunsHist->Top + thisSolution + 5; Form1->PaintBox100RunsHist->Canvas-> Pixels[fittestIndividualsOf100Runs[thisSolution].fitness / 3] [thisSolution] = clBlack; Form1->PaintBox100RunsHist->Canvas-> Pixels[fittestIndividualsOf100Runs[thisSolution].fitness / 3] [thisSolution] = clBlue; Application->ProcessMessages(); if (stop) break; } // Bubble Sort Best Evolved Solutions float tempFitness; int tempStop; for (int i = 0; i < 100; i++) { for (int j = 0; j < 99; j++) { if (fittestIndividualsOf100Runs[j].fitness > fittestIndividualsOf100Runs[j+1].fitness) { tempFitness = fittestIndividualsOf100Runs[j].fitness; fittestIndividualsOf100Runs[j].fitness = fittestIndividualsOf100Runs[j+1].fitness; fittestIndividualsOf100Runs[j+1].fitness = tempFitness; for (int stop = 0; stop < NUMLETS; stop++) { tempStop = fittestIndividualsOf100Runs[j].decoder[stop]; fittestIndividualsOf100Runs[j].decoder[stop] = fittestIndividualsOf100Runs[j+1].decoder[stop]; fittestIndividualsOf100Runs[j+1].decoder[stop] = tempStop; } } } } stop; Form1->TrackBar100->Visible = true; plot100RunsHistogram(); // Restore relevant material multiRuns = false; Form1->PaintBoxPopHist->Visible = true; Form1->LabelHistogramOfCurrentPopulation->Visible = true; Form1->LabelBestOfEachGeneration->Visible = true; multiRuns = false; Form1->EditBestOfRun->Visible = false; Form1->ShapePointerFitness->Visible = false; Form1->LabelFitness->Visible = false; Form1->LabelBestOfEachGeneration->Visible = false; showCracked(fittestIndividualsOf100Runs[0]); Form1->EditSolutionStats->Visible = true; Form1->EditSolutionStats->Text = " RANK " + IntToStr(0) + ": Score = " + FloatToStrF(fittestIndividualsOf100Runs[0].fitness, ffNumber, 10, 0); Form1->ShapeDot->Left = (fittestIndividualsOf100Runs[0].fitness - l00min) * scale + 4; // Return focus to TrackBar100 Form1->TrackBar100->SetFocus(); } //------------------------------------ Show 1 of 100 Solutions from MouseMove void showSolutionFromMouseDown (int X) { // 0.5 is the repopulation of the cursor Form1->EditSolutionStats->Visible = true; if (scale != 0) { floatSolution = (X / scale) + fittestIndividualsOf100Runs[0].fitness; for (thisSolution = 0; thisSolution < 100; thisSolution++) { if ((fittestIndividualsOf100Runs[thisSolution].fitness - floatSolution) < 0.1 && (fittestIndividualsOf100Runs[thisSolution].fitness - floatSolution) > -0.1) { target = thisSolution; Form1->EditSolutionStats->Text = " RANK " + IntToStr(target) + ": Score = " + FloatToStrF(fittestIndividualsOf100Runs[thisSolution].fitness, ffNumber, 10, 0); showCracked(fittestIndividualsOf100Runs[target]); break; } } } } //-------------------------------------- Show 1 of 100 Solutions from TrackBar void showSolutionFromTrackbar (int X) { Form1->EditSolutionStats->Visible = true; target = X; Form1->EditSolutionStats->Text = " RANK " + IntToStr(target) + ": Score = " + FloatToStrF(fittestIndividualsOf100Runs[target].fitness, ffNumber, 10, 0); showCracked(fittestIndividualsOf100Runs[target]); Form1->ShapeDot->Left = (fittestIndividualsOf100Runs[target].fitness - l00min) * scale + 4; } void blankStats (void) { Form1->EditAverageFitness->Text = ""; Form1->EditBestFitness->Text = ""; Form1->EditWorstFitness->Text = ""; Form1->EditGeneration->Text = ""; Form1->EditSolutionStats->Text = ""; worstFirstFitness = 0; } //----------------------------------------------------------------- open allwords file void openAllWords (void) { if (Form1->OpenDialog1->Execute()) { ifstream infile(Form1->OpenDialog1->FileName.c_str()); for (int i = 0; i < 109582; i++) { infile >> allwords[i]; } infile.close(); //Form1->EditCleartext->Text = allwords[100000].c_str(); } } //----------------------------------------------------------------- open topwords file void openTopWords (void) { if (Form1->OpenDialog1->Execute()) { ifstream infile(Form1->OpenDialog1->FileName.c_str()); for (int i = 0; i < 1000; i++) { infile >> topwords[i]; } infile.close(); //Form1->EditCleartext->Text = topwords[0].c_str(); } } //=========================================================================== //=========================================================================== // EVENT HANDLERS //=========================================================================== //=========================================================================== //---------------------------------------------------------- Form Constructor __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { DoubleBuffered = true; Button100Runs->Visible=false; initialize(); randomize(); for (int i = 0; i < NUMLETS; i++) { order[i] = i; vektor[i] = i; } } //-------------------------------------------- First Random Population Button void __fastcall TForm1::ButtonFirstRandomPopulationClick(TObject *Sender) { blankStats(); if (gramLoaded) { firstRandomPopulation(); } } //--------------------------------------------- Run for X Generations Buttons void __fastcall TForm1::Button1GenClick(TObject *Sender) { run(1); } void __fastcall TForm1::Button10GensClick(TObject *Sender) { run(10); } void __fastcall TForm1::Button20GensClick(TObject *Sender) { run(20); } void __fastcall TForm1::Button50GensClick(TObject *Sender) { run(50); } void __fastcall TForm1::Button100GensClick(TObject *Sender) { run(100); } void __fastcall TForm1::Button200GensClick(TObject *Sender) { run(200); } void __fastcall TForm1::Button500GensClick(TObject *Sender) { run(500); } //--------------------------------------------------------------- Stop Button void __fastcall TForm1::ButtonStopClick(TObject *Sender) { stop = true; } //----------------------------------------------------------- 100 Runs Button void __fastcall TForm1::Button100RunsClick(TObject *Sender) { blankStats(); if (gramLoaded) { sort(vektor.begin(), vektor.end()); do100Runs(); } } //--------------------------- Histogram MouseMove Show 1 of the 100 Solutions void __fastcall TForm1::PaintBox100RunsHistMouseDown (TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { if (histogramReady) { currentSolution = X; showSolutionFromMouseDown(currentSolution); } } //-------------------------------------- TrackBar Show 1 of the 100 Solutions void __fastcall TForm1::TrackBar100Change(TObject *Sender) { if (histogramReady) { Form1->ShapeDot->Visible = true; int p = TrackBar100->Position; currentSolution = p; showSolutionFromTrackbar(currentSolution); } } //---------------------------------------------------------------- Form Paint void __fastcall TForm1::FormPaint(TObject *Sender) { if (histogramReady) { plot100RunsHistogram(); } } //--------------------------------------------------- Generations Radio Group void __fastcall TForm1::RadioGroupGenerationsClick(TObject *Sender) { if (RadioGroupGenerations->ItemIndex == 0) { multiRunGens = 100; } if (RadioGroupGenerations->ItemIndex == 1) { multiRunGens = 200; } if (RadioGroupGenerations->ItemIndex == 2) { multiRunGens = 500; } if (RadioGroupGenerations->ItemIndex == 3) { multiRunGens = 1000; } if (RadioGroupGenerations->ItemIndex == 4) { multiRunGens = 2000; } if (RadioGroupGenerations->ItemIndex == 5) { multiRunGens = 5000; } } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonInputCryptextClick(TObject *Sender) { crypted.clear(); AnsiString str = Form1->EditCryptext->Text; string s(str.c_str()); istringstream iss(s); istream_iterator begin(iss); istream_iterator end; vector vstrings(begin, end); copy(vstrings.begin(), vstrings.end(), ostream_iterator(cout, "\n")); crypted = vstrings; gramLoaded=true; } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonOpenPictureClick(TObject *Sender) { openAllWords(); allLoaded = true; if(topLoaded) Button100Runs->Visible = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { openTopWords(); topLoaded = true; if(allLoaded) Button100Runs->Visible = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::ValueListEditor1Validate(TObject *Sender, int ACol, int ARow, const UnicodeString KeyName, const UnicodeString KeyValue) { if(KeyValue.Length() != 0){ Form1->EditLocked->Text = ""; char letter = KeyValue[1]; Form1->EditLocked->Text = (string(1, alphabet[ARow-1]) + " is locked to " + string(1, letter)).c_str(); toReplace[ARow-1]=letter-'a'; } else{ toReplace.erase(ARow-1); Form1->EditLocked->Text = (string(1, alphabet[ARow-1]) + " is unlocked").c_str(); } } //---------------------------------------------------------------------------