⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit_main.cpp

📁 针对TSP问题
💻 CPP
📖 第 1 页 / 共 2 页
字号:

void cities::paint_result() {
  // draw the black background
  TColor tcr = 0;
  TRect tr;
  tr.Left = 1;
  tr.Right = 400;
  tr.Top = 1;
  tr.Bottom = 400;
  ASForm->Canvas->Brush->Color = clBlack;
  ASForm->Canvas->FillRect(tr);

  // draw the cities
  ASForm->Canvas->Brush->Color = clWhite;
  for(int i = 0; i < nofc; i++) {
    tr.Left = city[i][0] * 400 - 2;
    tr.Right = city[i][0] * 400 + 2;
    tr.Top = city[i][1] * 400 - 2;
    tr.Bottom = city[i][1] * 400 + 2;
    ASForm->Canvas->FillRect(tr);
  }

  // find the range of tao[][]
  // it's enough to search the upper triangle
  double max_tao = MINDOUBLE,
         min_tao = MAXDOUBLE;
  for(int i = 0; i < nofc - 1; i++)
    for(int j = i + 1; j < nofc; j++)
      if (i != j) {
        if (tao[i][j] > max_tao) max_tao = tao[i][j];
        if (tao[i][j] < min_tao) min_tao = tao[i][j];
      }

  // draw the edge based on tao
  ASForm->Canvas->Pen->Mode = pmMerge;
  double tao_range = max_tao - min_tao;
  for(int i = 0; i < nofc - 1; i++)
    for(int j = i + 1; j < nofc; j++) {
      if (tao_range == 0.0)
        tcr = clRed;
      else
        tcr = (tao[i][j] - min_tao) / tao_range * 255;
      ASForm->Canvas->Pen->Color = tcr;
      ASForm->Canvas->MoveTo(city[i][0] * 400, city[i][1] * 400);
      ASForm->Canvas->LineTo(city[j][0] * 400, city[j][1] * 400);
    }
/*
  // draw the best tour found last cycle
  if (ASForm->CheckBox_bestlength->Checked) {
    ASForm->Canvas->Pen->Mode = pmCopy;
    ASForm->Canvas->Pen->Color = clBlue;
    ASForm->Canvas->MoveTo(city[ ants[best_ant][0] ][0] * 400,
                           city[ ants[best_ant][0] ][1] * 400);
    for(int i = 1; i < nofc; i++) {
      ASForm->Canvas->LineTo(city[ ants[best_ant][i] ][0] * 400,
                             city[ ants[best_ant][i] ][1] * 400);
    }
    ASForm->Canvas->LineTo(city[ ants[best_ant][0] ][0] * 400,
                           city[ ants[best_ant][0] ][1] * 400);
  }
*/
  if (ASForm->CheckBox_bestlength->Checked) {
    ASForm->Canvas->Pen->Mode = pmCopy;
    ASForm->Canvas->Pen->Color = clBlue;
    ASForm->Canvas->MoveTo(city[ best_solution[0] ][0] * 400,
                           city[ best_solution[0] ][1] * 400);
    for(int i = 1; i < nofc; i++) {
      ASForm->Canvas->LineTo(city[ best_solution[i] ][0] * 400,
                             city[ best_solution[i] ][1] * 400);
    }
    ASForm->Canvas->LineTo(city[ best_solution[0] ][0] * 400,
                           city[ best_solution[0] ][1] * 400);
  }
}

//---------------------------------------------------------------------------
__fastcall TASForm::TASForm(TComponent* Owner)
	: TForm(Owner)
{
  AS_start = false;
  output_file = fopen("as.out", "at");
}

//---------------------------------------------------------------------------
void __fastcall TASForm::Button_goClick(TObject *Sender)
{
  AS_start = false;
  step_count = 0;
  Digits1->Value = 0;
  Repaint();
  if (AS != NULL)
    delete AS;
  AS = new cities(	Edit_cities->Text.ToInt(), 			// number of cities
            		Edit_ants->Text.ToInt(), 			// number of ants
            		Edit_alpha->Text.ToDouble(),		// alpha
            		Edit_beta->Text.ToDouble(),			// beta
            		Edit_eva->Text.ToDouble(),	 		//evaporation
            		Edit_Q->Text.ToDouble(), 			// Q
            		Edit_tolerance->Text.ToDouble()); 	// tolerance
  AS->loaded = false;
  AS->generate_city_map();
  AS->ini_visibility_and_tao();
  StatusBar->Panels->Items[0]->Text = "";
  StatusBar->Panels->Items[1]->Text = "";
  StatusBar->Panels->Items[2]->Text = "";
  StatusBar->Panels->Items[3]->Text = "";
  StatusBar->Panels->Items[4]->Text = "";
  StatusBar->Panels->Items[5]->Text = "";
  Button_step->Enabled = true;
  Button_load->Enabled = true;
  Button_write->Enabled = false;
}

