Пример #1
0
class Generalize(object):
  """docstring for Generalize"""
  def __init__(self):
    self.op=Optimal()
    self.para=self.op.read()
    pass

  def read_score(self):
    #读取经验
    f=open('Score','rb')
    for line in f:
      score=msgpack.loads(line.strip()) #去除回车等影响
      #print game
      yield score

  def run(self):
    for status, turn, score in self.read_score():
      if turn == 2: #还是将其全部变成1为自身的情况,便于处理
        for i in range(3): 
          for j in range(3):
            if status[i][j] !=0:
              status[i][j] = 3-status[i][j]
      #可以利用optimal中的calc计算权值
      cal_score, scheme = self.op.calc(status)
      for i in range(9): #一共有9个参数
        self.para[str(i)]+=scheme[i]*(score-cal_score)*0.1
      print "new para", self.para
    #写回到data中
    self.update_para()

  def update_para(self):
    open('data_new','wb').write(json.dumps(self.para))
Пример #2
0
 def gen_seq(self,status,turn):
   #根据状态和turn来产生序列
   import copy
   if turn not in (1,2): return None
   tmp={}
   tmp[1]=copy.deepcopy(status)
   tmp[2]=copy.deepcopy(status)
   #if turn=1 则 tmp2全颠倒1和2, 否则tmp1颠倒.  暂时只有开始状态,所以不用处理
   op=Optimal()
   seq=[]
   while self.check(tmp[turn]):
     next=op.optimal(tmp[turn])
     if not next: break #没的下了也结束
     seq.append(next)
     tmp[turn][next[0]][next[1]] = 1 #表示走了一步棋
     tmp[3-turn][next[0]][next[1]] = 2 #对方看起来的效果 3-turn表示另一家
     turn = 3-turn #换边
   return seq
Пример #3
0
def run_test(difficulty, algorithm, output_name):
	genotype = get_test_data_path(difficulty)
	if algorithm == 'greedy':
		g = Greedy(genotype)
		t = Tester(g, None, genotype, output_name)
		t.run_analysis()
	elif algorithm == 'optimal':
		o = Optimal(genotype)
		t = Tester(o, None, genotype, output_name)
		t.run_analysis()
	elif algorithm == 'exhaustive':
		e = Exhaustive(genotype)
		t = Tester(e, None, genotype, output_name)
		t.run_analysis()
Пример #4
0
def run_training(difficulty, algorithm, output_name):
	genotype, haplotype = get_training_file_paths(difficulty)
	if algorithm == 'greedy':
		g = Greedy(genotype)
		t = Tester(g, haplotype, genotype, output_name)
		t.run_analysis()
	elif algorithm == 'optimal':
		o = Optimal(genotype)
		t = Tester(o, haplotype, genotype, output_name)
		t.run_analysis()
	elif algorithm == 'exhaustive':
		e = Exhaustive(genotype)
		t = Tester(e, haplotype, genotype, output_name)
		t.run_analysis()
Пример #5
0
def test_optimal():
  op= Optimal()
  status=[[1, 0, 1], [0, 0, 2], [0, 0, 0]]
  print op.optimal(status)
  return op.optimal(status)
Пример #6
0
 def __init__(self):
   self.op=Optimal()
   self.para=self.op.read()
   pass