Skip to content

Commit

Permalink
XOver almost working, oh yeah!
Browse files Browse the repository at this point in the history
  • Loading branch information
squillero committed Aug 25, 2023
1 parent 3460bab commit ea7e8a5
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 216 deletions.
6 changes: 3 additions & 3 deletions byron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@

__welcome__ = (
f'[bold]This is Byron v{__version__.rsplit(".", maxsplit=1)[0]} "[italic]{__codename__}[/italic]"[/]\n'
+ f"[bold](c) 2023 G. Squillero & A. Tonda — Licensed under Apache-2.0[/]"
+ f"[bold]© 2023 G. Squillero & A. Tonda — Licensed under Apache-2.0[/]"
)


Expand Down Expand Up @@ -142,7 +142,7 @@ def welcome(level=logging.DEBUG):
or not main_process
or user_messages.performance_warning(
"Paranoia checks are enabled in this notebook: performances can be significantly impaired\n"
+ "[see https://github.com/cad-polito-it/byron/blob/pre-alpha/docs/paranoia.md for details]"
+ "[see https://github.com/cad-polito-it/byron/blob/alpha/docs/paranoia.md for details]"
)
)
elif not notebook_mode:
Expand All @@ -151,7 +151,7 @@ def welcome(level=logging.DEBUG):
or not main_process
or user_messages.performance_warning(
"Paranoia checks are enabled: performances can be significantly impaired — consider using '-O'\n"
+ "[see https://github.com/cad-polito-it/byron/blob/pre-alpha/docs/paranoia.md for details]"
+ "[see https://github.com/cad-polito-it/byron/blob/alpha/docs/paranoia.md for details]"
)
)

Expand Down
12 changes: 12 additions & 0 deletions byron/classes/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ def __str__(self):

def evaluate_population(self, population: Population) -> None:
individuals = [(i, I, self.cook(population.dump_individual(i))) for i, I in population.not_finalized]
if not individuals:
logger.debug(f"PythonEvaluator: All individuals have already been finalized")
return

if self._max_workers == 1 or not self._backend:
# Simple, sequential, Python evaluator
Expand Down Expand Up @@ -398,6 +401,9 @@ def evaluate_population(self, population: Population) -> None:
if not g.is_finalized:
indexes.append(i)
phenotypes.append(self.cook(population.dump_individual(i)))
if not indexes:
logger.debug(f"MakefileEvaluator: All individuals have already been finalized")
return

with ThreadPoolExecutor(max_workers=self._max_workers, thread_name_prefix="byron$") as pool:
for i, result in zip(indexes, pool.map(self._evaluate, phenotypes)):
Expand Down Expand Up @@ -473,6 +479,9 @@ def __str__(self):

def evaluate_population(self, population: Population) -> None:
individuals = population.not_finalized
if not individuals:
logger.debug(f"ScriptEvaluator: All individuals have already been finalized")
return
files = list()
for idx, ind in individuals:
self._fitness_calls += 1
Expand Down Expand Up @@ -630,6 +639,9 @@ def evaluate_population(self, population: Population) -> None:
if not g.is_finalized:
indexes.append(i)
phenotypes.append(self.cook(population.dump_individual(i)))
if not indexes:
logger.debug(f"ParallelScriptEvaluator: All individuals have already been finalized")
return

with ThreadPoolExecutor(max_workers=self._max_workers, thread_name_prefix="byron$") as pool:
for i, result in zip(indexes, pool.map(self._evaluate, phenotypes)):
Expand Down
2 changes: 0 additions & 2 deletions byron/classes/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,6 @@ def _structure_tree(self) -> nx.DiGraph:
# PUBLIC METHODS

def run_paranoia_checks(self) -> bool:
assert self.valid, f"{PARANOIA_VALUE_ERROR}: Individual {self!r} is not valid"