//---------------------------------------------------------------------------
void __fastcall TASForm::Button_stepClick(TObject *Sender)
{
  Button_write->Enabled = true;
  // change the parameters
  AS->change_p( Edit_alpha->Text.ToDouble(),		// alpha
            	Edit_beta->Text.ToDouble(),			// beta
            	Edit_eva->Text.ToDouble(),	 		//evaporation
            	Edit_Q->Text.ToDouble(), 			// Q
            	Edit_tolerance->Text.ToDouble()); 	// tolerance

  char line[80];
  bool stagnation;

  StatusBar->Panels->Items[0]->Text = "";

  Screen->Cursor = crHourGlass;

  // must use a TASForm variable 'cause it will be cganged by Form2 (Porgress)
  steps_to_run = Edit_steps->Text.ToInt();
  Digits1->Value = steps_to_run;
  for(int i = 0; i < steps_to_run; i++) {
    Digits1->Decrease();
    step_count++;
    if (stagnation = AS->one_cycle()) {
      StatusBar->Panels->Items[0]->Text = "S";
      break;
    }
  }

  Screen->Cursor = crDefault;

  // update the status bar
  AnsiString ts = (int)(AS->lengths[AS->best_ant]*1000)/1000.0;

  ts = "BL: " + ts;
  StatusBar->Panels->Items[1]->Text = ts;

  itoa(AS->cycle_count_when_best_found, line, 10);
  ts = (int)(AS->best_length*1000)/1000.0;
  ts = "B: " + ts +" <" + line + ">";
  StatusBar->Panels->Items[2]->Text = ts;

  if (AS->loaded) {
    double dt = (AS->best_length - AS->optimal_length) / AS->optimal_length;
    ts = (int)(dt * 1000)/10.0;
    ts = "D: " + ts + "%";
    StatusBar->Panels->Items[4]->Text = ts;
  }

  itoa(step_count, line, 10);
  StatusBar->Panels->Items[5]->Text = line;

  AS_start = true;
  ASForm->Paint();
  ASForm->Update();
}
//---------------------------------------------------------------------------
void __fastcall TASForm::Button_loadClick(TObject *Sender)
{
  AS_start = false;
  step_count = 0;
  Digits1->Value = 0;
  // execute OpenDialog and Get the City Points & Optimal Solution
  OpenDialog1->Title = "Open a city points file";
  OpenDialog1->Filter = "City Points|*.tsp";
  if (!OpenDialog1->Execute())
    return;
  AnsiString pf = OpenDialog1->FileName;

  OpenDialog1->Title = "Open the optimal solution file";
  OpenDialog1->Filter = "Optimal Solution|*.opt";
  OpenDialog1->FileName = "";
  if (!OpenDialog1->Execute())
    return;
  AnsiString tf = OpenDialog1->FileName;

  // Get the number of cities
  char line[81];
  FILE * f = fopen(pf.c_str(), "rt");
  fgets(line, 80, f);
  line[ strlen(line) - 1] = NULL; // eliminate the new line character
  Edit_cities->Text = line;
  fclose(f);

  // initialization
  Repaint();

  AS = new cities(	Edit_cities->Text.ToInt(), 			// number of cities
            		Edit_ants->Text.ToInt(), 			// number of ants
            		Edit_alpha->Text.ToDouble(),		// alpha
            		Edit_beta->Text.ToDouble(),			// beta
            		Edit_eva->Text.ToDouble(),	 		//evaporation
            		Edit_Q->Text.ToDouble(), 			// Q
            		Edit_tolerance->Text.ToDouble()); 	// tolerance
  AS->load_city_map(pf, tf);
  AS->ini_visibility_and_tao();
  Button_step->Enabled = true;

  // display the optimal solution
  AnsiString ts = (int)(AS->optimal_length*1000)/1000.0;
  ts = "Op: " + ts;
  StatusBar->Panels->Items[3]->Text = ts;
  StatusBar->Panels->Items[1]->Text = "";
  StatusBar->Panels->Items[2]->Text = "";
  Button_load->Enabled = false;
  Button_write->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TASForm::Show_About(TObject *Sender)
{
	AboutBox->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TASForm::FormPaint(TObject *Sender)
{
  if (AS_start)
    AS->paint_result();
}
//---------------------------------------------------------------------------
void __fastcall TASForm::Change(TObject *Sender)
{
  ASForm->Repaint();
  ASForm->Update();
}
//---------------------------------------------------------------------------
void __fastcall TASForm::write_result(TObject *Sender)
{
  if (!AS_start)
    return;

  char line[256];

  if (AS->loaded)
    sprintf(line, "%3d %4d %.2lf %.2lf %.2lf %.2lf - %.4lf %5d %.4lf %s\n",
                  AS->nofc, AS->nofa,
                  AS->alpha, AS->beta, AS->evaporation, AS->Q,
   				  AS->best_length, AS->cycle_count_when_best_found,
                  AS->optimal_length, AS->file_loaded);
  else
    sprintf(line, "%3d %4d %.2lf %.2lf %.2lf %.2lf - %.4lf %5d %.4lf R(%s)\n",
                  AS->nofc, AS->nofa,
                  AS->alpha, AS->beta, AS->evaporation, AS->Q,
   				  AS->best_length, AS->cycle_count_when_best_found,
  				  0.0, Edit_seed->Text.c_str());
  fputs(line, output_file);
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -