Example #1
0
def main():
    state = [{
        "type": "ROOK",
        "x": 2,
        "y": 3,
        "color": "WHITE"
    }, {
        "type": "QUEEN",
        "x": 2,
        "y": 5,
        "color": "WHITE"
    }, {
        "type": "QUEEN",
        "x": 2,
        "y": 7,
        "color": "WHITE"
    }, {
        "type": "QUEEN",
        "x": 2,
        "y": 1,
        "color": "WHITE"
    }, {
        "type": "BISHOP",
        "x": 6,
        "y": 3,
        "color": "BLACK"
    }]
    printBoard(state)
    print(rookEat(state[0], state))
def simulated_annealing(states):
    current = init(states)
    printBoard(current)
    print("Tentukan temperatur awal: ")
    temperature = int(input())
    print("Tentukan iterasi maksimal: ")
    max_i = int(input())
    print("Tentukan derajat pengurangan: ")
    t = int(input())
    i = 0
    while (i < max_i):
        print(i + 1)
        if temperature > 0:
            temperature = temperature - t
        print("temperature:", temperature)
        neighbor = generateNeighbor(current)
        next = neighbor[random.randint(0, len(neighbor) - 1)]
        next_cost = totalCost(next)
        current_cost = totalCost(current)
        delta_E = (next_cost['total_diff'] - next_cost['total_same']) - (
            current_cost['total_diff'] - current_cost['total_same'])
        if delta_E > 0:
            print("BETTER")
            current = next
        else:
            if temperature > 0:
                if delta_E == 0:
                    print("same")
                else:
                    print("worse")
                print("Delta E =", delta_E)
                probability = math.exp(delta_E / temperature)
                p = random.random()
                print("Probability =", probability)
                if p <= probability:
                    print("accepted")
                    current = next
        printBoard(current)
        print(totalCost(current))
        i += 1


# def main():
#   simulated_annealing()

# if __name__ == '__main__':
#   main()
Example #3
0
def main():
    state = [{
        "type": "BISHOP",
        "x": 3,
        "y": 3,
        "color": "BLACK"
    }, {
        "type": "BISHOP",
        "x": 4,
        "y": 4,
        "color": "BLACK"
    }, {
        "type": "BISHOP",
        "x": 2,
        "y": 4,
        "color": "BLACK"
    }]
    printBoard(state)
    print(bishopEat(state[0], state))
Example #4
0
def main():
    state = [{
        "type": "BISHOP",
        "x": 3,
        "y": 3,
        "color": "BLACK"
    }, {
        "type": "BISHOP",
        "x": 4,
        "y": 4,
        "color": "BLACK"
    }, {
        "type": "BISHOP",
        "x": 2,
        "y": 4,
        "color": "BLACK"
    }]
    printBoard(state)
    print(totalCost(state))
Example #5
0
def main():
    states = [{
        "type": "KNIGHT",
        "x": 2,
        "y": 3,
        "color": "WHITE"
    }, {
        "type": "QUEEN",
        "x": 4,
        "y": 4,
        "color": "BLACK"
    }, {
        "type": "QUEEN",
        "x": 4,
        "y": 2,
        "color": "WHITE"
    }, {
        "type": "QUEEN",
        "x": 2,
        "y": 1,
        "color": "WHITE"
    }]
    printBoard(states)
    print(horseEat(states[0], states))
def print_info(state):
    printBoard(state)
    print(state)
    print(totalCost(state))
    print()
Example #7
0
def hillClimbing(states):
  current = init(states)
  printBoard(current)
  print(totalCost(current))
  print("Tentukan iterasi maksimal: ")
  max_iteration = int(input())
  print()
  isLocalMax = False
  i = 0
  iteration = 1
  while not(isLocalMax) and iteration <= max_iteration:
    print("iteration number : ", iteration)
    current_cost = totalCost(current)
    neighbors = generateNeighbor(current)
    # print(neighbors)
    first = True
    i = 1
    for neighbor in neighbors:
      # print(i)
      # printBoard(neighbor)
      neighbor_cost = totalCost(neighbor)
      if first:
        best_neighbor = neighbor
        best_neighbor_cost = neighbor_cost
        first = False
        # print(neighbor_cost)
      elif neighbor_cost['total_diff'] - neighbor_cost['total_same'] > best_neighbor_cost['total_diff'] - best_neighbor_cost['total_same']:
        # print("better")
        best_neighbor = neighbor
        best_neighbor_cost = neighbor_cost
      # else:
        # print(neighbor_cost)
        # print("not best")
      i += 1

    if best_neighbor_cost['total_diff'] - best_neighbor_cost['total_same'] < current_cost['total_diff'] - current_cost['total_same']:
      isLocalMax = True
      print("LOCAL MAX")
      print("============== HASIL AKHIR ==============")
      printBoard(current)
      print(totalCost(current))
      print()
    else:
      print("best_neighbor")
      printBoard(best_neighbor)
      print(totalCost(best_neighbor))
      print()
      print("=============================================")
      print()
      current = best_neighbor
      print("current")
      printBoard(current)
      print(totalCost(current))
      print()
    iteration += 1
    if iteration > max_iteration:
      print("============== HASIL AKHIR ==============")
      printBoard(current)
      print(totalCost(current))
      print()

  return current