Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use warnings;
- use Math::Combinatorics;
- use List::Util qw(min max);
- require 'Random.pl';
- require 'Matrix.pl';
- require 'Moves.pl';
- my @files = glob("instances/*.txt");
- my @instance_data = ();
- foreach my $file(@files)
- {
- #open(INSTANCE,$file) || die("No pude abrir el archivo");
- open(INSTANCE,"instances/GKD-Ic_4_n500_m50.txt") || die("No pude abrir el archivo");
- my ($n,$m) = split(/ /,<INSTANCE>);
- my @lines = <INSTANCE>;
- print "$file\n";
- @instance_data = &create_matrix($n); # matrix with 0s
- # filling up the matrix with information from each file
- foreach my $line(@lines)
- {
- chomp($line);
- my ($i,$j,$val) = split(/ /,$line);
- $instance_data[$i][$j] = $val;
- $instance_data[$j][$i] = $val;
- }
- my (@best_solution, @tlist, @tabu_list) = ();
- my ($itercount,$best_cost,$current_cost) = 0;
- my $tenure = 500;
- # get the very first solution wich is random
- my @current_solution = &get_array_rand($n,$m);
- # set that first solution as the best one just for initialize
- @best_solution = @current_solution;
- # calculate the cost of the solution
- $best_cost = &getBestNeighboor(\@instance_data,\@current_solution);
- # the first solution is added to the tabu list
- unshift(@tlist,@current_solution);
- $itercount++; # this is because there is one element in tlist we to take it into account
- for my $i (1..1000)
- {
- # getting the permutations of the solution
- # getting neighbors
- @current_solution = &get_array_rand($n,$m);
- $current_cost = &getBestNeighboor(\@instance_data,\@current_solution);
- if (@current_solution ~~ @tlist)
- {
- print "TABU\n";
- # Evaluation aspiration criteria
- if ($current_cost > $best_cost)
- {
- $best_cost = $current_cost;
- @best_solution = @current_solution;
- }
- } else {
- if ($current_cost > $best_cost)
- {
- $best_cost = $current_cost;
- @best_solution = @current_solution;
- }
- # the following lins just remove or add a solution to the tabu list
- if ($itercount == $tenure )
- {
- my $curr_size = @current_solution; # get size of current solution
- splice(@tlist,-$curr_size); # this will remove the last solution in tlist
- unshift(@tlist,@current_solution);
- $itercount = 1;
- } else {
- unshift(@tlist,@current_solution);
- $itercount++;
- }
- }
- print "Best : $best_cost\n";
- }
- close(INSTANCE);
- last;
- }
- exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement