|
|
""" |
|
|
Creative problem solving capabilities. |
|
|
""" |
|
|
import torch |
|
|
import torch.nn as nn |
|
|
import numpy as np |
|
|
from typing import List, Dict, Optional, Tuple |
|
|
import random |
|
|
|
|
|
|
|
|
class GenerativeModel(nn.Module): |
|
|
""" |
|
|
Generative model for creative generation (simplified diffusion/GAN). |
|
|
""" |
|
|
def __init__(self, latent_dim: int = 100, output_dim: int = 784): |
|
|
super().__init__() |
|
|
self.latent_dim = latent_dim |
|
|
|
|
|
self.generator = nn.Sequential( |
|
|
nn.Linear(latent_dim, 256), |
|
|
nn.ReLU(), |
|
|
nn.Linear(256, 512), |
|
|
nn.ReLU(), |
|
|
nn.Linear(512, output_dim), |
|
|
nn.Tanh() |
|
|
) |
|
|
|
|
|
def forward(self, z: torch.Tensor) -> torch.Tensor: |
|
|
"""Generate from latent code""" |
|
|
return self.generator(z) |
|
|
|
|
|
def generate(self, num_samples: int = 1) -> torch.Tensor: |
|
|
"""Generate samples""" |
|
|
z = torch.randn(num_samples, self.latent_dim) |
|
|
return self.forward(z) |
|
|
|
|
|
|
|
|
class DivergentThinking: |
|
|
""" |
|
|
Implements mechanisms for exploring solution spaces. |
|
|
""" |
|
|
def __init__(self, temperature: float = 1.0): |
|
|
self.temperature = temperature |
|
|
|
|
|
def explore_solutions(self, initial_solution: np.ndarray, num_variants: int = 10) -> List[np.ndarray]: |
|
|
""" |
|
|
Generate diverse variants of a solution. |
|
|
""" |
|
|
solutions = [initial_solution] |
|
|
|
|
|
for _ in range(num_variants - 1): |
|
|
|
|
|
noise = np.random.randn(*initial_solution.shape) * self.temperature |
|
|
variant = initial_solution + noise |
|
|
|
|
|
|
|
|
variant = np.clip(variant, -1, 1) |
|
|
|
|
|
solutions.append(variant) |
|
|
|
|
|
return solutions |
|
|
|
|
|
def mutate(self, solution: np.ndarray, mutation_rate: float = 0.1) -> np.ndarray: |
|
|
"""Mutate a solution""" |
|
|
mask = np.random.random(solution.shape) < mutation_rate |
|
|
noise = np.random.randn(*solution.shape) * 0.1 |
|
|
mutated = solution.copy() |
|
|
mutated[mask] += noise[mask] |
|
|
return np.clip(mutated, -1, 1) |
|
|
|
|
|
|
|
|
class AnalogicalReasoning: |
|
|
""" |
|
|
Enhanced analogical reasoning across domains. |
|
|
""" |
|
|
def __init__(self): |
|
|
self.analogies = {} |
|
|
|
|
|
def find_analogy(self, source: Dict, target: Dict) -> Optional[Dict]: |
|
|
""" |
|
|
Find analogy between source and target domains. |
|
|
""" |
|
|
|
|
|
source_structure = self._extract_structure(source) |
|
|
target_structure = self._extract_structure(target) |
|
|
|
|
|
|
|
|
mappings = self._find_mappings(source_structure, target_structure) |
|
|
|
|
|
return { |
|
|
"source": source, |
|
|
"target": target, |
|
|
"mappings": mappings, |
|
|
"similarity": self._compute_similarity(source_structure, target_structure) |
|
|
} |
|
|
|
|
|
def _extract_structure(self, domain: Dict) -> Dict: |
|
|
"""Extract structural representation""" |
|
|
|
|
|
return domain |
|
|
|
|
|
def _find_mappings(self, source: Dict, target: Dict) -> List[Tuple]: |
|
|
"""Find mappings between structures""" |
|
|
mappings = [] |
|
|
for key in source.keys(): |
|
|
if key in target: |
|
|
mappings.append((key, key)) |
|
|
return mappings |
|
|
|
|
|
def _compute_similarity(self, source: Dict, target: Dict) -> float: |
|
|
"""Compute structural similarity""" |
|
|
common_keys = set(source.keys()) & set(target.keys()) |
|
|
if not common_keys: |
|
|
return 0.0 |
|
|
|
|
|
similarities = [] |
|
|
for key in common_keys: |
|
|
if isinstance(source[key], (int, float)) and isinstance(target[key], (int, float)): |
|
|
sim = 1.0 / (1.0 + abs(source[key] - target[key])) |
|
|
else: |
|
|
sim = 1.0 if source[key] == target[key] else 0.0 |
|
|
similarities.append(sim) |
|
|
|
|
|
return np.mean(similarities) if similarities else 0.0 |
|
|
|
|
|
|
|
|
class ConceptCombination: |
|
|
""" |
|
|
Combines concepts in novel ways. |
|
|
""" |
|
|
def __init__(self): |
|
|
self.concepts = {} |
|
|
|
|
|
def combine(self, concept1: str, concept2: str) -> Dict: |
|
|
""" |
|
|
Combine two concepts to create a new one. |
|
|
""" |
|
|
vec1 = self._get_concept_vector(concept1) |
|
|
vec2 = self._get_concept_vector(concept2) |
|
|
|
|
|
|
|
|
combined_add = vec1 + vec2 |
|
|
combined_mult = vec1 * vec2 |
|
|
combined_avg = (vec1 + vec2) / 2 |
|
|
|
|
|
return { |
|
|
"concept1": concept1, |
|
|
"concept2": concept2, |
|
|
"combined_add": combined_add, |
|
|
"combined_mult": combined_mult, |
|
|
"combined_avg": combined_avg, |
|
|
"novelty": self._compute_novelty(combined_add) |
|
|
} |
|
|
|
|
|
def _get_concept_vector(self, concept: str) -> np.ndarray: |
|
|
"""Get vector representation of concept""" |
|
|
if concept not in self.concepts: |
|
|
|
|
|
self.concepts[concept] = np.random.randn(100) |
|
|
return self.concepts[concept] |
|
|
|
|
|
def _compute_novelty(self, combined: np.ndarray) -> float: |
|
|
"""Compute novelty of combined concept""" |
|
|
|
|
|
if not self.concepts: |
|
|
return 1.0 |
|
|
|
|
|
existing = np.array(list(self.concepts.values())) |
|
|
distances = np.linalg.norm(existing - combined, axis=1) |
|
|
min_distance = np.min(distances) |
|
|
|
|
|
|
|
|
novelty = min(1.0, min_distance / 10.0) |
|
|
return novelty |
|
|
|
|
|
|
|
|
class CreativeProblemSolver: |
|
|
""" |
|
|
Complete creative problem solving system. |
|
|
""" |
|
|
def __init__(self): |
|
|
self.generator = GenerativeModel() |
|
|
self.divergent = DivergentThinking() |
|
|
self.analogy = AnalogicalReasoning() |
|
|
self.concept_combo = ConceptCombination() |
|
|
|
|
|
def solve_creatively(self, problem: Dict) -> List[Dict]: |
|
|
""" |
|
|
Solve problem using creative methods. |
|
|
""" |
|
|
solutions = [] |
|
|
|
|
|
|
|
|
initial = self.generator.generate(1).detach().numpy().flatten() |
|
|
variants = self.divergent.explore_solutions(initial, num_variants=10) |
|
|
|
|
|
|
|
|
analogies = self._find_relevant_analogies(problem) |
|
|
|
|
|
|
|
|
if "concepts" in problem: |
|
|
concepts = problem["concepts"] |
|
|
if len(concepts) >= 2: |
|
|
combined = self.concept_combo.combine(concepts[0], concepts[1]) |
|
|
solutions.append({"type": "concept_combination", "solution": combined}) |
|
|
|
|
|
|
|
|
for variant in variants: |
|
|
mutated = self.divergent.mutate(variant) |
|
|
solutions.append({"type": "mutated", "solution": mutated}) |
|
|
|
|
|
return solutions |
|
|
|
|
|
def _find_relevant_analogies(self, problem: Dict) -> List[Dict]: |
|
|
"""Find relevant analogies for problem""" |
|
|
|
|
|
|
|
|
return [] |
|
|
|
|
|
|