# !usr/bin/perl

# CS4771 - Machine Learning
# Group - Wei-Ang Lee, Naho Ogasawara, George Pen Kang Yi
# Genetic Tree Tweaking Algorithm

# After we had come up with a good tree to beat
# Edgar, we wanted to see if the tree would
# do well against Edgar and against Random Players.
# This code was used to specifically modify this
# tree (prefix notation):
#
# - / black_near_corners + white_near_corners white_edges black_edges
#
# we took the leaves and randomly generated them
# from the set of possible terminals, ran it through
# OthelloCache 50 times to come up with better results.
#
# we took the internal nodes (operators) and randomly
# generated those to produce a variant tree, with the same structure
# and with the same primitives, black_near_corners, white_near_corners,
# white_edges, and black_edges.


# the tree is broken up into 7 pieces, I called them bits.
# according to the tree above, we have
# op op primitive op primitive primitive primitive
# (op is for operator)
# each bit will be loaded into a string and sent off
# to --> java OthelloCache "PREFIX-TREE-STRING" <--
# this will run 50 times.

for ($i=0; $i < 50 ; $i++) {

$bit1 = "-";                
$bit2 = "/";
$bit3 = RNDprimitive();          # generates random leaves
$bit4 = "+";
$bit5 = RNDprimitive();
$bit6 = RNDprimitive();
$bit7 = RNDprimitive();

# $bit1 = RNDoperator();         # generates random operators
# $bit2 = RNDoperator();
# $bit3 = "black_near_corners";
# $bit4 = RNDoperator();
# $bit5 = "white_near_corners";
# $bit6 = "white_edges";
# $bit7 = "black_edges';

$string = "$bit1 $bit2 $bit3 $bit4 $bit5 $bit6 $bit7";


# system call for java OthelloCache below
system("java OthelloCache \"$string\"");  

print "THE PREFIX STRING: $string"; <STDIN>;
}



# generates random operators
sub RNDoperator() {
$save = rand;
$save = $save*10;
$save = substr($save,0,1) %4;

if (substr($save,0,1) == 0) { return "+"; }
if (substr($save,0,1) == 1) { return "-"; }
if (substr($save,0,1) == 2) { return "/"; }
if (substr($save,0,1) == 3) { return "*"; }
}



# generates random leaves
sub RNDprimitive() {
  $save = rand;
  $save = $save*10;

  if (substr($save,0,1) == 0) { return "black_near_corners"; }
  if (substr($save,0,1) == 1) { return "white"; }
  if (substr($save,0,1) == 2) { return "black"; }
  if (substr($save,0,1) == 3) { return "white_edges"; }
  if (substr($save,0,1) == 4) { return "black_edges"; }
  if (substr($save,0,1) == 5) { return "white_corners"; }
  if (substr($save,0,1) == 6) { return "black_corners"; }
  if (substr($save,0,1) == 7) { return "white_near_corners"; }
  if (substr($save,0,1) == 8) { return "black_near_corners"; }
  if (substr($save,0,1) == 9) { return "10"; }
}