# ==[check genome (structural)]======================================
assert self.genome == self._genome, f"{PARANOIA_VALUE_ERROR}: Panic!"
assert self.genome == self.G, f"{PARANOIA_VALUE_ERROR}: Panic!"
Expand Down
9 changes: 7 additions & 2 deletions byron/classes/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def is_fastened(self) -> bool:
def value(self):
assert (
self.is_fastened
), f"{PARANOIA_VALUE_ERROR}: Attempt to retrieve the value of an unfastened structural parameter"
), f"{PARANOIA_VALUE_ERROR}: Attempt to access the value of an unfastened structural parameter"
if self._node_reference is None:
return None
return next(
Expand All @@ -154,8 +154,13 @@ def value(self):
)

@value.setter
def value(self, target_node):
def value(self, target):
old_target_node = self.value
if isinstance(target, NodeReference):
target_node = target.node
assert target.graph == self._node_reference.graph, f"{PARANOIA_VALUE_ERROR}: Graph mismatch"
else:
target_node = target
if old_target_node is not None:
self._node_reference.graph.remove_edge(self._node_reference.node, old_target_node, self.key)
if target_node is not None:
Expand Down
2 changes: 1 addition & 1 deletion byron/classes/selement.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def _is_valid_debug(self, node_ref: 'NodeReference') -> None:
check_result = True
for f in self.__class__.NODE_CHECKS:
if not f(node_ref):
logger.info(f"{PARANOIA_SYSTEM_ERROR}: Failed check {f.__qualname__}({node_ref})")
logger.info(f"{PARANOIA_SYSTEM_ERROR}: Failed check {f}({node_ref})")
check_result = False
return check_result

Expand Down
6 changes: 5 additions & 1 deletion byron/framework/parameter_structural_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ def mutate(self, strength: float = 1.0) -> None:
else:
target = rrandom.choice(potential_targets, self.value, sigma=strength)
if target is None:
# get_all_macros(G, root=f, data=False, node_id=True)
new_node = unroll_selement(self._target_frame, self._node_reference.graph)
self._node_reference.graph.add_edge(NODE_ZERO, new_node.node, _type=FRAMEWORK)
initialize_subtree(new_node)
target = new_node.node
if first_macro:
target = get_all_macros(new_node.graph, root=new_node.node, data=False, node_id=True)[0]
else:
target = new_node.node

if not target:
raise ByronOperatorFailure
Expand Down
12 changes: 3 additions & 9 deletions byron/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,9 @@ def wrapper(*args: Individual | FrameABC, **kwargs):
offspring = func(*args, **kwargs)
except ByronOperatorFailure:
offspring = list()

if offspring is None:
offspring = []
elif isinstance(offspring, Individual):
deprecation_warning(
f"Genetic operators should return list[Individual]: found {func.__qualname__} -> Individual",
stacklevel_offset=0,
)
offspring = [offspring]
else:
if offspring is None:
offspring = []

