-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharc_puzzle_ai_assistant.py
More file actions
1244 lines (1052 loc) · 50.3 KB
/
arc_puzzle_ai_assistant.py
File metadata and controls
1244 lines (1052 loc) · 50.3 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
"""
AI Assistant for ARC Puzzle Editor
Interprets natural language commands and provides puzzle analysis
"""
import re
import json
from typing import Dict, List, Optional, Tuple, Any
from dataclasses import dataclass
import boto3
import os
from arc_unified_heuristics import unified_heuristics as heuristics_manager
from arc_verification_oracle import verification_oracle
@dataclass
class GridCommand:
"""Represents a parsed grid command"""
action: str # 'set_color', 'resize', 'copy', 'fill', 'clear'
params: Dict[str, Any]
class PuzzleAIAssistant:
"""AI Assistant for puzzle interaction with function calling"""
# Color name to number mapping
COLOR_MAP = {
'black': 0,
'blue': 1,
'red': 2,
'green': 3,
'yellow': 4,
'gray': 5, 'grey': 5,
'pink': 6, 'magenta': 6,
'orange': 7,
'light blue': 8, 'cyan': 8,
'brown': 9, 'maroon': 9
}
def __init__(self):
self.current_puzzle = None
self.output_grid = None
self.heuristics = [] # Store available heuristics
self.tools = [] # Store available tools
self.last_applied_heuristic = None # Track last heuristic for verification feedback
# Initialize Bedrock client
try:
self.bedrock = boto3.client(
service_name='bedrock-runtime',
region_name=os.environ.get('AWS_DEFAULT_REGION', 'us-east-1')
)
self.bedrock_available = True
except Exception as e:
print(f"Warning: Bedrock not available: {e}")
self.bedrock_available = False
def set_puzzle(self, puzzle: Dict):
"""Set the current puzzle context"""
self.current_puzzle = puzzle
# Initialize output grid from test input
if puzzle and 'test' in puzzle and puzzle['test']:
test_input = puzzle['test'][0]['input']
self.output_grid = [row[:] for row in test_input]
def set_output_grid(self, grid: List[List[int]]):
"""Update the output grid state"""
self.output_grid = grid
def set_heuristics(self, heuristics: List[Dict]):
"""Set available heuristics"""
self.heuristics = heuristics
def set_tools(self, tools: List[Dict]):
"""Set available tools"""
self.tools = tools
def process_command(self, user_input: str) -> Dict:
"""Process a natural language command"""
# If Bedrock is available, use it for more intelligent processing
if self.bedrock_available:
try:
response = self._query_bedrock(user_input)
if response:
return response
except Exception as e:
print(f"Bedrock query failed: {e}")
# Fall back to local processing
user_input = user_input.lower().strip()
# Try to parse as a command
command = self.parse_command(user_input)
if command:
result = self.execute_command(command)
return {
'type': 'command',
'command': command.action,
'result': result,
'message': self.format_command_result(command, result)
}
# Try to answer as a question
answer = self.answer_question(user_input)
if answer:
return {
'type': 'answer',
'message': answer
}
# Provide general help
return {
'type': 'help',
'message': self.get_help_message()
}
def parse_command(self, text: str) -> Optional[GridCommand]:
"""Parse natural language into a grid command"""
# Pattern: "make/set square/cell X,Y color"
color_pattern = r'(?:make|set|color|paint|change)\s+(?:square|cell|position)?\s*(\d+)\s*,\s*(\d+)\s+(?:to\s+)?(\w+(?:\s+\w+)?)'
match = re.search(color_pattern, text)
if match:
row, col, color_name = match.groups()
color_num = self._parse_color(color_name)
if color_num is not None:
return GridCommand('set_color', {
'row': int(row),
'col': int(col),
'color': color_num
})
# Pattern: "resize/change output to/grid WxH"
resize_pattern = r'(?:resize|change|set)\s+(?:the\s+)?(?:output|grid)\s+(?:to\s+)?(\d+)\s*[x×]\s*(\d+)'
match = re.search(resize_pattern, text)
if match:
width, height = match.groups()
return GridCommand('resize', {
'width': int(width),
'height': int(height)
})
# Pattern: "copy from input"
if 'copy' in text and 'input' in text:
return GridCommand('copy', {'source': 'input'})
# Pattern: "clear/reset output"
if any(word in text for word in ['clear', 'reset']) and 'output' in text:
return GridCommand('clear', {})
# Pattern: "fill region/area"
fill_pattern = r'fill\s+(?:from\s+)?(\d+),(\d+)\s+(?:to\s+)?(\d+),(\d+)\s+(?:with\s+)?(\w+)'
match = re.search(fill_pattern, text)
if match:
r1, c1, r2, c2, color_name = match.groups()
color_num = self._parse_color(color_name)
if color_num is not None:
return GridCommand('fill', {
'start': (int(r1), int(c1)),
'end': (int(r2), int(c2)),
'color': color_num
})
# Pattern: "make X,Y same as input X,Y"
copy_cell_pattern = r'make\s+(?:square|cell)?\s*(\d+),(\d+)\s+(?:the\s+)?same\s+(?:as|color)\s+(?:input\s+)?(?:at\s+)?(\d+),(\d+)'
match = re.search(copy_cell_pattern, text)
if match:
out_r, out_c, in_r, in_c = match.groups()
return GridCommand('copy_cell', {
'output': (int(out_r), int(out_c)),
'input': (int(in_r), int(in_c))
})
return None
def _parse_color(self, color_text: str) -> Optional[int]:
"""Parse color name or number to color index"""
color_text = color_text.strip().lower()
# Try as number first
try:
color_num = int(color_text)
if 0 <= color_num <= 9:
return color_num
except ValueError:
pass
# Try as color name
return self.COLOR_MAP.get(color_text)
def execute_command(self, command: GridCommand) -> Dict:
"""Execute a parsed command"""
if not self.output_grid:
return {'success': False, 'error': 'No puzzle loaded'}
try:
if command.action == 'set_color':
row, col = command.params['row'], command.params['col']
color = command.params['color']
if 0 <= row < len(self.output_grid) and 0 <= col < len(self.output_grid[0]):
self.output_grid[row][col] = color
return {'success': True, 'grid': self.output_grid}
else:
return {'success': False, 'error': f'Position ({row},{col}) out of bounds'}
elif command.action == 'resize':
width = command.params['width']
height = command.params['height']
new_grid = []
for i in range(height):
row = []
for j in range(width):
if i < len(self.output_grid) and j < len(self.output_grid[0]):
row.append(self.output_grid[i][j])
else:
row.append(0)
new_grid.append(row)
self.output_grid = new_grid
return {'success': True, 'grid': self.output_grid}
elif command.action == 'copy':
if self.current_puzzle and 'test' in self.current_puzzle:
test_input = self.current_puzzle['test'][0]['input']
self.output_grid = [row[:] for row in test_input]
return {'success': True, 'grid': self.output_grid}
return {'success': False, 'error': 'No input to copy from'}
elif command.action == 'clear':
for i in range(len(self.output_grid)):
for j in range(len(self.output_grid[0])):
self.output_grid[i][j] = 0
return {'success': True, 'grid': self.output_grid}
elif command.action == 'fill':
start = command.params['start']
end = command.params['end']
color = command.params['color']
for i in range(min(start[0], end[0]), min(max(start[0], end[0]) + 1, len(self.output_grid))):
for j in range(min(start[1], end[1]), min(max(start[1], end[1]) + 1, len(self.output_grid[0]))):
self.output_grid[i][j] = color
return {'success': True, 'grid': self.output_grid}
elif command.action == 'copy_cell':
out_pos = command.params['output']
in_pos = command.params['input']
if self.current_puzzle and 'test' in self.current_puzzle:
test_input = self.current_puzzle['test'][0]['input']
if (0 <= in_pos[0] < len(test_input) and
0 <= in_pos[1] < len(test_input[0]) and
0 <= out_pos[0] < len(self.output_grid) and
0 <= out_pos[1] < len(self.output_grid[0])):
color = test_input[in_pos[0]][in_pos[1]]
self.output_grid[out_pos[0]][out_pos[1]] = color
return {'success': True, 'grid': self.output_grid}
return {'success': False, 'error': 'Invalid positions'}
except Exception as e:
return {'success': False, 'error': str(e)}
return {'success': False, 'error': 'Unknown command'}
def format_command_result(self, command: GridCommand, result: Dict) -> str:
"""Format command result as human-readable message"""
if result.get('success'):
if command.action == 'set_color':
return f"✓ Set cell ({command.params['row']},{command.params['col']}) to color {command.params['color']}"
elif command.action == 'resize':
return f"✓ Resized grid to {command.params['width']}×{command.params['height']}"
elif command.action == 'copy':
return "✓ Copied input to output"
elif command.action == 'clear':
return "✓ Cleared output grid"
elif command.action == 'fill':
return f"✓ Filled region with color {command.params['color']}"
elif command.action == 'copy_cell':
return f"✓ Copied color from input {command.params['input']} to output {command.params['output']}"
else:
return f"✗ Failed: {result.get('error', 'Unknown error')}"
def answer_question(self, question: str) -> Optional[str]:
"""Answer questions about the puzzle"""
if not self.current_puzzle:
return "No puzzle loaded. Please load a puzzle first."
question = question.lower()
# Grid size questions
if 'grid size' in question or 'dimensions' in question:
if 'input' in question:
# Check which example
example_num = self._extract_number(question)
if example_num is not None and 'train' in self.current_puzzle:
if 0 < example_num <= len(self.current_puzzle['train']):
grid = self.current_puzzle['train'][example_num-1]['input']
return f"Input {example_num} has size {len(grid)}×{len(grid[0])}"
# Test input
if 'test' in question and 'test' in self.current_puzzle:
grid = self.current_puzzle['test'][0]['input']
return f"Test input has size {len(grid)}×{len(grid[0])}"
elif 'output' in question:
example_num = self._extract_number(question)
if example_num is not None and 'train' in self.current_puzzle:
if 0 < example_num <= len(self.current_puzzle['train']):
grid = self.current_puzzle['train'][example_num-1]['output']
return f"Output {example_num} has size {len(grid)}×{len(grid[0])}"
# Pattern questions
if 'pattern' in question:
return self.analyze_pattern()
# Color questions
if 'color' in question:
pos_match = re.search(r'(\d+),(\d+)', question)
if pos_match:
row, col = map(int, pos_match.groups())
return self.get_cell_color_info(row, col, question)
# Training example count
if 'how many' in question and ('example' in question or 'training' in question):
if 'train' in self.current_puzzle:
return f"There are {len(self.current_puzzle['train'])} training examples"
return None
def _extract_number(self, text: str) -> Optional[int]:
"""Extract a number from text"""
numbers = re.findall(r'\d+', text)
if numbers:
return int(numbers[-1]) # Use last number found
return None
def get_cell_color_info(self, row: int, col: int, context: str) -> str:
"""Get color information for a specific cell"""
if 'input' in context:
if 'test' in self.current_puzzle:
grid = self.current_puzzle['test'][0]['input']
if 0 <= row < len(grid) and 0 <= col < len(grid[0]):
color = grid[row][col]
color_name = self.get_color_name(color)
return f"Input cell ({row},{col}) is {color_name} (color {color})"
elif 'output' in context:
if self.output_grid:
if 0 <= row < len(self.output_grid) and 0 <= col < len(self.output_grid[0]):
color = self.output_grid[row][col]
color_name = self.get_color_name(color)
return f"Output cell ({row},{col}) is {color_name} (color {color})"
return f"Cell ({row},{col}) is out of bounds"
def get_color_name(self, color_num: int) -> str:
"""Get color name from number"""
color_names = ['black', 'blue', 'red', 'green', 'yellow', 'gray', 'pink', 'orange', 'light blue', 'brown']
if 0 <= color_num <= 9:
return color_names[color_num]
return f"color {color_num}"
def analyze_pattern(self) -> str:
"""Analyze the puzzle pattern"""
if not self.current_puzzle or 'train' not in self.current_puzzle:
return "No training examples to analyze"
train = self.current_puzzle['train']
# Check grid size changes
size_changes = []
for i, example in enumerate(train):
in_shape = (len(example['input']), len(example['input'][0]))
out_shape = (len(example['output']), len(example['output'][0]))
if in_shape != out_shape:
size_changes.append(f"Example {i+1}: {in_shape} → {out_shape}")
if size_changes:
return f"Grid size changes detected:\n" + "\n".join(size_changes)
# Check for color mappings
color_mappings = {}
for example in train:
for i in range(len(example['input'])):
for j in range(len(example['input'][0])):
in_color = example['input'][i][j]
out_color = example['output'][i][j]
if in_color != out_color:
if in_color not in color_mappings:
color_mappings[in_color] = set()
color_mappings[in_color].add(out_color)
if color_mappings:
mappings = []
for in_c, out_cs in color_mappings.items():
out_list = list(out_cs)
if len(out_list) == 1:
mappings.append(f"{self.get_color_name(in_c)} → {self.get_color_name(out_list[0])}")
if mappings:
return "Color transformations detected:\n" + "\n".join(mappings)
return "Pattern analysis: No simple transformations detected. The puzzle may involve complex spatial transformations."
def get_help_message(self) -> str:
"""Get help message with example commands"""
return """I can help you edit the output grid and answer questions about the puzzle!
**Example commands:**
• "Make square 3,3 red" - Set a cell color
• "Make cell 5,3 the same color as input 5,3" - Copy color from input
• "Resize output to 5x5" - Change grid size
• "Copy from input" - Copy entire input to output
• "Clear output" - Reset output to black
• "Fill from 0,0 to 2,2 with blue" - Fill a region
**Example questions:**
• "What is the grid size for input 2?" - Get dimensions
• "What color is input cell 3,4?" - Check cell color
• "How many training examples are there?" - Count examples
• "Analyze the pattern" - Pattern analysis
Try a command or ask a question!"""
def _query_bedrock(self, user_input: str) -> Optional[Dict]:
"""Query AWS Bedrock for intelligent response with full context"""
if not self.bedrock_available:
return None
# Prepare comprehensive context
context = self._build_context()
functions = self._get_available_functions()
prompt = f"""{context}
Available Functions:
{json.dumps(functions, indent=2)}
User command: {user_input}
You can analyze the puzzle, suggest patterns, modify the output grid, retrieve/create heuristics and tools.
If you need to perform an action, specify which function to call with parameters.
Provide helpful analysis and insights about the puzzle patterns.
"""
try:
# Use Claude via Bedrock
body = json.dumps({
"prompt": f"\n\nHuman: {prompt}\n\nAssistant:",
"max_tokens_to_sample": 200,
"temperature": 0.7,
"top_p": 0.9,
})
response = self.bedrock.invoke_model(
body=body,
modelId="anthropic.claude-v2",
accept="application/json",
contentType="application/json"
)
response_body = json.loads(response.get('body').read())
ai_response = response_body.get('completion', '').strip()
# Also try to extract any commands from the response
command = self.parse_command(user_input.lower())
if command:
result = self.execute_command(command)
return {
'type': 'command_with_explanation',
'command': command.action,
'result': result,
'message': ai_response + "\n\n" + self.format_command_result(command, result)
}
return {
'type': 'ai_response',
'message': ai_response
}
except Exception as e:
print(f"Bedrock query error: {e}")
return None
def _build_context(self) -> str:
"""Build comprehensive context for AI"""
context = "You are an AI assistant helping solve ARC AGI puzzles.\n\n"
if self.current_puzzle:
# Puzzle information
context += f"Current Puzzle: {self.current_puzzle.get('id', 'Unknown')}\n"
context += f"Training Examples: {len(self.current_puzzle.get('train', []))}\n"
# Training examples details
for i, example in enumerate(self.current_puzzle.get('train', [])):
inp = example.get('input', [])
out = example.get('output', [])
context += f"\nExample {i+1}:\n"
context += f" Input: {len(inp)}x{len(inp[0]) if inp else 0} grid\n"
context += f" Output: {len(out)}x{len(out[0]) if out else 0} grid\n"
# Analyze colors used
input_colors = set(cell for row in inp for cell in row)
output_colors = set(cell for row in out for cell in row)
context += f" Input colors: {sorted(input_colors)}\n"
context += f" Output colors: {sorted(output_colors)}\n"
# Test input details
if 'test' in self.current_puzzle and self.current_puzzle['test']:
test_input = self.current_puzzle['test'][0].get('input', [])
context += f"\nTest Input: {len(test_input)}x{len(test_input[0]) if test_input else 0} grid\n"
test_colors = set(cell for row in test_input for cell in row)
context += f"Test colors: {sorted(test_colors)}\n"
# Current output grid state
if self.output_grid:
context += f"\nCurrent Output Grid: {len(self.output_grid)}x{len(self.output_grid[0])} grid\n"
output_colors = set(cell for row in self.output_grid for cell in row)
context += f"Output colors: {sorted(output_colors)}\n"
# Available heuristics
if self.heuristics:
context += f"\nAvailable Heuristics: {len(self.heuristics)}\n"
for h in self.heuristics[:3]: # Show first 3
context += f" - {h.get('name', 'Unknown')}: {h.get('description', '')}\n"
# Available tools
if self.tools:
context += f"\nAvailable Tools: {len(self.tools)}\n"
for t in self.tools[:3]: # Show first 3
context += f" - {t.get('name', 'Unknown')}: {t.get('description', '')}\n"
return context
def _get_available_functions(self) -> List[Dict]:
"""Get list of functions the AI can call"""
return [
{
"name": "get_cell_color",
"description": "Get the color of a specific cell",
"parameters": {"grid_type": "input|output|test", "example_index": "int", "row": "int", "col": "int"}
},
{
"name": "set_output_cell",
"description": "Set the color of a cell in the output grid",
"parameters": {"row": "int", "col": "int", "color": "int (0-9)"}
},
{
"name": "resize_output_grid",
"description": "Resize the output grid",
"parameters": {"width": "int", "height": "int"}
},
{
"name": "copy_from_input",
"description": "Copy the test input to output grid",
"parameters": {}
},
{
"name": "clear_output",
"description": "Clear the output grid (set all to black/0)",
"parameters": {}
},
{
"name": "analyze_pattern",
"description": "Analyze patterns in the training examples",
"parameters": {}
},
{
"name": "get_all_heuristics",
"description": "Get all heuristics from knowledge base",
"parameters": {}
},
{
"name": "get_relevant_heuristics",
"description": "Get heuristics relevant to current puzzle",
"parameters": {}
},
{
"name": "rank_heuristics",
"description": "Rank heuristics by effectiveness for this puzzle",
"parameters": {"heuristic_ids": "list of heuristic IDs to rank"}
},
{
"name": "apply_heuristic",
"description": "Apply a specific heuristic to the puzzle",
"parameters": {"heuristic_id": "string"}
},
{
"name": "test_heuristic",
"description": "Test a heuristic against training examples",
"parameters": {"heuristic_id": "string"}
},
{
"name": "create_heuristic",
"description": "Create a new heuristic and add to knowledge base",
"parameters": {
"name": "string",
"description": "string",
"pattern_type": "color_mapping|symmetry|size_transform|object_based|pattern_completion",
"conditions": "list of conditions when to apply",
"transformations": "list of transformations to apply",
"complexity": "int (1-5)",
"tags": "list of tags"
}
},
{
"name": "search_heuristics",
"description": "Search heuristics by name, description, or tags",
"parameters": {"query": "string"}
},
{
"name": "get_heuristics_stats",
"description": "Get statistics about heuristics knowledge base",
"parameters": {}
},
{
"name": "get_tools",
"description": "Get list of available tools",
"parameters": {}
},
{
"name": "apply_tool",
"description": "Apply a specific tool to the puzzle",
"parameters": {"tool_id": "string", "parameters": "dict"}
},
{
"name": "create_tool",
"description": "Create a new tool",
"parameters": {"name": "string", "description": "string", "code": "string"}
},
{
"name": "find_transformation",
"description": "Find the transformation pattern from training examples",
"parameters": {}
},
{
"name": "apply_transformation",
"description": "Apply a transformation to the output grid",
"parameters": {"transformation_type": "string", "parameters": "dict"}
},
{
"name": "submit_solution",
"description": "Submit current output grid for verification",
"parameters": {}
},
{
"name": "verify_solution",
"description": "Verify if a specific solution is correct",
"parameters": {"solution_grid": "2D array of integers (optional, uses current output if not provided)"}
},
{
"name": "get_verification_stats",
"description": "Get verification statistics for current puzzle",
"parameters": {}
},
{
"name": "check_if_solved",
"description": "Check if current puzzle has been solved",
"parameters": {}
}
]
def execute_function(self, function_name: str, parameters: Dict) -> Dict:
"""Execute a function call from the AI"""
try:
if function_name == "get_cell_color":
return self._get_cell_color(**parameters)
elif function_name == "set_output_cell":
return self._set_output_cell(**parameters)
elif function_name == "resize_output_grid":
return self._resize_output_grid(**parameters)
elif function_name == "copy_from_input":
return self._copy_from_input()
elif function_name == "clear_output":
return self._clear_output()
elif function_name == "analyze_pattern":
return self._analyze_pattern()
elif function_name == "get_all_heuristics":
return {"heuristics": heuristics_manager.get_all_heuristics()}
elif function_name == "get_relevant_heuristics":
return self._get_relevant_heuristics()
elif function_name == "rank_heuristics":
return self._rank_heuristics(**parameters)
elif function_name == "apply_heuristic":
return self._apply_heuristic(**parameters)
elif function_name == "test_heuristic":
return self._test_heuristic(**parameters)
elif function_name == "create_heuristic":
return self._create_heuristic(**parameters)
elif function_name == "search_heuristics":
return {"heuristics": heuristics_manager.search_heuristics(parameters.get("query", ""))}
elif function_name == "get_heuristics_stats":
return heuristics_manager.get_statistics()
elif function_name == "get_tools":
return {"tools": self.tools}
elif function_name == "apply_tool":
return self._apply_tool(**parameters)
elif function_name == "create_tool":
return self._create_tool(**parameters)
elif function_name == "find_transformation":
return self._find_transformation()
elif function_name == "apply_transformation":
return self._apply_transformation(**parameters)
elif function_name == "submit_solution":
return self._submit_solution()
elif function_name == "verify_solution":
return self._verify_solution(**parameters)
elif function_name == "get_verification_stats":
return self._get_verification_stats()
elif function_name == "check_if_solved":
return self._check_if_solved()
else:
return {"error": f"Unknown function: {function_name}"}
except Exception as e:
return {"error": str(e)}
def _get_cell_color(self, grid_type: str, example_index: int, row: int, col: int) -> Dict:
"""Get color of a specific cell"""
if not self.current_puzzle:
return {"error": "No puzzle loaded"}
try:
if grid_type == "output":
if self.output_grid:
return {"color": self.output_grid[row][col]}
elif grid_type == "test":
test_input = self.current_puzzle['test'][0]['input']
return {"color": test_input[row][col]}
elif grid_type == "input":
example = self.current_puzzle['train'][example_index]
return {"color": example['input'][row][col]}
else:
return {"error": "Invalid grid type"}
except (IndexError, KeyError) as e:
return {"error": f"Invalid cell coordinates: {e}"}
def _set_output_cell(self, row: int, col: int, color: int) -> Dict:
"""Set color of output cell"""
if not self.output_grid:
return {"error": "No output grid"}
try:
self.output_grid[row][col] = color
return {"success": True, "message": f"Set cell ({row},{col}) to color {color}"}
except IndexError:
return {"error": "Invalid cell coordinates"}
def _resize_output_grid(self, width: int, height: int) -> Dict:
"""Resize the output grid"""
new_grid = [[0 for _ in range(width)] for _ in range(height)]
# Copy existing data
if self.output_grid:
for i in range(min(height, len(self.output_grid))):
for j in range(min(width, len(self.output_grid[0]))):
new_grid[i][j] = self.output_grid[i][j]
self.output_grid = new_grid
return {"success": True, "message": f"Resized output to {height}x{width}"}
def _copy_from_input(self) -> Dict:
"""Copy test input to output"""
if not self.current_puzzle or 'test' not in self.current_puzzle:
return {"error": "No test input available"}
test_input = self.current_puzzle['test'][0]['input']
self.output_grid = [row[:] for row in test_input]
return {"success": True, "message": "Copied test input to output"}
def _clear_output(self) -> Dict:
"""Clear output grid"""
if not self.output_grid:
return {"error": "No output grid"}
for i in range(len(self.output_grid)):
for j in range(len(self.output_grid[0])):
self.output_grid[i][j] = 0
return {"success": True, "message": "Cleared output grid"}
def _analyze_pattern(self) -> Dict:
"""Analyze patterns in the puzzle"""
if not self.current_puzzle:
return {"error": "No puzzle loaded"}
analysis = {
"patterns": [],
"transformations": [],
"observations": []
}
# Analyze training examples
for i, example in enumerate(self.current_puzzle.get('train', [])):
inp = example['input']
out = example['output']
# Check size changes
if len(inp) != len(out) or len(inp[0]) != len(out[0]):
analysis["observations"].append(f"Example {i+1}: Size change from {len(inp)}x{len(inp[0])} to {len(out)}x{len(out[0])}")
# Check color mappings
input_colors = set(cell for row in inp for cell in row)
output_colors = set(cell for row in out for cell in row)
if input_colors != output_colors:
analysis["observations"].append(f"Example {i+1}: Color set changed from {input_colors} to {output_colors}")
return analysis
def _get_relevant_heuristics(self) -> Dict:
"""Get heuristics relevant to current puzzle"""
if not self.current_puzzle:
return {"error": "No puzzle loaded"}
# Extract puzzle features
features = self._extract_puzzle_features()
# Get relevant heuristics
relevant = heuristics_manager.get_relevant_heuristics(features)
return {
"heuristics": relevant,
"total": len(relevant),
"puzzle_features": features
}
def _rank_heuristics(self, heuristic_ids: List[str]) -> Dict:
"""Rank heuristics by effectiveness"""
if not self.current_puzzle:
return {"error": "No puzzle loaded"}
puzzle_data = self._get_puzzle_data()
ranked = heuristics_manager.rank_heuristics(heuristic_ids, puzzle_data)
return {
"ranked_heuristics": ranked,
"top_recommendation": ranked[0] if ranked else None
}
def _apply_heuristic(self, heuristic_id: str) -> Dict:
"""Apply a heuristic to the puzzle"""
if not self.current_puzzle:
return {"error": "No puzzle loaded"}
# Track the heuristic being applied
self.last_applied_heuristic = heuristic_id
puzzle_data = self._get_puzzle_data()
result = heuristics_manager.apply_heuristic(heuristic_id, puzzle_data)
# If heuristic suggests specific transformations, try to apply them
if result.get("applied") and result.get("confidence", 0) > 0.5:
# Update output grid based on suggested transformations
# This is a simplified implementation
if "color_replace" in result.get("transformations_suggested", []):
self._apply_color_mapping()
elif "mirror_horizontal" in result.get("transformations_suggested", []):
self._apply_mirror_horizontal()
elif "crop_to_content" in result.get("transformations_suggested", []):
self._apply_crop()
return result
def _test_heuristic(self, heuristic_id: str) -> Dict:
"""Test a heuristic against training examples"""
if not self.current_puzzle:
return {"error": "No puzzle loaded"}
test_results = []
for i, example in enumerate(self.current_puzzle.get('train', [])):
puzzle_data = {
"puzzle_id": self.current_puzzle.get('id'),
"input": example['input'],
"expected_output": example['output']
}
result = heuristics_manager.test_heuristic(
heuristic_id,
puzzle_data,
example['output']
)
test_results.append({
"example": i + 1,
"success": result.get("success", False),
"analysis": result.get("analysis", "")
})
success_rate = sum(1 for r in test_results if r["success"]) / len(test_results) if test_results else 0
return {
"heuristic_id": heuristic_id,
"test_results": test_results,
"overall_success_rate": success_rate,
"recommendation": "Use this heuristic" if success_rate > 0.5 else "Try a different heuristic"
}
def _create_heuristic(self, name: str, description: str, pattern_type: str,
conditions: List[str], transformations: List[str],
complexity: int = 1, tags: List[str] = None) -> Dict:
"""Create a new heuristic and add to knowledge base"""
heuristic = heuristics_manager.create_heuristic(
name=name,
description=description,
pattern_type=pattern_type,
conditions=conditions,
transformations=transformations,
complexity=complexity,
tags=tags
)
# Return a success response
return {
"success": True,
"heuristic_id": heuristic.id,
"message": f"Added new heuristic: {name}",
"heuristic": {
"id": heuristic.id,
"name": heuristic.name,
"description": heuristic.description,
"pattern_type": heuristic.pattern_type,
"conditions": heuristic.conditions,
"transformations": heuristic.transformations,
"complexity": heuristic.complexity,
"tags": heuristic.tags
}
}
def _extract_puzzle_features(self) -> Dict:
"""Extract features from current puzzle for heuristic matching"""
if not self.current_puzzle:
return {}
features = {
"conditions": [],
"pattern_type": None,
"complexity": 1,
"size_change": False,
"colors": set()
}
# Analyze training examples
for example in self.current_puzzle.get('train', []):
inp = example.get('input', [])
out = example.get('output', [])
# Check size changes
if len(inp) != len(out) or (inp and out and len(inp[0]) != len(out[0])):
features["size_change"] = True
features["conditions"].append("size_transform")
else:
features["conditions"].append("same_grid_size")
# Collect colors
for row in inp:
features["colors"].update(row)
for row in out:
features["colors"].update(row)
# Check for patterns
if self._has_symmetry(inp) or self._has_symmetry(out):
features["conditions"].append("mirror_pattern")
features["pattern_type"] = "symmetry"
if self._has_repeating_pattern(inp):
features["conditions"].append("repeating_pattern")
if self._count_objects(inp) > 0:
features["conditions"].append("discrete_objects")
# Estimate complexity
features["complexity"] = min(5, max(1, len(features["colors"]) - 2))
return features
def _get_puzzle_data(self) -> Dict:
"""Get puzzle data for heuristic processing"""
if not self.current_puzzle:
return {}
return {
"puzzle_id": self.current_puzzle.get('id'),
"train": self.current_puzzle.get('train', []),
"test": self.current_puzzle.get('test', []),
"colors": list(self._extract_puzzle_features().get("colors", [])),
"size_change": self._extract_puzzle_features().get("size_change", False)
}
def _has_symmetry(self, grid: List[List[int]]) -> bool:
"""Check if grid has symmetry"""
if not grid:
return False
# Check horizontal symmetry
for i in range(len(grid) // 2):
if grid[i] != grid[-(i+1)]:
break