forked from algorithmicsuperintelligence/openevolve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_adapter.py
More file actions
1438 lines (1217 loc) · 59 KB
/
Copy pathtask_adapter.py
File metadata and controls
1438 lines (1217 loc) · 59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Task adapter for converting AlgoTune tasks to OpenEvolve format.
This adapter extracts AlgoTune tasks from an external repository and converts them to OpenEvolve format,
creating the necessary initial_program.py, evaluator.py, and config.yaml files.
"""
import os
import sys
import importlib.util
import shutil
import ast
import inspect
from pathlib import Path
from typing import Dict, Any, Optional, List
import logging
class AlgoTuneTaskAdapter:
"""Adapter to convert AlgoTune tasks to OpenEvolve format."""
def __init__(self, algotune_path: Optional[str] = None, task: Optional[str] = None):
"""
Initialize the adapter.
Args:
algotune_path: Path to AlgoTune repository directory (e.g., /path/to/AlgoTune)
task: Task name to create OpenEvolve files for
"""
if algotune_path is None:
raise ValueError("Please specify algotune_path to the AlgoTune repository directory.")
self.algotune_path = Path(algotune_path)
self.algotune_tasks_path = self.algotune_path / "AlgoTuneTasks"
self.algotuner_path = self.algotune_path / "AlgoTuner"
self.output_path = Path(__file__).parent
self.task = task
# Validate paths exist
if not self.algotune_tasks_path.exists():
raise ValueError(f"AlgoTuneTasks directory not found at: {self.algotune_tasks_path}")
if not self.algotuner_path.exists():
raise ValueError(f"AlgoTuner directory not found at: {self.algotuner_path}")
# Add AlgoTune paths to Python path for importing
self._setup_import_paths()
# Load all available tasks
self._load_tasks()
if self.task is not None:
if self.task not in self.available_tasks:
raise ValueError(f"Task '{self.task}' not found. Available tasks: {list(self.available_tasks.keys())}")
self.task_info = self.available_tasks[self.task]
self.task_name = self.task # Use the task name directly
def _setup_import_paths(self):
"""Setup Python import paths for AlgoTune modules."""
# Add AlgoTune base directory to path
if str(self.algotune_path) not in sys.path:
sys.path.insert(0, str(self.algotune_path))
# Try to import AlgoTune modules
try:
from AlgoTuneTasks.base import TASK_REGISTRY
from AlgoTuneTasks.registry import TASK_REGISTRY as REGISTRY_TASK_REGISTRY
print(f"Successfully imported AlgoTune modules from {self.algotune_path}")
except ImportError as e:
print(f"Warning: Could not import AlgoTune tasks: {e}")
print(f"Make sure AlgoTune is properly installed and accessible")
print(f"AlgoTune path: {self.algotune_path}")
TASK_REGISTRY = {}
REGISTRY_TASK_REGISTRY = {}
def _load_tasks(self):
"""Load all available AlgoTune tasks."""
self.available_tasks = {}
# Scan the tasks directory
for task_dir in self.algotune_tasks_path.iterdir():
if task_dir.is_dir() and not task_dir.name.startswith('_'):
task_name = task_dir.name
description_file = task_dir / "description.txt"
task_file = task_dir / f"{task_name}.py"
if description_file.exists() and task_file.exists():
self.available_tasks[task_name] = {
'path': task_dir,
'description_file': description_file,
'task_file': task_file
}
print(f"Loaded {len(self.available_tasks)} tasks from {self.algotune_tasks_path}")
def get_task_description(self, task_name: str) -> str:
"""Get the description of a task."""
if task_name not in self.available_tasks:
raise ValueError(f"Task '{task_name}' not found. Available tasks: {list(self.available_tasks.keys())}")
description_file = self.available_tasks[task_name]['description_file']
with open(description_file, 'r') as f:
return f.read().strip()
def _extract_task_class_info(self, task_name: str) -> Dict[str, Any]:
"""Extract class information from the task file with improved method extraction."""
task_info = self.available_tasks[task_name]
# Read the task file
with open(task_info['task_file'], 'r') as f:
task_code = f.read()
# Parse the AST to find the class
try:
tree = ast.parse(task_code)
except Exception as e:
print(f"Error parsing AST for {task_name}: {e}")
raise
class_info = {
'name': None,
'solve_method': None,
'init_method': None,
'generate_problem_method': None,
'is_solution_method': None,
'imports': [],
'class_code': None
}
# Extract imports with improved filtering
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
import_str = f"import {alias.name}"
if alias.asname:
import_str += f" as {alias.asname}"
# Filter out AlgoTune-specific imports
if not any(x in import_str for x in ['AlgoTune', 'register_task', 'Task']):
class_info['imports'].append(import_str)
elif isinstance(node, ast.ImportFrom):
module = node.module or ""
for alias in node.names:
import_str = f"from {module} import {alias.name}"
if alias.asname:
import_str += f" as {alias.asname}"
# Filter out AlgoTune-specific imports
if not any(x in import_str for x in ['AlgoTune', 'register_task', 'Task']):
class_info['imports'].append(import_str)
# Find the task class and extract the solve method
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef):
# Check if this class inherits from Task or has the task name
is_task_class = False
if node.bases is not None:
for base in node.bases:
base_str = ast.unparse(base) if hasattr(ast, 'unparse') else str(base)
if 'Task' in base_str:
is_task_class = True
break
# Also check if the class name matches the task name (case-insensitive)
if not is_task_class and task_name.lower() in node.name.lower():
is_task_class = True
if is_task_class:
class_info['name'] = node.name
# Extract the entire class code
class_info['class_code'] = ast.unparse(node)
# Find the solve, __init__, and is_solution methods using AST
for item in node.body:
if isinstance(item, ast.FunctionDef) and item.name in ['solve', '__init__', 'is_solution']:
try:
# Get the source lines for this method
method_start = item.lineno - 1 # Convert to 0-based index
method_end = item.end_lineno if hasattr(item, 'end_lineno') else method_start + 1
# Extract the method source code
source_lines = task_code.split('\n')
method_source_lines = source_lines[method_start:method_end]
# Extract the method body with proper indentation
body_lines = []
def_indent = len(method_source_lines[0]) - len(method_source_lines[0].lstrip())
signature_end = 0
for i, line in enumerate(method_source_lines):
if ':' in line and line.strip().endswith(':'):
signature_end = i
break
for line in method_source_lines[signature_end + 1:]:
if line.strip():
line_indent = len(line) - len(line.lstrip())
if line_indent > def_indent:
dedented_line = line[def_indent:]
body_lines.append(' ' + dedented_line)
elif line_indent == def_indent and line.strip().startswith('def '):
break
elif line_indent == def_indent:
break
else:
body_lines.append('')
if body_lines:
min_indent = float('inf')
for line in body_lines:
if line.strip():
indent = len(line) - len(line.lstrip())
min_indent = min(min_indent, indent)
if min_indent != float('inf'):
fixed_lines = []
for line in body_lines:
if line.strip():
current_indent = len(line) - len(line.lstrip())
relative_indent = current_indent - min_indent
additional_spaces = relative_indent
new_indent = ' ' + (' ' * additional_spaces)
stripped = line.strip()
fixed_lines.append(new_indent + stripped)
else:
fixed_lines.append('')
body_lines = fixed_lines
if body_lines:
if item.name == 'solve':
class_info['solve_method'] = '\n'.join(body_lines)
elif item.name == '__init__':
class_info['init_method'] = '\n'.join(body_lines)
elif item.name == 'is_solution':
class_info['is_solution_method'] = '\n'.join(body_lines)
else:
if item.name == 'solve':
class_info['solve_method'] = ' # Placeholder for solve method\n pass'
elif item.name == '__init__':
class_info['init_method'] = ' # Placeholder for __init__ method\n pass'
elif item.name == 'is_solution':
class_info['is_solution_method'] = ' # Placeholder for is_solution method\n pass'
except Exception as e:
if item.name == 'solve':
class_info['solve_method'] = ' # Placeholder for solve method\n pass'
elif item.name == '__init__':
class_info['init_method'] = ' # Placeholder for __init__ method\n pass'
elif item.name == 'is_solution':
class_info['is_solution_method'] = ' # Placeholder for is_solution method\n pass'
return class_info
def _harmonize_solve_and_is_solution(self, solve_method: str, is_solution_method: str, task_name: str) -> tuple:
"""
Harmonize the formats between solve() and is_solution() methods.
Fixes common mismatches like returning numpy arrays vs expecting lists.
Args:
solve_method: The extracted solve method code
is_solution_method: The extracted is_solution method code
task_name: Name of the task for specific fixes
Returns:
Tuple of (harmonized_solve_method, harmonized_is_solution_method)
"""
import re
# Fix common type checking issues in is_solution
harmonized_is_solution = is_solution_method
# Replace strict list checking with flexible array/list checking
if 'isinstance(proposed_list, list)' in harmonized_is_solution:
harmonized_is_solution = harmonized_is_solution.replace(
'isinstance(proposed_list, list)',
'isinstance(proposed_list, (list, np.ndarray))'
)
# Fix error messages to reflect the change
harmonized_is_solution = harmonized_is_solution.replace(
"'transformed_image' is not a list.",
"'transformed_image' is not a list or array."
)
# Add conversion logic for arrays to lists where needed
if 'transformed_image' in harmonized_is_solution and task_name == 'affine_transform_2d':
# For affine_transform_2d, convert arrays to lists for validation
conversion_code = '''
# Convert numpy array to list if needed for validation
if isinstance(proposed_list, np.ndarray):
proposed_list = proposed_list.tolist()
'''
# Insert conversion code after extracting proposed_list
pattern = r'(proposed_list = solution\["transformed_image"\])'
harmonized_is_solution = re.sub(
pattern,
r'\1' + conversion_code,
harmonized_is_solution
)
# Fix fft_convolution to return list format instead of numpy array
if task_name == 'fft_convolution':
# Ensure the solve method returns list format
if 'convolution_result = signal.fftconvolve' in solve_method:
# Replace the solution return to ensure list format
solve_method = solve_method.replace(
'solution = {"convolution": convolution_result}',
'solution = {"convolution": convolution_result.tolist()}'
)
# Add similar fixes for other common patterns
# Handle empty array checks
if 'if proposed_list == []' in harmonized_is_solution:
harmonized_is_solution = harmonized_is_solution.replace(
'if proposed_list == []',
'if (isinstance(proposed_list, list) and proposed_list == []) or (isinstance(proposed_list, np.ndarray) and proposed_list.size == 0)'
)
# Fix numpy array shape mismatch issues
if 'operands could not be broadcast' in task_name or task_name == 'affine_transform_2d':
# Add proper array handling
array_handling = '''
# Ensure arrays are properly formatted
if isinstance(proposed_list, np.ndarray):
if proposed_list.size == 0:
proposed_list = []
else:
proposed_list = proposed_list.tolist()
'''
# Insert after variable extraction
if 'proposed_list = solution["transformed_image"]' in harmonized_is_solution:
harmonized_is_solution = harmonized_is_solution.replace(
'proposed_list = solution["transformed_image"]',
'proposed_list = solution["transformed_image"]' + array_handling
)
return solve_method, harmonized_is_solution
def _clean_init_method(self, init_method: str) -> str:
"""
Clean up extracted __init__ method body by removing docstrings and super() calls.
Keep only the actual initialization statements.
"""
lines = init_method.split('\n')
cleaned_lines = []
in_docstring = False
for line in lines:
stripped = line.strip()
# Skip docstring lines
if '"""' in line:
if in_docstring:
in_docstring = False
continue
else:
in_docstring = True
continue
if in_docstring:
continue
# Skip super() calls
if 'super().__init__' in line or 'super().__init__' in line:
continue
# Skip empty lines
if not stripped:
continue
# Keep actual initialization statements (assignments to self.*)
if stripped.startswith('self.') or '=' in stripped:
# Ensure proper indentation (8 spaces for method body)
cleaned_lines.append(' ' + stripped)
if cleaned_lines:
return '\n'.join(cleaned_lines)
else:
return ' pass'
def _generate_initial_program(self, task_name: str) -> str:
"""Generate the initial program for OpenEvolve based on the actual task implementation."""
task_info = self.available_tasks[task_name]
description = self.get_task_description(task_name)
class_info = self._extract_task_class_info(task_name)
if not class_info['name']:
raise ValueError(f"Could not find Task class in {task_name}")
# Create imports section - remove duplicates and filter problematic imports
unique_imports = []
seen_imports = set()
# Filter out AlgoTune-specific imports that won't be available
problematic_imports = [
'from AlgoTuneTasks.base import',
'import AlgoTuneTasks',
'from AlgoTuneTasks.',
'import AlgoTuneTasks.'
]
for imp in class_info['imports']:
# Skip problematic imports
if any(problematic in imp for problematic in problematic_imports):
continue
if imp not in seen_imports:
unique_imports.append(imp)
seen_imports.add(imp)
# Add essential imports for OpenEvolve environment
essential_imports = [
'import logging',
'import numpy as np',
'from typing import Any, Dict, List, Optional'
]
# Remove duplicate typing imports
unique_imports = [imp for imp in unique_imports if not imp.startswith('from typing import')]
for imp in essential_imports:
if imp not in seen_imports:
unique_imports.append(imp)
seen_imports.add(imp)
imports = "\n".join(unique_imports)
# Use the actual solve method from the original task
solve_method = class_info['solve_method']
if solve_method:
# The method body is already properly indented from extraction
method_body = solve_method
else:
# Fallback to task-specific method if extraction failed
method_body = self._generate_task_specific_method(task_name, solve_method, class_info)
# Use the actual __init__ method from the original task
init_method = class_info['init_method']
if init_method:
# Clean up the extracted __init__ method
init_method_body = self._clean_init_method(init_method)
else:
# Fallback to simple pass if extraction failed
init_method_body = ' pass'
# Use the actual is_solution method from the original task
is_solution_method = class_info['is_solution_method']
if is_solution_method:
# The method body is already properly indented from extraction
is_solution_method_body = is_solution_method
else:
# Fallback method if extraction failed
is_solution_method_body = ''' """Check if the provided solution is valid."""
# Placeholder validation - always returns True
# This should be replaced with actual validation logic
return True'''
# Harmonize solve and is_solution methods to fix format mismatches
if solve_method and is_solution_method:
method_body, is_solution_method_body = self._harmonize_solve_and_is_solution(
method_body, is_solution_method_body, task_name
)
# Clean the description for use in docstring
import re
docstring_description = description.replace('\\', '\\\\')
# Use simple string replacement instead of regex for better reliability
docstring_description = docstring_description.replace('\\x', '\\\\x')
docstring_description = docstring_description.replace('b\\x', 'b\\\\x')
# Additional fixes for problematic byte literals
# Replace byte literals with safer representations
docstring_description = re.sub(r'b\\\\x[0-9a-fA-F]{2}', 'b\'\\\\x00\'', docstring_description)
docstring_description = re.sub(r'\\\\x[0-9a-fA-F]{2}', '\\\\x00', docstring_description)
# Fix any remaining problematic patterns
docstring_description = docstring_description.replace('\\\\xencrypted', '\\\\x00encrypted')
docstring_description = docstring_description.replace('\\\\xauthentication', '\\\\x00authentication')
initial_program = f'''# EVOLVE-BLOCK-START
"""
{docstring_description}
This is the initial implementation that will be evolved by OpenEvolve.
The solve method will be improved through evolution.
"""
{imports}
class {class_info['name']}:
"""
Initial implementation of {task_name} task.
This will be evolved by OpenEvolve to improve performance and correctness.
"""
def __init__(self):
"""Initialize the {class_info['name']}."""
{init_method_body}
def solve(self, problem):
"""
Solve the {task_name} problem.
Args:
problem: Dictionary containing problem data specific to {task_name}
Returns:
The solution in the format expected by the task
"""
try:
{method_body}
except Exception as e:
logging.error(f"Error in solve method: {{e}}")
raise e
def is_solution(self, problem, solution):
"""
Check if the provided solution is valid.
Args:
problem: The original problem
solution: The proposed solution
Returns:
True if the solution is valid, False otherwise
"""
try:
{is_solution_method_body}
except Exception as e:
logging.error(f"Error in is_solution method: {{e}}")
return False
def run_solver(problem):
"""
Main function to run the solver.
This function is used by the evaluator to test the evolved solution.
Args:
problem: The problem to solve
Returns:
The solution
"""
solver = {class_info['name']}()
return solver.solve(problem)
# EVOLVE-BLOCK-END
# Test function for evaluation
if __name__ == "__main__":
# Example usage
print("Initial {task_name} implementation ready for evolution")
'''
return initial_program
def _generate_evaluator(self, task_name: str) -> str:
"""Generate the evaluator for OpenEvolve using the actual task implementation with baseline comparison."""
task_info = self.available_tasks[task_name]
description = self.get_task_description(task_name)
class_info = self._extract_task_class_info(task_name)
evaluator = f'''"""
Evaluator for the {task_name} task with baseline comparison
This evaluator compares OpenEvolve's evolved solutions against the reference
AlgoTune baseline implementation to measure performance improvements.
The speedup becomes the primary fitness score for evolution.
"""
import importlib.util
import numpy as np
import time
import concurrent.futures
import traceback
import logging
import sys
import os
from pathlib import Path
from typing import Dict, Any, Optional, List, Tuple
# Add AlgoTune to path for importing reference tasks
# These paths will be dynamically determined based on the AlgoTune installation
# The adapter will handle path setup when the evaluator is created
# Setup AlgoTune paths dynamically
def setup_algotune_paths():
"""Setup Python import paths for AlgoTune modules."""
# The AlgoTune path should be passed as a parameter to the evaluator
possible_algotune_paths = [
Path("{str(self.algotune_path)}"),
Path(__file__).parent.parent.parent.parent / "AlgoTune",
Path.home() / "github" / "AlgoTune",
]
algotune_base = None
for path in possible_algotune_paths:
if path.exists():
algotune_base = path
break
if algotune_base is None:
print("Warning: Could not find AlgoTune installation")
return False
# Add AlgoTune base directory to path
if str(algotune_base) not in sys.path:
sys.path.insert(0, str(algotune_base))
return True
# Setup paths and try to import AlgoTune tasks
if setup_algotune_paths():
try:
from AlgoTuneTasks.base import TASK_REGISTRY
# Import the specific {task_name} task to register it
from AlgoTuneTasks.{task_name}.{task_name} import {class_info['name']}
print("Successfully imported AlgoTune tasks and {task_name}")
except ImportError as e:
print(f"Error: Could not import AlgoTune tasks: {{e}}")
print("Make sure AlgoTune is properly installed and accessible")
TASK_REGISTRY = {{}}
else:
print("Warning: Could not setup AlgoTune paths")
TASK_REGISTRY = {{}}
def run_with_timeout(func, args=(), kwargs={{}}, timeout_seconds=30):
"""
Run a function with a timeout using concurrent.futures
Args:
func: Function to run
args: Arguments to pass to the function
kwargs: Keyword arguments to pass to the function
timeout_seconds: Timeout in seconds
Returns:
Result of the function or raises TimeoutError
"""
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(func, *args, **kwargs)
try:
result = future.result(timeout=timeout_seconds)
return result
except concurrent.futures.TimeoutError:
raise TimeoutError(f"Function timed out after {{timeout_seconds}} seconds")
def safe_convert(value):
"""Convert a value safely for evaluation"""
try:
if isinstance(value, (list, tuple)):
return [safe_convert(v) for v in value]
elif isinstance(value, np.ndarray):
return value.tolist()
else:
return value
except Exception:
return value
def calculate_speedup(baseline_time_ms: float, evolved_time_ms: float, is_valid: bool) -> Optional[float]:
"""
Calculate speedup between baseline and evolved solution.
Speedup = (Baseline Time) / (Evolved Time)
Higher is better.
Args:
baseline_time_ms: Time taken by baseline implementation
evolved_time_ms: Time taken by evolved solution
is_valid: Whether the evolved solution is valid
Returns:
Speedup value or None if calculation is not possible
"""
if not is_valid:
return None
if baseline_time_ms is None or baseline_time_ms <= 0:
return None
if evolved_time_ms is None:
return None
if evolved_time_ms <= 0:
return float('inf') # Infinite speedup for instant solution
return baseline_time_ms / evolved_time_ms
def measure_baseline_performance(task_instance, problem, num_runs=3, warmup_runs=1):
"""
Measure baseline performance using the original AlgoTune implementation.
Args:
task_instance: The AlgoTune task instance
problem: Problem to solve
num_runs: Number of timing runs
warmup_runs: Number of warmup runs
Returns:
Dictionary with baseline timing results
"""
try:
# Warmup runs
for _ in range(warmup_runs):
try:
task_instance.solve(problem)
except Exception:
pass # Ignore warmup errors
# Timing runs
times = []
for _ in range(num_runs):
start_time = time.perf_counter()
try:
result = run_with_timeout(task_instance.solve, args=(problem,), timeout_seconds=30)
end_time = time.perf_counter()
if result is not None:
elapsed_ms = (end_time - start_time) * 1000
times.append(elapsed_ms)
except Exception as e:
print(f"Baseline run failed: {{e}}")
continue
if not times:
return {{
"success": False,
"error": "All baseline runs failed",
"avg_time_ms": None,
"min_time_ms": None,
"std_time_ms": None
}}
return {{
"success": True,
"avg_time_ms": float(np.mean(times)),
"min_time_ms": float(np.min(times)),
"std_time_ms": float(np.std(times)),
"times": times
}}
except Exception as e:
return {{
"success": False,
"error": str(e),
"avg_time_ms": None,
"min_time_ms": None,
"std_time_ms": None
}}
def measure_evolved_performance(program, problem, num_runs=3, warmup_runs=1, timeout_seconds=30):
"""
Measure evolved solution performance.
Args:
program: The evolved program module
problem: Problem to solve
num_runs: Number of timing runs
warmup_runs: Number of warmup runs
timeout_seconds: Timeout per run
Returns:
Dictionary with evolved timing results
"""
try:
# Warmup runs
for _ in range(warmup_runs):
try:
run_with_timeout(program.run_solver, args=(problem,), timeout_seconds=timeout_seconds)
except Exception:
pass # Ignore warmup errors
# Timing runs
times = []
results = []
for _ in range(num_runs):
start_time = time.perf_counter()
try:
result = run_with_timeout(program.run_solver, args=(problem,), timeout_seconds=timeout_seconds)
end_time = time.perf_counter()
elapsed_ms = (end_time - start_time) * 1000
times.append(elapsed_ms)
results.append(result)
except TimeoutError:
print(f"Evolved solution timed out after {{timeout_seconds}} seconds")
continue
except Exception as e:
print(f"Evolved run failed: {{e}}")
continue
if not times:
return {{
"success": False,
"error": "All evolved runs failed",
"avg_time_ms": None,
"min_time_ms": None,
"std_time_ms": None,
"results": []
}}
return {{
"success": True,
"avg_time_ms": float(np.mean(times)),
"min_time_ms": float(np.min(times)),
"std_time_ms": float(np.std(times)),
"times": times,
"results": results
}}
except Exception as e:
return {{
"success": False,
"error": str(e),
"avg_time_ms": None,
"min_time_ms": None,
"std_time_ms": None,
"results": []
}}
def evaluate(program_path, config=None):
"""
Enhanced evaluation with baseline comparison for {task_name} task.
This evaluator:
1. Loads the evolved solve method from initial_program.py
2. Generates test problems using the original AlgoTune task
3. Measures baseline performance using original AlgoTune implementation
4. Measures evolved solution performance
5. Calculates speedup as primary fitness score
6. Validates correctness using the original task's validation method
Args:
program_path: Path to the evolved program file (initial_program.py)
config: Configuration dictionary with evaluator settings
Returns:
Dictionary of metrics including speedup as primary fitness score
"""
try:
# Load configuration
if config is None:
# Try to load config from YAML file first
try:
import yaml
from pathlib import Path
config_path = Path(__file__).parent / "config.yaml"
if config_path.exists():
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
else:
raise FileNotFoundError("config.yaml not found")
except Exception as e:
# Could not load config.yaml, using defaults
config = {{
"algotune": {{
"num_trials": 5,
"data_size": 100,
"timeout": 300,
"num_runs": 3,
"warmup_runs": 1
}}
}}
# Extract AlgoTune task-specific settings from config
algotune_config = config.get("algotune", {{}})
num_trials = algotune_config.get("num_trials", 5)
data_size = algotune_config.get("data_size", 100)
timeout_seconds = algotune_config.get("timeout", 300)
num_runs = algotune_config.get("num_runs", 3)
warmup_runs = algotune_config.get("warmup_runs", 1)
# Load the program
spec = importlib.util.spec_from_file_location("program", program_path)
program = importlib.util.module_from_spec(spec)
spec.loader.exec_module(program)
# Check if the required function exists
if not hasattr(program, "run_solver"):
print(f"Error: program does not have 'run_solver' function")
return {{
"correctness_score": 0.0,
"performance_score": 0.0,
"combined_score": 0.0,
"speedup_score": 0.0, # Primary fitness score
"baseline_comparison": {{
"mean_speedup": None,
"median_speedup": None,
"success_rate": 0.0,
"baseline_times": [],
"evolved_times": [],
"speedups": []
}},
"error": "Missing run_solver function",
}}
# Get the original task for reference solutions and problem generation
task_class = None
if "{task_name}" in TASK_REGISTRY:
task_class = TASK_REGISTRY["{task_name}"]
print(f"Successfully loaded {task_name} task from registry")
else:
print(f"Error: {task_name} task not found in TASK_REGISTRY")
print(f"Available tasks: {{list(TASK_REGISTRY.keys())}}")
raise Exception("Could not load {task_name} task from AlgoTune registry")
# Generate test problems and evaluate
correctness_scores = []
performance_scores = []
baseline_times = []
evolved_times = []
speedups = []
valid_count = 0
success_count = 0
for trial in range(num_trials):
try:
# Generate a test problem using the original task
if task_class:
task_instance = task_class()
problem = task_instance.generate_problem(n=data_size, random_seed=trial)
else:
raise Exception("Could not load original AlgoTune task for problem generation")
# Measure baseline performance
baseline_result = measure_baseline_performance(
task_instance, problem, num_runs, warmup_runs
)
if not baseline_result["success"]:
print(f"Trial {{trial}}: Baseline measurement failed: {{baseline_result.get('error', 'Unknown error')}}")
continue
# Measure evolved performance
evolved_result = measure_evolved_performance(
program, problem, num_runs, warmup_runs, timeout_seconds
)
if not evolved_result["success"]:
print(f"Trial {{trial}}: Evolved measurement failed: {{evolved_result.get('error', 'Unknown error')}}")
continue
# Validate evolved solution
correctness_score = 0.0
is_valid = False
if evolved_result["results"]:
# Use the first result for validation
evolved_solution = evolved_result["results"][0]
evolved_solution = safe_convert(evolved_solution)
try:
# Use the evolved program's own is_solution method for validation
# This ensures consistency between the extracted solve and validation logic
evolved_solver = program.{class_info['name']}()
is_valid = evolved_solver.is_solution(problem, evolved_solution)
correctness_score = 1.0 if is_valid else 0.0
except Exception as e:
print(f"Trial {{trial}}: Error checking solution validity with evolved is_solution: {{e}}")
correctness_score = 0.0
is_valid = False
# Calculate speedup
baseline_time = baseline_result["min_time_ms"] # Use minimum time for fair comparison
evolved_time = evolved_result["min_time_ms"]
speedup = calculate_speedup(baseline_time, evolved_time, is_valid)
# Store results
correctness_scores.append(correctness_score)
baseline_times.append(baseline_time)
evolved_times.append(evolved_time)
if speedup is not None:
speedups.append(speedup)
valid_count += 1
# Performance score based on execution time
performance_score = 1.0 / (1.0 + evolved_time) if evolved_time > 0 else 0.0
performance_scores.append(performance_score)
success_count += 1
except Exception as e:
print(f"Trial {{trial}}: Error - {{str(e)}}")
print(traceback.format_exc())
continue
# If all trials failed, return zero scores
if success_count == 0:
return {{
"correctness_score": 0.0,
"performance_score": 0.0,
"combined_score": 0.0,
"speedup_score": 0.0, # Primary fitness score
"baseline_comparison": {{
"mean_speedup": None,
"median_speedup": None,
"success_rate": 0.0,
"baseline_times": [],
"evolved_times": [],
"speedups": []
}},
"error": "All trials failed",
}}
# Calculate metrics
avg_correctness = float(np.mean(correctness_scores))
avg_performance = float(np.mean(performance_scores))