assert all(
isinstance(i, Individual) for i in offspring
Expand Down
9 changes: 7 additions & 2 deletions docs/paranoia.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# PARANOID MODE

> All day long I think of things
> But nothing seems to satisfy
> Think I'll lose my mind if I don't
> Find something to pacify
>
> Can you help me occupy my brain?
> Oh, yeah
Expand Down Expand Up @@ -72,9 +77,9 @@ import byron
* :-1: All the code must be packed into one single cell
* :-1: Byron will not detect Jupyter anymore

### Tamper with the bytecode cache
### :bomb: Tamper with the bytecode cache

:warning: This hack may cause your system to become unstable and/or provide incorrect results. You acknowledge that you are solely responsible for any harm or damage that may result from it.
:warning: This hack may cause your system to become unstable and provide incorrect results. You acknowledge that you are solely responsible for any harm or damage that may result.

* Generate the optimized bytecode (e.g., run `python -O -m pytest`)
* Locate all the folders that contain compiled bytecode (e.g., `**/__pycache__`)
Expand Down
10 changes: 6 additions & 4 deletions examples/onemax/onemax-go/golang.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def framework():

# standard
main_prologue = byron.f.macro('package main\nfunc evolved_function() uint64 {{')
main_prologue.baptize('prologue')
imath = byron.f.macro(
'{var} {op}= {num}',
var=byron.f.global_reference(variable, creative_zeal=1),
Expand All @@ -42,12 +41,16 @@ def framework():
var2=(byron.f.global_reference(variable)),
var3=(byron.f.global_reference(variable)),
)
# NOTE[GX]: in macro definition "name='prologue'"" would be considered an (illegal) parameter
main_epilogue = byron.f.macro('return {var}\n}}', var=byron.f.global_reference(variable))
main_prologue.baptize('prologue')
main_epilogue.baptize('epilogue')

# proc
subs_prologue = byron.f.macro('func {_node}(foo uint64) uint64 {{', _label='')
subs_math = byron.f.macro('foo {op}= {num}', op=math_op, num=int64)
subs_epilogue = byron.f.macro('return foo\n}}')
subs = byron.f.sequence([subs_prologue, byron.f.bunch([subs_math], size=2), subs_epilogue])
subs = byron.f.sequence([subs_prologue, byron.f.bunch([subs_math], size=2), subs_epilogue], name='function')

call = byron.f.macro(
'{var1} = {sub}({var2})',
Expand All @@ -56,6 +59,5 @@ def framework():
var2=byron.f.global_reference(variable),
)

code = byron.f.bunch([imath, vmath, call], size=5)
main_epilogue = byron.f.macro('return {var}\n}}', var=byron.f.global_reference(variable))
code = byron.f.bunch([imath, vmath, call], size=5, name='body')
return byron.f.sequence((main_prologue, code, main_epilogue), max_instances=1)
53 changes: 3 additions & 50 deletions examples/onemax/onemax-go/onemax-go.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,19 @@
# SPDX-License-Identifier: Apache-2.0

import logging
import platform
import argparse

import byron


def define_library():
byron.f.set_parameter('_comment', '//')
byron.f.set_option('$dump_node_info', False)

# The parameter '{_byron}' can be used to get information on the current system. Available fields are:
# 'version', 'system', 'machine', 'python', and 'networkx'
# To get all information use the macro 'byron.f.Info'

int64 = byron.f.integer_parameter(0, 2**64)
math_op = byron.f.choice_parameter(['+', '-', '*', '/', '&', '^', '|'])
variable = byron.f.macro('var {_node} uint64', _label='')
variable.force_parent('prologue')

# standard
main_prologue = byron.f.macro('package main\nfunc evolved_function() uint64 {{')
main_prologue.baptize('prologue')
imath = byron.f.macro(
'{var} {op}= {num}',
var=byron.f.global_reference(variable, creative_zeal=1),
op=math_op,
num=int64,
)
vmath = byron.f.macro(
'{var1} = {var2} {op} {var3}',
var1=(byron.f.global_reference(variable, creative_zeal=1, first_macro=True)),
op=math_op,
var2=(byron.f.global_reference(variable)),
var3=(byron.f.global_reference(variable)),
)

# proc
subs_prologue = byron.f.macro('func {_node}(foo uint64) uint64 {{', _label='')
subs_math = byron.f.macro('foo {op}= {num}', op=math_op, num=int64)
subs_epilogue = byron.f.macro('return foo\n}}')
subs = byron.f.sequence([subs_prologue, byron.f.bunch([subs_math], size=3), subs_epilogue])

call = byron.f.macro(
'{var1} = {sub}({var2})',
var1=byron.f.global_reference(variable),
sub=byron.f.global_reference(subs, creative_zeal=1, first_macro=True),
var2=byron.f.global_reference(variable),
)

code = byron.f.bunch([imath, vmath, call], size=10)
main_epilogue = byron.f.macro('return {var}\n}}', var=byron.f.global_reference(variable))
return byron.f.sequence((main_prologue, code, main_epilogue))
import golang


def main():
top_frame = define_library()
top_frame = golang.framework()

evaluator = byron.evaluator.ScriptEvaluator('./evaluate-all.sh', filename_format="individual{i:06}.go")
# evaluator = byron.evaluator.ParallelScriptEvaluator(
# 'go', 'onemax.go', other_required_files=('main.go',), flags=('run',), timeout=10, default_result='0'
# 'go', 'onemax.go', other_required_files=('main.go',), flags=('run',), timeout=10, default_result='-1'
# )

byron.f.set_option('$dump_node_info', True)
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorials/evolution/genetic-operators.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"text": [
"/var/folders/31/dkl97hks2c14b663vl55pt440000gn/T/ipykernel_12399/1429530752.py:1: ByronPerformanceWarning: \n",
" Paranoia checks are enabled in this notebook: performances can be significantly impaired\n",
" [see https://github.com/cad-polito-it/byron/blob/pre-alpha/docs/paranoia.md for details]\n",
" [see https://github.com/cad-polito-it/byron/blob/alpha/docs/paranoia.md for details]\n",
" import byron\n"
]
}
Expand Down
4 changes: 2 additions & 2 deletions experiments/exceptions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"text": [
"/var/folders/31/dkl97hks2c14b663vl55pt440000gn/T/ipykernel_17807/1429530752.py:1: RuntimeWarning: \n",
" Paranoia checks are enabled in this notebook: performances can be significantly impaired\n",
" [see https://github.com/cad-polito-it/byron/blob/pre-alpha/docs/paranoia.md for details]\n",
" [see https://github.com/cad-polito-it/byron/blob/alpha/docs/paranoia.md for details]\n",
" import byron\n"
]
}
Expand All @@ -52,7 +52,7 @@
"output_type": "stream",
"text": [
"15:00:50 ▷ INFO ▷ byron::🖋: This is Byron v4!2.0a0.dev1 \"Meaning of Liff\"\n",
"15:00:50 ▷ INFO ▷ byron::🖋: (c) 2023 G. Squillero & A. Tonda — Licensed under Apache-2.0\n"
"15:00:50 ▷ INFO ▷ byron::🖋: © 2023 G. Squillero & A. Tonda — Licensed under Apache-2.0\n"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion experiments/g.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"text": [
"/var/folders/31/dkl97hks2c14b663vl55pt440000gn/T/ipykernel_4216/1429530752.py:1: ByronPerformanceWarning: \n",
" Paranoia checks are enabled in this notebook: performances can be significantly impaired\n",
" [see https://github.com/cad-polito-it/byron/blob/pre-alpha/docs/paranoia.md for details]\n",
" [see https://github.com/cad-polito-it/byron/blob/alpha/docs/paranoia.md for details]\n",
" import byron\n"
]
}
Expand Down
2 changes: 1 addition & 1 deletion experiments/h.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"text": [
"/var/folders/31/dkl97hks2c14b663vl55pt440000gn/T/ipykernel_7764/1429530752.py:1: ByronPerformanceWarning: \n",
" Paranoia checks are enabled in this notebook: performances can be significantly impaired\n",
" [see https://github.com/cad-polito-it/byron/blob/pre-alpha/docs/paranoia.md for details]\n",
" [see https://github.com/cad-polito-it/byron/blob/alpha/docs/paranoia.md for details]\n",
" import byron\n"
]
}
Expand Down
2 changes: 1 addition & 1 deletion experiments/i.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"text": [
"/var/folders/31/dkl97hks2c14b663vl55pt440000gn/T/ipykernel_6777/826285489.py:1: ByronPerformanceWarning: \n",
" Paranoia checks are enabled in this notebook: performances can be significantly impaired\n",
" [see https://github.com/cad-polito-it/byron/blob/pre-alpha/docs/paranoia.md for details]\n",
" [see https://github.com/cad-polito-it/byron/blob/alpha/docs/paranoia.md for details]\n",
" import byron\n"
]
}
Expand Down
Loading

0 comments on commit ea7e8a5

Please sign in to comment.