Represents a program in the database
| 60 | |
| 61 | @dataclass |
| 62 | class Program: |
| 63 | """Represents a program in the database""" |
| 64 | |
| 65 | # Program identification |
| 66 | id: str |
| 67 | code: str |
| 68 | language: str = "python" |
| 69 | |
| 70 | # Evolution information |
| 71 | parent_id: Optional[str] = None |
| 72 | generation: int = 0 |
| 73 | # Default to current time when created |
| 74 | timestamp: float = field(default_factory=time.time) |
| 75 | iteration_found: int = 0 # Track which iteration this program was found |
| 76 | |
| 77 | # Performance metrics dict |
| 78 | metrics: Dict[str, float] = field(default_factory=dict) |
| 79 | |
| 80 | # Derived features used for MAP-Elites |
| 81 | complexity: float = 0.0 |
| 82 | diversity: float = 0.0 |
| 83 | |
| 84 | # Metadata (e.g., island assignment, changes summary) |
| 85 | metadata: Dict[str, Any] = field(default_factory=dict) |
| 86 | |
| 87 | # Prompts used to generate this program (optional logging) |
| 88 | prompts: Optional[Dict[str, Any]] = None |
| 89 | |
| 90 | # Artifact storage |
| 91 | artifacts_json: Optional[str] = None # JSON-serialized small artifacts |
| 92 | artifact_dir: Optional[str] = None # Path to large artifact files on disk |
| 93 | |
| 94 | # Embedding vector for novelty rejection sampling |
| 95 | embedding: Optional[List[float]] = None |
| 96 | |
| 97 | def to_dict(self) -> Dict[str, Any]: |
| 98 | """Convert to dictionary representation""" |
| 99 | return asdict(self) |
| 100 | |
| 101 | @classmethod |
| 102 | def from_dict(cls, data: Dict[str, Any]) -> "Program": |
| 103 | """Create from dictionary representation""" |
| 104 | # Get the valid field names for the Program dataclass to ignore extra fields in JSON |
| 105 | valid_fields = {f.name for f in fields(cls)} |
| 106 | |
| 107 | # Filter the data to only include valid fields |
| 108 | filtered_data = {k: v for k, v in data.items() if k in valid_fields} |
| 109 | |
| 110 | # Log if we're filtering out any fields (useful for debugging schema changes) |
| 111 | if len(filtered_data) != len(data): |
| 112 | filtered_out = set(data.keys()) - set(filtered_data.keys()) |
| 113 | logger.debug(f"Filtered out unsupported fields when loading Program: {filtered_out}") |
| 114 | |
| 115 | return cls(**filtered_data) |
| 116 | |
| 117 | |
| 118 | class ProgramDatabase: |
no outgoing calls
no test coverage detected