class Gargor::Dsl

Constants

GLOBAL_OPTS

Attributes

attack_proc[RW]
evaluate_proc[RW]
param_procs[RW]

Public Class Methods

new() click to toggle source
# File lib/gargor/dsl.rb, line 28
def initialize
  @param_procs = {}
  @attack_proc = nil
  @evaluate_proc = Proc.new { 0 }
  @fitness_precision = 100000000
  @population = 0
  @max_generations = 1
  @elite = 0
  @attack_cmd = "false"
  @target_nodes = []
  @state = nil
end

Public Instance Methods

attack(cmd,&block) click to toggle source
# File lib/gargor/dsl.rb, line 61
def attack cmd,&block
  @attack_cmd = cmd
  @attack_proc = block
end
create_individual(values = nil) click to toggle source
# File lib/gargor/dsl.rb, line 81
def create_individual values = nil
  individual = Individual.new
  param_procs.each { |name,proc|
    param =  Parameter.new(name)
    param.instance_eval(&proc)
    values && param.value = values[name]
    individual.params[name] = param
  }
  individual
end
evaluate(&block) click to toggle source
# File lib/gargor/dsl.rb, line 66
def evaluate &block
  @evaluate_proc = block
end
has_state?() click to toggle source
# File lib/gargor/dsl.rb, line 77
def has_state?
  !!@state
end
load_state(file=@state) click to toggle source
# File lib/gargor/dsl.rb, line 92
def load_state file=@state
  log "load state #{file}"
  state = JSON.parse(File.read(file))
  individuals = Individuals.new
  state.each { |i|
    individuals << create_individual(i)
  }
  individuals
rescue Errno::ENOENT =>e
  false
end
log(message,level=Logger::INFO) click to toggle source
# File lib/gargor/dsl.rb, line 53
def log message,level=Logger::INFO
  Gargor.log(message,level)
end
logger(*args, &block) click to toggle source
# File lib/gargor/dsl.rb, line 70
def logger *args, &block
  file = args.shift
  logger = Logger.new(Gargor.logfile(file),*args)
  block.call(logger) if block
  Gargor.logger = logger
end
options=(options) click to toggle source
# File lib/gargor/dsl.rb, line 47
def options= options
  GLOBAL_OPTS.each { |name|
    send(name.to_sym,options[name]) if options.has_key?(name)
  }
end
param(name,&block) click to toggle source
# File lib/gargor/dsl.rb, line 57
def param name,&block
  @param_procs[name] = block
end
params() click to toggle source
# File lib/gargor/dsl.rb, line 41
def params
  result = {}
  GLOBAL_OPTS.map { |name| result[name] = send(name)  }
  result
end
save_state(individuals,file = @state) click to toggle source
# File lib/gargor/dsl.rb, line 104
def save_state individuals,file = @state
  log "save state #{file}"
  json = individuals.to_json
  File.open(file,"w") { |f| f.write(json) }
end
target_nodes(*args) click to toggle source
# File lib/gargor/dsl.rb, line 16
def target_nodes *args
  return @target_nodes if args.count == 0
  nodes = args.shift
  @target_nodes = if nodes.is_a? Array
                    nodes
                  else
                    nodes.split(",")
                  end
end