Browse Source

Keep track of best seen

master
Forest Belton 3 years ago
parent
commit
7c77661b61
2 changed files with 21 additions and 22 deletions
  1. +1
    -15
      ex.py
  2. +20
    -7
      gbso/optimize.py

+ 1
- 15
ex.py View File

@ -20,7 +20,7 @@ prgm = Program(
outputs = [R8.A]
test_cases = [TestCase()]
max_size = 4
num_iters = 1_000_000 # 10_000_000
num_iters = 100_000_000
initial_cost = cost(prgm, test_cases, outputs, prgm)
initial_cycles = prgm.perf()
@ -44,20 +44,6 @@ end_time = time()
optimized_prgm.display()
optimized_cost = cost(prgm, test_cases, outputs, optimized_prgm)
optimized_cycles = optimized_prgm.perf()
print(f"Cost: {optimized_cost}")
print(f"Cycles: {optimized_cycles}")
optimized_cost = cost(prgm, test_cases, outputs, optimized_prgm)
optimized_cycles = optimized_prgm.perf()
print(f"Cost: {optimized_cost}")
print(f"Cycles: {optimized_cycles}")
optimized_cost = cost(prgm, test_cases, outputs, optimized_prgm)
optimized_cycles = optimized_prgm.perf()

+ 20
- 7
gbso/optimize.py View File

@ -1,6 +1,6 @@
from math import exp, log
from random import random
from typing import List
from typing import List, Tuple
from gbso.program.test_case import Output, TestCase, eq_on_testcase
from gbso.program.mutate import mutate_program
@ -18,15 +18,16 @@ DEFAULT_PROB_INSN = 0.25
DEFAULT_PROB_INSN_UNUSED = 0.1
def cost(orig_prgm, test_cases, outputs, prgm) -> int:
def cost(orig_prgm, test_cases, outputs, prgm) -> Tuple[int, bool]:
c = prgm.perf() - orig_prgm.perf()
eq = c == 0
# print(f"init cost: {c}")
for test_case in test_cases:
c += eq_on_testcase(orig_prgm, prgm, test_case, outputs)
# print(f"cost after testcase: {c}")
return c
return c, eq
def optimize(
@ -34,7 +35,7 @@ def optimize(
max_size: int,
test_cases: List[TestCase],
outputs: List[Output],
beta: int = 0.75, # How far away in cost you are allowed to search
beta: int = 0.5, # How far away in cost you are allowed to search
num_iters: int = DEFAULT_NUM_ITERS,
prob_opcode: float = DEFAULT_PROB_OPCODE,
prob_operand: float = DEFAULT_PROB_OPERAND,
@ -45,16 +46,28 @@ def optimize(
padded_prgm = prgm.pad(max_size)
last_prgm = padded_prgm
last_cost = cost(padded_prgm, test_cases, outputs, last_prgm)
last_cost, _last_eq = cost(padded_prgm, test_cases, outputs, last_prgm)
best_prgm = padded_prgm
best_cost = last_cost
for _ in range(num_iters):
candidate_prgm = mutate_program(
last_prgm, prob_opcode, prob_operand, prob_swap, prob_insn, prob_insn_unused
)
candidate_cost = cost(padded_prgm, test_cases, outputs, candidate_prgm)
candidate_cost, candidate_eq = cost(
padded_prgm, test_cases, outputs, candidate_prgm
)
if candidate_cost < best_cost and candidate_eq:
best_prgm = candidate_prgm
best_cost = candidate_cost
if candidate_cost < last_cost - log(random()) / beta:
last_prgm = candidate_prgm
last_cost = candidate_cost
return last_prgm
print("last")
last_prgm.display()
return best_prgm

Loading…
Cancel
Save