Class: Gargor::Individual

Inherits:
Object
  • Object
show all
Defined in:
lib/gargor/individual.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIndividual

Returns a new instance of Individual.



9
10
11
12
# File 'lib/gargor/individual.rb', line 9

def initialize
  @params = {}
  @fitness = nil
end

Instance Attribute Details

#fitnessObject

Returns the value of attribute fitness.



8
9
10
# File 'lib/gargor/individual.rb', line 8

def fitness
  @fitness
end

#paramsObject

Returns the value of attribute params.



8
9
10
# File 'lib/gargor/individual.rb', line 8

def params
  @params
end

Instance Method Details

#attackObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/gargor/individual.rb', line 80

def attack
  ret = nil;out = nil
  cmd = Gargor.opt('attack_cmd')
  log "==> attack"
  log "execute: #{cmd}"
  tms = Benchmark.realtime do
    out,ret = shell(cmd)
  end

  @fitness = Gargor.opt('evaluate_proc').call(ret,out,tms)
  log "fitness: #{@fitness}"
  @fitness
end

#deployObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gargor/individual.rb', line 58

def deploy
  Gargor.opt("target_nodes").each { |node|
    log "==> deploy to #{node}"
    cmd = Gargor.opt("target_cooking_cmd") % [node]
    log "    #{cmd}"
    out,r = shell(cmd)
    unless r == 0
      log "deploy failed",Logger::ERROR
      @fitness = 0
      sleep 1
      raise Gargor::DeployError
    end
  }
  true
end

#load_nowObject



28
29
30
31
32
33
34
35
# File 'lib/gargor/individual.rb', line 28

def load_now
  log "==> load current json"
  @params.each { |name,param|
    json = File.read(param.file)
    @params[name].value = JsonPath.on(json,param.path).first
  }
  self
end

#log(message, level = Logger::INFO) ⇒ Object



24
25
26
# File 'lib/gargor/individual.rb', line 24

def log message,level = Logger::INFO
  Gargor.log message,level
end

#overwrite_by(i, a, b) ⇒ Object

a/b の確率でiを上書きする



95
96
97
98
99
100
# File 'lib/gargor/individual.rb', line 95

def overwrite_by i,a,b
  @params.each { |name,param|
    @params[name] = i.params[name] if a > Gargor.float_rand(b)
  }
  self
end

#set_param(param, json) ⇒ Object



37
38
39
# File 'lib/gargor/individual.rb', line 37

def set_param param,json
  JSON.pretty_generate(JsonPath.for(json).gsub(param.path) { |v| param.value }.to_hash)
end

#set_paramsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gargor/individual.rb', line 41

def set_params
  log "==> set params"
  jsons = {}
  @params.each { |name,param|
    unless jsons.has_key?(param.file)
      log "load #{param.file}"
      jsons[param.file] = File.read(param.file)
    end
    jsons[param.file] = set_param(param,jsons[param.file])
    log " #{name}: #{param}"
  }
  jsons.each { |file,json|
    File.open(file,"w") { |f| f.write(json) }
    log " write #{file}"
  }
end

#shell(command) ⇒ Object



74
75
76
77
78
# File 'lib/gargor/individual.rb', line 74

def shell command
  out = `#{command}`
  ret = $?
  [out,ret.exitstatus]
end

#to_hashObject



18
19
20
21
22
# File 'lib/gargor/individual.rb', line 18

def to_hash
  hash = {}
  @params.each { |name,param| hash[name] = param.value }
  hash
end

#to_sObject



14
15
16
# File 'lib/gargor/individual.rb', line 14

def to_s
  [@params,@fitness].to_s
end