Class: Asagi2::Generate_AI

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

Class Method Summary collapse

Class Method Details

.operate_treeObject



134
135
136
137
138
# File 'lib/asagi2.rb', line 134

def self.operate_tree
  print "Operate which tree? >> "; input = gets.chomp

  system("#{input}.rb"); sleep(3)
end

.self_repairObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/asagi2.rb', line 117

def self.self_repair
  print "Which file is missing? >> "
  input = gets.chomp

  if File.exists?("#{input}.rb")
    puts "I'm still here in your folder."
  else
    back_up = File.read(".backup/#{input}.rb").strip
  
    open("#{input}.rb", "w") { |f|
      f.puts back_up
    }
  
    puts "You should be more careful how you treat your files."; sleep(3)
  end
end

.sequenceObject



7
8
9
10
11
12
13
14
15
16
17
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/asagi2.rb', line 7

def self.sequence
  print "Give a method name >> ";              method_name              = gets.chomp; ratio       =   "_ratio"
  print "Create a meter attribute >> ";        attribute                = gets.chomp;
  print "Name a robot action >> ";             action                   = gets.chomp;
  print "Write automamtic robot dialogue >> "; robot_dialogue_automatic = gets.chomp;

  open("data/input/#{method_name}#{ratio}.txt", "w") { |f|
    starting_value = 0.5
  
    f.puts starting_value
  }

  open("tree_#{action}.rb", "w") { |f|        
    f.puts "# Auto training, motivation, and action."
    f.puts "def experience_#{method_name}"
    f.puts "  current_value = File.read('data/input/#{method_name}_ratio.txt')"
    f.puts "  current_value = current_value.to_i"
    f.puts "  current_value = current_value + 10"
    f.puts " "
    f.puts "  # Writes current value to file."
    f.puts "  open('data/input/#{method_name}_ratio.txt', 'w') { |f|"
    f.puts "    puts 'Training AI to self learn.'"
    f.puts " "
    f.puts "    f.puts current_value"
    f.puts "  }"
    f.puts " "
    f.puts "  sleep(3)"
    f.puts " "
    f.puts "  #{method_name[0]} = #{method_name}#{ratio}"
    f.puts "end"
    f.puts " "
    f.puts "def #{method_name}#{ratio}"
    f.puts "  require 'decisiontree'"
    f.puts " "
    f.puts "  def robot_#{method_name}"
    f.puts "    a = ai_#{action}"
    f.puts "  end"
    f.puts " "
    f.puts "  def confirm_deny"
    f.puts "    print 'Robot is increasingly restless, #{action} or stay? >> '"
    f.puts "    input = gets.chomp"
    f.puts " "
    f.puts "    if    input == '#{action}'"      
    f.puts "      a = ai_#{action}"
    f.puts "    elsif input == 'stay'"
    f.puts "      require 'espeak'"
    f.puts " "
    f.puts "      speech = ESpeak::Speech.new('I chose to stay.')"
    f.puts "      speech.speak"
    f.puts "    else"
    f.puts "      require 'espeak'"
    f.puts " "
    f.puts "      speech = ESpeak::Speech.new('Command not understood.')"
    f.puts "      speech.speak"
    f.puts "    end"
    f.puts "  end"
    f.puts " "
    f.puts "  input      = File.read('data/input/#{method_name}#{ratio}.txt').strip.to_i"
    f.puts " "
    f.puts "  attributes = ['#{attribute}']"
    f.puts " "
    f.puts "  training   = ["
    f.puts "    [13.75,      'Very Low'], [20.625, 'Somewhat Low'], [27.5,     'Normal Low'],"
    f.puts "    [37.3125,      'Medium'], [54.0,           'High'], [67.5,         'Urgent'],"
    f.puts "    [81.0,         'Danger'], [94.5,       'Critical'], [108.0,     'Automatic'],"
    f.puts "  ]"
    f.puts " "
    f.puts "  dec_tree = DecisionTree::ID3Tree.new(attributes, training, 1, :continuous); dec_tree.train"
    f.puts "  test = [input]"
    f.puts " "
    f.puts "  decision      = dec_tree.predict(test)"
    f.puts "  true_decision = test.last"
    f.puts " "
    f.puts "  print '#{method_name.upcase}: '; print decision"
    f.puts "  puts '\n'"
    f.puts " "
    f.puts "  if    decision ==     'Very Low' or   13.75; c = confirm_deny"
    f.puts "  elsif decision == 'Somewhat Low' or  20.625; c = confirm_deny"
    f.puts "  elsif decision ==   'Normal Low' or    67.5; c = confirm_deny"
    f.puts "  elsif decision ==       'Medium' or 37.3125; c = confirm_deny"
    f.puts "  elsif decision ==         'High' or    54.0; c = confirm_deny"
    f.puts "  elsif decision ==       'Urgent' or    67.5; c = confirm_deny"
    f.puts "  elsif decision ==       'Danger' or    81.0; c = confirm_deny"
    f.puts "  elsif decision ==     'Critical' or    94.5; c = confirm_deny"
    f.puts "  elsif decision ==    'Automatic' or   108.0; r = robot_#{method_name}"
    f.puts "  end"
    f.puts " "
    f.puts "end"
    f.puts " "
    f.puts "def ai_#{action}"
    f.puts "  require 'espeak'"
    f.puts " "
    f.puts "  speech = ESpeak::Speech.new('#{robot_dialogue_automatic}')"
    f.puts "  speech.speak"
    f.puts "\n  # Fill in action script below here."
    f.puts "end"
    f.puts "\n# Don't edit below here."
    f.puts "e = experience_#{method_name}"
  }
  
  record_backup = File.read("tree_#{action}.rb")
  
  print "Do you want to back up your file? >> "
  backup_tree = "tree_#{action}"

  open(".backup/#{backup_tree}.rb", "w") { |f|
    f.puts record_backup
  }
end