Skip to content

Commit a186f02

Browse files
committed
Start tweaking ruby files so that we can gather stats on this.
1 parent 9524ede commit a186f02

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ book/*
22
deprecated/*
33
web/*
44
workspace/*
5-
src/*
65
*.py~
76
*.pyc
87
*/__pycache__/*

python/stat_runner.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#! /usr/bin/env python3
22

3+
"""
4+
This script assumes that all the demo scripts can be ran with a csv flag which
5+
will output the log trace as CSV (to stdout).
6+
7+
It further assumes that the last line of the CSV output is some kind of
8+
description about the best solution arrived at.
9+
"""
10+
311
import subprocess
412
import sys
513

src/algorithms/stochastic/iterated_local_search.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Iterated Local Search algorithm in the Ruby Programming Language
2-
32
# The Clever Algorithms Project: http://www.CleverAlgorithms.com
43
# (c) Copyright 2010 Jason Brownlee. Some Rights Reserved.
54
# This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Australia License.
@@ -65,7 +64,7 @@ def perturbation(cities, best)
6564
return candidate
6665
end
6766

68-
def search(cities, max_iterations, max_no_improv)
67+
def search(cities, max_iterations, max_no_improv, output_format="human")
6968
best = {}
7069
best[:vector] = random_permutation(cities)
7170
best[:cost] = cost(best[:vector], cities)
@@ -74,7 +73,11 @@ def search(cities, max_iterations, max_no_improv)
7473
candidate = perturbation(cities, best)
7574
candidate = local_search(candidate, cities, max_no_improv)
7675
best = candidate if candidate[:cost] < best[:cost]
77-
puts " > iteration #{(iter+1)}, best=#{best[:cost]}"
76+
if output_format == "csv" then
77+
puts "#{(iter + 1)},#{best[:cost]}"
78+
else # The default
79+
puts " > iteration #{(iter+1)}, best=#{best[:cost]}"
80+
end
7881
end
7982
return best
8083
end
@@ -93,7 +96,17 @@ def search(cities, max_iterations, max_no_improv)
9396
# algorithm configuration
9497
max_iterations = 100
9598
max_no_improv = 50
99+
100+
output_format = "human"
101+
if $*.length >= 1 then
102+
output_format = "csv"
103+
end
104+
96105
# execute the algorithm
97-
best = search(berlin52, max_iterations, max_no_improv)
98-
puts "Done. Best Solution: c=#{best[:cost]}, v=#{best[:vector].inspect}"
106+
best = search(berlin52, max_iterations, max_no_improv, output_format)
107+
if output_format == "csv" then
108+
puts "#{best[:cost]},#{best[:vector].join(",")}"
109+
else
110+
puts "Done. Best Solution: c=#{best[:cost]}, v=#{best[:vector].inspect}"
111+
end
99112
end

0 commit comments

Comments
 (0)