# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# %cd /home/jovyan/work

from chordifier.KeyboardRenderer import KeyboardRenderer
from chordifier.experiments.KeyboardFactory import make

# -

keyboard = make([0, 0, 0, 0, 2, 2, 0, 0, 0, 0])
KeyboardRenderer(keyboard, 'Handheld keyboard, operated with thumbs').present()


from chordifier.algorithm.Pipeline import Pipeline
from chordifier.utils import vector


# +
# %%time

global PARAMETERS
PARAMETERS = {
    # general
    'keyboard': keyboard,
    'characters': 35,

    # sequencer
    'filename': "dataset/ngram.json",
    'length': 3,
    'samples': 1000,

    # dynamics
    'x_y_ratio': 1.5,
    'stiffness': vector(1.8, 1.3, 1.2, 1.1, 2, 2, 1.1, 1.2, 1.3, 1.8),

    # pruner
    'priority': vector(18000, 13000, 12000, 11000, 14500,
                       14516, 11008, 12004, 13002, 18001),
    'finger_priorities': 0.1,
    'average_offsets': 0.5,
    'deviation_of_offsets': 2,

    # evaluator
    'distances_travelled': 1,
    'chord_difficulties': 1,

    # optimizer
    'best': True,
    'generations': 200,
    'population_size': 300,
    'mate_probability': 0.5,
    'mutate_probability': 0.2,
    'mutate_independent_probability': 0.1,
    'select_tournament_size': 30,
}

global pipeline3
pipeline3 = Pipeline(PARAMETERS)

pipeline3.prepare()
global result3
result3 = pipeline3.optimize()


# +
# %%time

PARAMETERS["length"] = 2

global pipeline2
pipeline2 = Pipeline(PARAMETERS)

pipeline2.prepare()
global result2
result2 = pipeline2.optimize()


# +
# %%time

PARAMETERS["length"] = 1

global pipeline1
pipeline1 = Pipeline(PARAMETERS)

pipeline1.prepare()
global result1
result1 = pipeline1.optimize()


# +
import pprint

pp = pprint.PrettyPrinter(indent=4)

def print_results(title, pipeline, result):
    print("===", title, "===")
    print("fitness: ", -pipeline.optimizer.winner.keys[0].wvalues[0])
    print("mapping:")
    pp.pprint(result)
    print("===============")
    print()
    
def print_reevaluate(title, pipeline):
    permutation = pipeline.optimizer.winner.items[0]
    fitness = pipeline3.evaluator.evaluate(permutation)
    
    print("===", title, "===")
    print("fitness: ", fitness[0])
    print("===============")
    print()



# -

print_results("Pipeline 1", pipeline1, result1)
print_results("Pipeline 2", pipeline2, result2)
print_results("Pipeline 3", pipeline3, result3)


print_reevaluate("Pipeline 1", pipeline1)
print_reevaluate("Pipeline 2", pipeline2)
print_reevaluate("Pipeline 3", pipeline3)




StarcoderdataJupyterScriptsDedupFiltered