grape-bds 0.2.0__tar.gz → 0.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {grape_bds-0.2.0 → grape_bds-0.2.2}/PKG-INFO +2 -2
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/__init__.py +2 -2
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/el_ge/epigenetic_agent.py +83 -14
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/el_ge/lamarckian.py +96 -15
- {grape_bds-0.2.0 → grape_bds-0.2.2}/pyproject.toml +2 -2
- {grape_bds-0.2.0 → grape_bds-0.2.2}/tests/results/test_grammars/pagie1_test_seed7_gen010.bnf +1 -1
- {grape_bds-0.2.0 → grape_bds-0.2.2}/.gitignore +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/LICENSE +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/README.md +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/examples/example_classification.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/examples/example_increment.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/examples/example_parity.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/examples/example_regression.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/GP/ClassificationFPT/algorithms_gp.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/GP/ClassificationFPT/example_classification_fpt_gp.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/GP/ClassificationFPT/functions.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/GP/ClassificationFPT/fuzzify.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/GP/ClassificationFPT/paper_complete_script.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/GP/ClassificationFPT/selection.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/GP/SwitchingSelectionMethods/genetic.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/algorithms.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/DowNorm_test.txt +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/DowNorm_train.txt +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/Pagie1_test.txt +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/Pagie1_train.txt +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/SPECT.test +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/SPECT.train +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/USA_census_test.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/USA_census_train.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/Vladislavleva4_test.txt +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/Vladislavleva4_train.txt +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/adult.data +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/adult.test +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/adult_test.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/adult_test_targets.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/adult_training.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/australian.dat +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/bank-additional-full.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/banknote_Test.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/banknote_Train.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/compas-scores-two-years-violent.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/compas-scores-two-years.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/crx.data +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/data_lawsuit.mat +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/german.data +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/haberman.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/haberman_labels.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/horse-colic.data +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/horse-colic.test +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/iris.data +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/labels_lawsuit.mat +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/lupus.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/lupus_labels.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/parity3.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/parity4.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/parity5.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/pima.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/pima_labels.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/processed.cleveland.data +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/satellite_test.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/satellite_train.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/segmentation.data +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/segmentation.test +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/spaceshipTitanic_sample_submision.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/spaceshipTitanic_test.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/spaceshipTitanic_train.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/spambase.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/transfusion.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/transfusion_labels.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/vehicle.csv +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/datasets/wine.data +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/el_ge/__init__.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/el_ge/algorithms_elge.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/el_ge/remapper.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/functions.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/Banknote.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/Dow.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/Pagie1.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/TwoBoxes.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/Vladislavleva4.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/heartDisease.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/lawnmower64.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/parity3.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/parity4.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/parity4_v2.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/parity4_v3.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/parity5.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/parity6.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/quinticPolynomial.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/simpleIncrement.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grammars/spambase.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/grape/grape.py +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/tests/results/test_grammars/upgraded_Banknote.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/tests/results/test_grammars/upgraded_Pagie1.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/tests/results/test_grammars/upgraded_Vladislavleva4.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/tests/results/test_grammars/upgraded_heartDisease.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/tests/results/test_grammars/upgraded_parity4.bnf +0 -0
- {grape_bds-0.2.0 → grape_bds-0.2.2}/tests/test_el_ge_verification.py +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: grape-bds
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: Grammatical Evolution framework built with DEAP — now featuring Epigenetic-Lamarckian Grammatical Evolution (EL-GE)
|
|
5
5
|
Project-URL: Homepage, https://github.com/Caephas/grape
|
|
6
6
|
Project-URL: Repository, https://github.com/Caephas/grape
|
|
7
7
|
Project-URL: Upstream, https://github.com/bdsul/grape
|
|
8
|
-
Author: Allan de Lima
|
|
8
|
+
Author: Arinze Obidiegwu, Allan de Lima
|
|
9
9
|
License: BSD 3-Clause License
|
|
10
10
|
|
|
11
11
|
Copyright (c) 2022-2023, BDS Research Group at University of Limerick
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""GRAPE (Grammatical Algorithms in Python for Evolution) + EL-GE (v0.2.
|
|
1
|
+
"""GRAPE (Grammatical Algorithms in Python for Evolution) + EL-GE (v0.2.2)."""
|
|
2
2
|
|
|
3
3
|
from .grape import * # noqa: F401, F403
|
|
4
4
|
from .functions import * # noqa: F401, F403
|
|
@@ -14,4 +14,4 @@ from .el_ge import ( # noqa: F401
|
|
|
14
14
|
selEpsilonLexicase,
|
|
15
15
|
)
|
|
16
16
|
|
|
17
|
-
__version__ = "0.2.
|
|
17
|
+
__version__ = "0.2.2"
|
|
@@ -99,6 +99,7 @@ class EpigeneticBNFAgent:
|
|
|
99
99
|
ollama_url: str = "http://localhost:11434/api/generate",
|
|
100
100
|
temperature: float = 0.2,
|
|
101
101
|
timeout_sec: int = 30,
|
|
102
|
+
target_non_terminals: Optional[List[str]] = None,
|
|
102
103
|
):
|
|
103
104
|
self.initial_bnf_path = Path(initial_bnf_path)
|
|
104
105
|
self.grammar_class = grammar_class
|
|
@@ -107,6 +108,7 @@ class EpigeneticBNFAgent:
|
|
|
107
108
|
self.max_rules_per_epoch = max_rules_per_epoch
|
|
108
109
|
self.max_total_added_rules = max_total_added_rules
|
|
109
110
|
self.elite_fraction = elite_fraction
|
|
111
|
+
self.user_target_nts = target_non_terminals
|
|
110
112
|
|
|
111
113
|
self.backend = backend.lower().strip()
|
|
112
114
|
self.model_name = model_name
|
|
@@ -124,14 +126,74 @@ class EpigeneticBNFAgent:
|
|
|
124
126
|
for idx, nt in enumerate(grammar.non_terminals)
|
|
125
127
|
}
|
|
126
128
|
|
|
127
|
-
def
|
|
128
|
-
"""
|
|
129
|
+
def _discover_target_non_terminals(self, grammar) -> List[str]:
|
|
130
|
+
"""
|
|
131
|
+
Grammar- and framework-agnostic discovery of expandable compositional non-terminals.
|
|
132
|
+
1. If the user explicitly passed `target_non_terminals`, use those.
|
|
133
|
+
2. Otherwise, inspect the BNF production graph:
|
|
134
|
+
- Preserve classic `<op>` / `<e>` preference for standard parity/regression grammars.
|
|
135
|
+
- Skip unary wrapper non-terminals (k == 1, like `<Program> ::= <Init> <Body> <Measure>`)
|
|
136
|
+
and pure terminal leaf tables (0 child non-terminals in all productions).
|
|
137
|
+
- Select recursive multi-choice non-terminals (k >= 2) and their multi-choice
|
|
138
|
+
compositional children.
|
|
139
|
+
"""
|
|
140
|
+
if self.user_target_nts:
|
|
141
|
+
valid_user = [nt for nt in self.user_target_nts if nt in grammar.non_terminals]
|
|
142
|
+
if valid_user:
|
|
143
|
+
return valid_user
|
|
144
|
+
|
|
129
145
|
if "<op>" in grammar.non_terminals and "<e>" in grammar.non_terminals:
|
|
130
146
|
e_idx = grammar.non_terminals.index("<e>")
|
|
131
147
|
e_prods = [pr[0].strip() for pr in grammar.production_rules[e_idx]]
|
|
132
148
|
if "<op>" in e_prods and len(e_prods) == 2:
|
|
133
|
-
return "<op>"
|
|
134
|
-
|
|
149
|
+
return ["<op>"]
|
|
150
|
+
|
|
151
|
+
# If root non-terminal itself is multi-choice and recursive (e.g. `<e>` or `<log_op>`), return it
|
|
152
|
+
root_nt = grammar.non_terminals[0]
|
|
153
|
+
root_prods = [pr[0] for pr in grammar.production_rules[0]]
|
|
154
|
+
if len(root_prods) >= 2 and any(root_nt in p for p in root_prods):
|
|
155
|
+
return [root_nt]
|
|
156
|
+
|
|
157
|
+
# General BNF topology introspection for multi-stage / statement-block grammars
|
|
158
|
+
recursive_nts: List[str] = []
|
|
159
|
+
compositional_nts: List[str] = []
|
|
160
|
+
for idx, nt in enumerate(grammar.non_terminals):
|
|
161
|
+
prods = [pr[0] for pr in grammar.production_rules[idx]]
|
|
162
|
+
if len(prods) < 2:
|
|
163
|
+
continue
|
|
164
|
+
child_refs = [re.findall(r"\<([\(\)\w,-.]+)\>", p) for p in prods]
|
|
165
|
+
total_child_refs = sum(len(refs) for refs in child_refs)
|
|
166
|
+
if total_child_refs == 0:
|
|
167
|
+
# Pure terminal leaf list (e.g. qubits, constants, variable names)
|
|
168
|
+
continue
|
|
169
|
+
is_self_recursive = any(nt in p for p in prods)
|
|
170
|
+
if is_self_recursive:
|
|
171
|
+
recursive_nts.append(nt)
|
|
172
|
+
else:
|
|
173
|
+
compositional_nts.append(nt)
|
|
174
|
+
|
|
175
|
+
if recursive_nts:
|
|
176
|
+
# Prefer deeper recursive statement/block lists over top-level outer loop wrappers
|
|
177
|
+
discovered = list(reversed(recursive_nts))[:2]
|
|
178
|
+
for c_nt in compositional_nts:
|
|
179
|
+
if len(discovered) < 3:
|
|
180
|
+
discovered.append(c_nt)
|
|
181
|
+
return discovered
|
|
182
|
+
if compositional_nts:
|
|
183
|
+
return compositional_nts[:2]
|
|
184
|
+
return [root_nt]
|
|
185
|
+
|
|
186
|
+
def _target_non_terminal(self, grammar) -> str:
|
|
187
|
+
"""Select the primary recursive expression/operator/statement non-terminal to evolve."""
|
|
188
|
+
return self._discover_target_non_terminals(grammar)[0]
|
|
189
|
+
|
|
190
|
+
def _max_rule_len(self, grammar) -> int:
|
|
191
|
+
"""Compute an adaptive maximum rule length from the initial grammar's longest production."""
|
|
192
|
+
max_existing = max(
|
|
193
|
+
(len(pr[0]) for idx in range(len(grammar.non_terminals)) for pr in grammar.production_rules[idx]),
|
|
194
|
+
default=40,
|
|
195
|
+
)
|
|
196
|
+
return max(65, min(160, int(max_existing * 1.5)))
|
|
135
197
|
|
|
136
198
|
def _build_agent_prompt(
|
|
137
199
|
self,
|
|
@@ -154,6 +216,10 @@ class EpigeneticBNFAgent:
|
|
|
154
216
|
for i, ind in enumerate(parsimony_elites[:3])
|
|
155
217
|
]
|
|
156
218
|
sub_hints = [f" - {r}" for _nt, r, _s in ast_suggestions[:5]]
|
|
219
|
+
t_idx = grammar.non_terminals.index(target_nt)
|
|
220
|
+
sample_prods = [pr[0].strip() for pr in grammar.production_rules[t_idx][:2]]
|
|
221
|
+
example_str = " or ".join(f"`{p}`" for p in sample_prods) if sample_prods else f"`{target_nt}` expansion"
|
|
222
|
+
max_chars = self._max_rule_len(grammar)
|
|
157
223
|
|
|
158
224
|
return (
|
|
159
225
|
"You are the Epigenetic BNF Grammar Architect for Grammatical Evolution (GRAPE).\n"
|
|
@@ -171,8 +237,8 @@ class EpigeneticBNFAgent:
|
|
|
171
237
|
+ "\n\nSTRICT CONSTRAINTS:\n"
|
|
172
238
|
f"1. Every proposed rule MUST be a valid right-hand-side expansion for `{target_nt}` using ONLY "
|
|
173
239
|
"the exact function names, operators, variables, and non-terminals (`<...>`) already present in the BNF grammar above.\n"
|
|
174
|
-
"2. Keep rules short (<=
|
|
175
|
-
f"3. Do NOT repeat existing single rules; propose composite right-hand-side expressions (WITHOUT `{target_nt} ::=`), e.g.
|
|
240
|
+
f"2. Keep rules short (<= {max_chars} chars) and contain at most 2 non-terminal `<...>` slots so tree depth does not explode.\n"
|
|
241
|
+
f"3. Do NOT repeat existing single rules; propose composite right-hand-side expressions (WITHOUT `{target_nt} ::=`), e.g. {example_str}.\n"
|
|
176
242
|
"4. Respond ONLY with a valid JSON object in this exact format:\n"
|
|
177
243
|
'{"reasoning": "1-sentence explanation", "proposed_rules": ["rhs_expr_1", "rhs_expr_2"]}\n'
|
|
178
244
|
)
|
|
@@ -254,7 +320,8 @@ class EpigeneticBNFAgent:
|
|
|
254
320
|
|
|
255
321
|
def _is_syntactically_safe_rule(self, rule_str: str, grammar) -> bool:
|
|
256
322
|
"""Verify that `rule_str` only references valid non-terminals and has balanced parens."""
|
|
257
|
-
|
|
323
|
+
max_len = self._max_rule_len(grammar)
|
|
324
|
+
if not rule_str or "|" in rule_str or "::=" in rule_str or len(rule_str) > max_len:
|
|
258
325
|
return False
|
|
259
326
|
if rule_str.count("(") != rule_str.count(")"):
|
|
260
327
|
return False
|
|
@@ -312,8 +379,9 @@ class EpigeneticBNFAgent:
|
|
|
312
379
|
for i, nt in enumerate(grammar.non_terminals)
|
|
313
380
|
}
|
|
314
381
|
|
|
315
|
-
|
|
316
|
-
|
|
382
|
+
target_nts = self._discover_target_non_terminals(grammar)
|
|
383
|
+
max_lifted_len = self._max_rule_len(grammar)
|
|
384
|
+
is_multistage = len(target_nts) > 1 or target_nts[0] != grammar.non_terminals[0]
|
|
317
385
|
|
|
318
386
|
motif_elite_hits: Dict[Tuple[str, str], Set[int]] = defaultdict(set)
|
|
319
387
|
motif_rank_weight: Dict[Tuple[str, str], float] = defaultdict(float)
|
|
@@ -333,12 +401,12 @@ class EpigeneticBNFAgent:
|
|
|
333
401
|
if node.nt_name not in target_nts:
|
|
334
402
|
return
|
|
335
403
|
sub_depth = node.max_subtree_depth()
|
|
336
|
-
if sub_depth < 2 or sub_depth > 4:
|
|
404
|
+
if sub_depth < 2 or sub_depth > (6 if is_multistage else 4):
|
|
337
405
|
return
|
|
338
406
|
|
|
339
|
-
if 2 <= sub_depth <= 3 and len(node.children) >= 1:
|
|
407
|
+
if 2 <= sub_depth <= (5 if is_multistage else 3) and len(node.children) >= 1:
|
|
340
408
|
concrete = node.render_phenotype()
|
|
341
|
-
if "x[" in concrete and len(concrete) <= 50 and node.count_terminal_nodes() >= 2:
|
|
409
|
+
if ("x[" in concrete) and len(concrete) <= 50 and node.count_terminal_nodes() >= 2:
|
|
342
410
|
norm_c = concrete.replace(" ", "")
|
|
343
411
|
if norm_c not in existing_rules[node.nt_name]:
|
|
344
412
|
if any(op in concrete for op in ("+", "-", "*")) and not concrete.startswith("("):
|
|
@@ -348,10 +416,11 @@ class EpigeneticBNFAgent:
|
|
|
348
416
|
motif_rank_weight[key_c] += weight * 1.5
|
|
349
417
|
|
|
350
418
|
lifted = _extract_lifted_template(node)
|
|
351
|
-
if lifted and len(lifted) <=
|
|
419
|
+
if lifted and len(lifted) <= max_lifted_len and "<" in lifted and ">" in lifted:
|
|
352
420
|
arity = len(re.findall(r"\<([\(\)\w,-.]+)\>", lifted))
|
|
353
421
|
norm = lifted.replace(" ", "")
|
|
354
|
-
|
|
422
|
+
max_arity = 3 if is_multistage else 2
|
|
423
|
+
if 1 <= arity <= max_arity and norm not in existing_rules[node.nt_name] and norm != node.nt_name:
|
|
355
424
|
key = (node.nt_name, lifted)
|
|
356
425
|
motif_elite_hits[key].add(rank_idx)
|
|
357
426
|
motif_rank_weight[key] += weight * (1.4 if arity == 1 else 1.1)
|
|
@@ -59,6 +59,70 @@ def _make_digit_constant_node(val: float, nt_name: str, grammar) -> Optional[Der
|
|
|
59
59
|
)
|
|
60
60
|
|
|
61
61
|
|
|
62
|
+
def _eval_ind(evaluate_fn, ind, points_train) -> float:
|
|
63
|
+
"""Call `evaluate_fn(ind, points_train)` or `evaluate_fn(ind)` when `points_train is None`."""
|
|
64
|
+
if points_train is not None:
|
|
65
|
+
return evaluate_fn(ind, points_train)[0]
|
|
66
|
+
try:
|
|
67
|
+
return evaluate_fn(ind)[0]
|
|
68
|
+
except TypeError:
|
|
69
|
+
return evaluate_fn(ind, None)[0]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _is_noop_quantum_gate(gate_pheno: str) -> bool:
|
|
73
|
+
s = gate_pheno.replace('"', "").replace("\\n", "").replace(" ", "").strip().lower()
|
|
74
|
+
if any(s.startswith(p) for p in ("qc.id(", "qml.identity(", "cirq.i(")):
|
|
75
|
+
return True
|
|
76
|
+
for prefix in (
|
|
77
|
+
"qc.rx(0,", "qc.ry(0,", "qc.rz(0,", "qc.rxx(0,", "qc.ryy(0,", "qc.rzz(0,", "qc.rzx(0,", "qc.u(0,0,0,",
|
|
78
|
+
"qml.rx(0,", "qml.ry(0,", "qml.rz(0,", "cirq.rx(0)", "cirq.ry(0)", "cirq.rz(0)",
|
|
79
|
+
):
|
|
80
|
+
if s.startswith(prefix):
|
|
81
|
+
return True
|
|
82
|
+
return False
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _are_inverse_quantum_gates(g1_pheno: str, g2_pheno: str) -> bool:
|
|
86
|
+
s1 = g1_pheno.replace('"', "").replace("\\n", "").replace(" ", "").strip().lower()
|
|
87
|
+
s2 = g2_pheno.replace('"', "").replace("\\n", "").replace(" ", "").strip().lower()
|
|
88
|
+
if s1 == s2:
|
|
89
|
+
for inv_prefix in (
|
|
90
|
+
# Qiskit
|
|
91
|
+
"qc.h(", "qc.x(", "qc.y(", "qc.z(", "qc.cx(", "qc.cy(", "qc.cz(", "qc.swap(", "qc.ccx(", "qc.cswap(",
|
|
92
|
+
# PennyLane
|
|
93
|
+
"qml.hadamard(", "qml.paulix(", "qml.pauliy(", "qml.pauliz(", "qml.cnot(", "qml.cy(", "qml.cz(", "qml.swap(", "qml.toffoli(",
|
|
94
|
+
# Cirq
|
|
95
|
+
"cirq.h(", "cirq.x(", "cirq.y(", "cirq.z(", "cirq.cnot(", "cirq.cz(", "cirq.swap(", "cirq.toffoli(",
|
|
96
|
+
):
|
|
97
|
+
if s1.startswith(inv_prefix):
|
|
98
|
+
return True
|
|
99
|
+
for p1, p2 in (("qc.s(", "qc.sdg("), ("qc.sdg(", "qc.s("), ("qc.t(", "qc.tdg("), ("qc.tdg(", "qc.t(")):
|
|
100
|
+
if s1.startswith(p1) and s2.startswith(p2) and s1[len(p1):] == s2[len(p2):]:
|
|
101
|
+
return True
|
|
102
|
+
return False
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _is_tunable_numeric_nt(nt_name: str, grammar) -> bool:
|
|
106
|
+
"""
|
|
107
|
+
Grammar-agnostic detection of numeric / angle parameter leaf non-terminals
|
|
108
|
+
(e.g. `<c>`, `<Angle>`, `<theta>`, `<param>`, `<const>`).
|
|
109
|
+
"""
|
|
110
|
+
if nt_name == "<c>":
|
|
111
|
+
return True
|
|
112
|
+
if nt_name not in grammar.non_terminals:
|
|
113
|
+
return False
|
|
114
|
+
idx = grammar.non_terminals.index(nt_name)
|
|
115
|
+
prods = [pr[0].replace('"', "").strip() for pr in grammar.production_rules[idx]]
|
|
116
|
+
if len(prods) < 4:
|
|
117
|
+
return False
|
|
118
|
+
# Must be a pure terminal leaf table (no child `<...>`) where choices represent numbers/angles
|
|
119
|
+
if any("<" in p or ">" in p for p in prods):
|
|
120
|
+
return False
|
|
121
|
+
has_float_or_pi = any(("." in p or "pi" in p.lower()) for p in prods)
|
|
122
|
+
all_numeric_like = all(any(ch.isdigit() for ch in p) or "pi" in p.lower() for p in prods)
|
|
123
|
+
return has_float_or_pi and all_numeric_like
|
|
124
|
+
|
|
125
|
+
|
|
62
126
|
def _is_zero_phenotype(pheno: str) -> bool:
|
|
63
127
|
try:
|
|
64
128
|
return "x[" not in pheno and abs(float(pheno)) < 1e-9
|
|
@@ -232,6 +296,20 @@ def simplify_derivation_tree(node: DerivationNode, grammar) -> Tuple[DerivationN
|
|
|
232
296
|
if left.children[0].render_phenotype() == r_pheno:
|
|
233
297
|
return left.children[2], True
|
|
234
298
|
|
|
299
|
+
# 5. Any right-recursive statement/gate list (`<List> ::= <Item> <List>`) unitary simplification
|
|
300
|
+
if len(node.children) == 2 and node.children[1].nt_name == node.nt_name:
|
|
301
|
+
first_gate, tail_list = node.children[0], node.children[1]
|
|
302
|
+
g1_str = first_gate.render_phenotype()
|
|
303
|
+
# (a) Drop identity / zero-rotation gate
|
|
304
|
+
if _is_noop_quantum_gate(g1_str):
|
|
305
|
+
return tail_list, True
|
|
306
|
+
# (b) Cancel adjacent self-inverse gates: G * (G * Rest) -> Rest
|
|
307
|
+
if len(tail_list.children) == 2 and tail_list.children[1].nt_name == node.nt_name:
|
|
308
|
+
second_gate, inner_rest = tail_list.children[0], tail_list.children[1]
|
|
309
|
+
g2_str = second_gate.render_phenotype()
|
|
310
|
+
if _are_inverse_quantum_gates(g1_str, g2_str):
|
|
311
|
+
return inner_rest, True
|
|
312
|
+
|
|
235
313
|
return node, changed
|
|
236
314
|
|
|
237
315
|
|
|
@@ -289,7 +367,7 @@ def _causal_knockout_prune(
|
|
|
289
367
|
)
|
|
290
368
|
reMap_fn(ind, cand_genome, grammar, max_tree_depth, codon_consumption)
|
|
291
369
|
if not ind.invalid:
|
|
292
|
-
cand_fit = evaluate_fn
|
|
370
|
+
cand_fit = _eval_ind(evaluate_fn, ind, points_train)
|
|
293
371
|
is_better_or_equal = (
|
|
294
372
|
not math.isnan(cand_fit)
|
|
295
373
|
and ((cand_fit <= best_fit + 1e-12) if minimize else (cand_fit >= best_fit - 1e-12))
|
|
@@ -304,15 +382,15 @@ def _causal_knockout_prune(
|
|
|
304
382
|
return root, best_fit, pruned_any
|
|
305
383
|
|
|
306
384
|
|
|
307
|
-
def _collect_constant_nodes(node: DerivationNode, out: List[DerivationNode]) -> None:
|
|
385
|
+
def _collect_constant_nodes(node: DerivationNode, grammar, out: List[DerivationNode]) -> None:
|
|
308
386
|
if node.rule_str == "<c><c>.<c><c>" and len(node.children) == 4:
|
|
309
387
|
out.append(node)
|
|
310
388
|
return
|
|
311
|
-
if node.
|
|
389
|
+
if not node.children and _is_tunable_numeric_nt(node.nt_name, grammar):
|
|
312
390
|
out.append(node)
|
|
313
391
|
return
|
|
314
392
|
for c in node.children:
|
|
315
|
-
_collect_constant_nodes(c, out)
|
|
393
|
+
_collect_constant_nodes(c, grammar, out)
|
|
316
394
|
|
|
317
395
|
|
|
318
396
|
def lamarckian_simplify_individual(
|
|
@@ -371,7 +449,7 @@ def lamarckian_simplify_individual(
|
|
|
371
449
|
simplified_root = root.clone()
|
|
372
450
|
tree_changed = False
|
|
373
451
|
|
|
374
|
-
if tune_constants and evaluate_fn is not None and
|
|
452
|
+
if tune_constants and evaluate_fn is not None and old_fitness is not None:
|
|
375
453
|
# Step A: Causal Knock-Out Bloat Pruning (only on bloated trees: used_codons > 20)
|
|
376
454
|
if is_bloated:
|
|
377
455
|
simplified_root, best_fit, pruned_any = _causal_knockout_prune(
|
|
@@ -388,17 +466,20 @@ def lamarckian_simplify_individual(
|
|
|
388
466
|
rng=_rng,
|
|
389
467
|
)
|
|
390
468
|
|
|
391
|
-
# Step B: Coordinate Constant Tuning on
|
|
469
|
+
# Step B: Agnostic Coordinate Parameter / Constant Tuning on any numeric/angle leaf non-terminal
|
|
392
470
|
const_nodes: List[DerivationNode] = []
|
|
393
|
-
_collect_constant_nodes(simplified_root, const_nodes)
|
|
394
|
-
if const_nodes
|
|
395
|
-
c_idx = grammar.non_terminals.index("<c>")
|
|
396
|
-
k_c = grammar.n_rules[c_idx]
|
|
471
|
+
_collect_constant_nodes(simplified_root, grammar, const_nodes)
|
|
472
|
+
if const_nodes:
|
|
397
473
|
minimize = ind.fitness.weights[0] < 0
|
|
398
474
|
|
|
399
475
|
for _ in range(n_tune_steps):
|
|
400
476
|
target = _rng.choice(const_nodes)
|
|
401
477
|
digit_child = _rng.choice(target.children) if (target.rule_str == "<c><c>.<c><c>" and len(target.children) == 4) else target
|
|
478
|
+
c_nt = digit_child.nt_name
|
|
479
|
+
if c_nt not in grammar.non_terminals:
|
|
480
|
+
continue
|
|
481
|
+
c_idx = grammar.non_terminals.index(c_nt)
|
|
482
|
+
k_c = grammar.n_rules[c_idx]
|
|
402
483
|
|
|
403
484
|
old_rule_idx = digit_child.rule_index
|
|
404
485
|
old_rule_str = digit_child.rule_str
|
|
@@ -420,7 +501,7 @@ def lamarckian_simplify_individual(
|
|
|
420
501
|
)
|
|
421
502
|
reMap_fn(ind, cand_genome, grammar, max_tree_depth, codon_consumption)
|
|
422
503
|
if not ind.invalid:
|
|
423
|
-
cand_fit = evaluate_fn
|
|
504
|
+
cand_fit = _eval_ind(evaluate_fn, ind, points_train)
|
|
424
505
|
is_better = (
|
|
425
506
|
not math.isnan(cand_fit)
|
|
426
507
|
and ((cand_fit < best_fit - 1e-9) if minimize else (cand_fit > best_fit + 1e-9))
|
|
@@ -456,8 +537,8 @@ def lamarckian_simplify_individual(
|
|
|
456
537
|
ind.fitness.values = (old_fitness,)
|
|
457
538
|
return False
|
|
458
539
|
|
|
459
|
-
if evaluate_fn is not None and
|
|
460
|
-
new_fit = evaluate_fn
|
|
540
|
+
if evaluate_fn is not None and old_fitness is not None:
|
|
541
|
+
new_fit = _eval_ind(evaluate_fn, ind, points_train)
|
|
461
542
|
minimize = ind.fitness.weights[0] < 0
|
|
462
543
|
strictly_better = (not math.isnan(new_fit)) and (
|
|
463
544
|
(new_fit < old_fitness - 1e-9) if minimize else (new_fit > old_fitness + 1e-9)
|
|
@@ -555,8 +636,8 @@ def lamarckian_population_step(
|
|
|
555
636
|
target_worst = worst_slots[worst_idx]
|
|
556
637
|
worst_idx += 1
|
|
557
638
|
reMap_fn(target_worst, list(probe.genome), grammar, max_tree_depth, codon_consumption)
|
|
558
|
-
if evaluate_fn is not None
|
|
559
|
-
target_worst.fitness.values = evaluate_fn
|
|
639
|
+
if evaluate_fn is not None:
|
|
640
|
+
target_worst.fitness.values = (_eval_ind(evaluate_fn, target_worst, points_train),)
|
|
560
641
|
else:
|
|
561
642
|
target_worst.fitness.values = (new_fit,)
|
|
562
643
|
n_simplified += 1
|
|
@@ -4,14 +4,14 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "grape-bds"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.2"
|
|
8
8
|
description = "Grammatical Evolution framework built with DEAP — now featuring Epigenetic-Lamarckian Grammatical Evolution (EL-GE)"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = { file = "LICENSE" }
|
|
11
11
|
requires-python = ">=3.9"
|
|
12
12
|
authors = [
|
|
13
|
+
{ name = "Arinze Obidiegwu" },
|
|
13
14
|
{ name = "Allan de Lima" },
|
|
14
|
-
{ name = "Caephas (EL-GE Architecture & Packaging)" },
|
|
15
15
|
]
|
|
16
16
|
keywords = [
|
|
17
17
|
"grammatical-evolution",
|
{grape_bds-0.2.0 → grape_bds-0.2.2}/tests/results/test_grammars/pagie1_test_seed7_gen010.bnf
RENAMED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
<e> ::= <e>+<e> | <e>-<e> | <e>*<e> | pdiv(<e>,<e>) | psqrt(<e>) | np.sin(<e>) | np.tanh(<e>) | plog(<e>) | x[0] | x[1] | <c><c>.<c><c> | np.sin(x[0]
|
|
1
|
+
<e> ::= <e>+<e> | <e>-<e> | <e>*<e> | pdiv(<e>,<e>) | psqrt(<e>) | np.sin(<e>) | np.tanh(<e>) | plog(<e>) | x[0] | x[1] | <c><c>.<c><c> | np.sin(x[0]) | np.tanh(x[1])
|
|
2
2
|
<c> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{grape_bds-0.2.0 → grape_bds-0.2.2}/grape/GP/ClassificationFPT/example_classification_fpt_gp.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|