Advertisement
Guest User

Fran

a guest
Jun 18th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.49 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3.  
  4. use Math::Combinatorics;
  5. use List::Util qw(min max);
  6.  
  7. require 'Random.pl';
  8. require 'Matrix.pl';
  9. require 'Moves.pl';
  10.  
  11. my @files = glob("instances/*.txt");
  12. my @instance_data = ();
  13.  
  14. foreach my $file(@files)
  15. {
  16.   #open(INSTANCE,$file) || die("No pude abrir el archivo");
  17.   open(INSTANCE,"instances/GKD-Ic_4_n500_m50.txt") || die("No pude abrir el archivo");
  18.   my ($n,$m) = split(/ /,<INSTANCE>);
  19.   my @lines = <INSTANCE>;
  20.  
  21.   print "$file\n";
  22.   @instance_data = &create_matrix($n); # matrix with 0s
  23.  
  24.   # filling up the matrix with information from each file
  25.   foreach my $line(@lines)
  26.   {
  27.     chomp($line);
  28.     my ($i,$j,$val) = split(/ /,$line);
  29.     $instance_data[$i][$j] = $val;
  30.     $instance_data[$j][$i] = $val;
  31.   }
  32.  
  33.   my (@best_solution, @tlist, @tabu_list) = ();
  34.   my ($itercount,$best_cost,$current_cost) = 0;
  35.   my $tenure = 500;
  36.  
  37.   # get the very first solution wich is random
  38.   my @current_solution = &get_array_rand($n,$m);
  39.  
  40.   # set that first solution as the best one just for initialize
  41.   @best_solution = @current_solution;
  42.  
  43.   # calculate the cost of the solution
  44.   $best_cost = &getBestNeighboor(\@instance_data,\@current_solution);
  45.  
  46.   # the first solution is added to the tabu list
  47.   unshift(@tlist,@current_solution);
  48.   $itercount++; # this is because there is one element in tlist we to take it into account
  49.  
  50.   for my $i (1..1000)
  51.   {
  52.     # getting the permutations of the solution
  53.     # getting neighbors
  54.     @current_solution = &get_array_rand($n,$m);
  55.     $current_cost = &getBestNeighboor(\@instance_data,\@current_solution);
  56.     if (@current_solution ~~ @tlist)
  57.     {
  58.       print "TABU\n";
  59.       # Evaluation aspiration criteria
  60.       if ($current_cost > $best_cost)
  61.       {
  62.         $best_cost = $current_cost;
  63.         @best_solution = @current_solution;
  64.       }
  65.     } else {
  66.       if ($current_cost > $best_cost)
  67.       {
  68.         $best_cost = $current_cost;
  69.         @best_solution = @current_solution;
  70.       }
  71.       # the following lins just remove  or add a solution to the tabu list  
  72.       if ($itercount == $tenure )
  73.       {
  74.         my $curr_size = @current_solution; # get size of current solution
  75.         splice(@tlist,-$curr_size); # this will remove the last solution in tlist
  76.         unshift(@tlist,@current_solution);
  77.         $itercount = 1;
  78.       } else {
  79.         unshift(@tlist,@current_solution);
  80.         $itercount++;
  81.       }
  82.     }
  83.  
  84.     print "Best : $best_cost\n";
  85.   }
  86.   close(INSTANCE);
  87.   last;
  88. }
  89. exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement