Module: Algorithm

Defined in:
lib/algorithm.rb

Overview

Note:

This approach is quite ugly and might be refactored in future versions.

This Module provides factory functions for instanciating Optimizers for the OptimizeTask class. If you want to be able to use your Optimizer you must modify the Algorithm.for_name method.

Class Method Summary collapse

Class Method Details

.decode_values(values) ⇒ Hash

Returns a Hash of key-value pairs from a String in the format “key1=value1,key2=value2”.

Parameters:

  • values (String)

    String containing key-value pairs

Returns:

  • (Hash)

73
74
75
# File 'lib/algorithm.rb', line 73

def Algorithm.decode_values(values)
  Hash[(values.split ",").map { |value| value.split "=" }]
end

.for_name(algo_name, algo_values, termination_name, termination_value, flag_set, evaluator) ⇒ Optimizer

Returns instance of a Optimizer.

Parameters:

  • algo_name (String)

    Name of the Optimizer to be created.

  • algo_values (String)

    Key value pairs of attributes to be set for the optimization algorithm. Given in the format “key1=value1,key2=value2”.

  • termination_name (String)

    Name of an termination criterion to be used with the algorithm.

  • termination_value (String)

    Optional value for the termination criterion.

  • flag_set (FlagSet)

    FlagSet to be used in the optimization run

  • evaluator (Evaluator)

    Evaluator to be used in the optimization run.

Returns:

Raises:

  • (IndexError)

    if no Optimizer with given name can found.


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/algorithm.rb', line 18

def Algorithm.for_name(algo_name, algo_values, termination_name,
                       termination_value, flag_set, evaluator)
  termination_criterion = Algorithm.for_termination_name(termination_name,
                                                         termination_value)
  case algo_name
  when "Random Search"
    values = decode_values algo_values
    OptimizerFactory.
      generate_random_search(evaluator, values["parallel"].to_i,
                             termination_criterion,
                             values["seed"].to_i, flag_set)
  when "Hillclimber"
    values = decode_values algo_values
    OptimizerFactory.
      generate_hillclimber(evaluator, values["parallel"].to_i,
                           termination_criterion,
                           values["seed"].to_i, flag_set)
  when "Genetic Algorithm"
    values = decode_values algo_values
    OptimizerFactory.
      generate_genetic_optimizer(evaluator, values["population_size"].to_i,
                                 values["selection_method"],
                                 values["crossovers"].to_i,
                                 values["crossover_probability"].to_f,
                                 values["mutations"].to_i,
                                 values["mutation_probability"].to_f,
                                 termination_criterion,
                                 values["seed"].to_i, flag_set)
  when "Local Search"
    values = decode_values algo_values
    OptimizerFactory.
      generate_local_search(evaluator, values["parallel"].to_i,
                            termination_criterion,
                            values["seed"].to_i, flag_set,
                            values["distance"].to_i)
  else
    raise IndexError, "No Algorithm with name #{algo_name} found"
  end
end

.for_termination_name(termination_name, termination_value) ⇒ Lambda

Returns instance of a termination criterion.

Parameters:

  • termination_name (String)

    Name of the termination criterion.

  • termination_value

    Optional value for the criterion.

Returns:

  • (Lambda)

62
63
64
65
66
67
# File 'lib/algorithm.rb', line 62

def Algorithm.for_termination_name(termination_name, termination_value)
  case termination_name
  when "Steps"
    OptimizerFactory.generate_steps_criterion(termination_value.to_i)
  end
end