Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement quantum-inspired narrative field simulation framework #22

Merged
merged 15 commits into from
Oct 27, 2024

Conversation

leonvanbokhorst
Copy link
Owner

@leonvanbokhorst leonvanbokhorst commented Oct 27, 2024

Summary by Sourcery

Implement a comprehensive narrative field simulation framework using quantum-inspired concepts, including story representation, pattern tracking, environmental effects, and interaction analysis.

New Features:

  • Introduce a NarrativeFieldSimulator class to simulate the evolution of a narrative field using quantum-inspired properties.
  • Add NarrativeWave class to represent stories as quantum wave functions with properties like amplitude, phase, and coherence.
  • Implement PatternMemory, PatternEvolution, and PhaseSpaceTracker classes to manage and track patterns and their evolution in the narrative field.
  • Include EnvironmentalCoupling and FrequencyAnalyzer classes to simulate environmental effects and perform frequency analysis on the narrative field.
  • Develop StoryInteractionAnalyzer and NarrativeFieldMetrics classes to analyze story interactions and track narrative field metrics.

In NarrativeWave, we now use view(1) to ensure that amplitude, phase, and coherence are 1D tensors with a single element.
In create_wave_function, we initialize amplitude, phase, and coherence as 1D tensors.
In apply_field_effects, we use torch.exp and torch.tensor to ensure all operations are performed on tensors.
1. In NarrativeWave.__post_init__, we now use .clone().detach() instead of torch.tensor() to create new tensors from existing ones.
In apply_environmental_effects, we convert the environment_coupling to a tensor to ensure compatibility with PyTorch operations.
In create_wave_function, we explicitly create tensors for amplitude, phase, and coherence to ensure they're of the correct type from the start.
Added type hints to improve code readability and maintainability, following PEP 484.
Enhanced the update_patterns method:
Improved temporal dynamics by introducing a decay rate for pattern strengths.
Added a pattern history to track the temporal evolution of patterns.
Implemented a more sophisticated pattern merging algorithm that considers the age of patterns.
Improved the encode_pattern method:
Added semantic structure to the pattern encoding by combining story embeddings.
Introduced modulation based on pattern metadata (coherence and field strength).
Ensured the returned embedding is normalized.
Added comments to explain the purpose and functionality of each section.
These changes align with the project's focus on complex systems theory, cognitive science, and machine learning. The enhanced PatternMemory class now better captures the temporal dynamics of patterns in the narrative field and provides a more sophisticated encoding mechanism.
We introduce a processed_stories set to keep track of stories that have already been included in a pattern.
In the first loop, we skip stories that have already been processed to avoid redundant patterns.
After adding a pattern, we update the processed_stories set with all the stories in that pattern.
In the pattern memory section, we only consider stories that haven't been processed yet. We create a new pattern only if there are new stories to add.
5. We update the processed_stories set after adding a pattern from the pattern memory as well.
These changes should significantly reduce redundancy in the detected patterns by ensuring that each story is only included in one pattern during a single detection cycle. This approach favors larger, more comprehensive patterns over smaller, overlapping ones.
…tory embeddings and can create meaningful pattern encodings. The encode_pattern method now uses actual story data to generate pattern representations, which should improve the overall simulation quality and pattern detection capabilities.
Enhanced Pattern Detection: The detect_emergence method now uses a geometric approach with centers and radii to better identify unique patterns. It calculates the center and radius for each potential pattern and includes all stories within a slightly expanded radius.
Sophisticated Quantum Interference: The quantum_interference method now includes more complex interactions between stories, considering coherence coupling, uncertainty principles, quantum tunneling, and entanglement effects.
Pattern Evolution Tracking: A new PatternEvolution class has been added to track pattern lifecycles. It maintains trajectories of patterns over time, matching current patterns with existing trajectories or creating new ones as needed.
4. Integration with Simulation: The simulate_timestep method now updates the pattern evolution after detecting emergent patterns in each timestep.
These enhancements should provide a more nuanced and realistic simulation of narrative field dynamics, with improved pattern detection and tracking over time. The quantum interference calculations now capture a wider range of quantum effects, potentially leading to more interesting and complex story interactions.
Enhanced Phase Space Analysis: The PhaseSpaceTracker class is added to record and analyze the system's evolution in phase space. It tracks the field state and pattern properties over time and provides methods to analyze attractor properties.
Pattern Interaction Dynamics: The calculate_pattern_interaction method is added to compute interactions between patterns based on their spatial overlap, phase relationship, and field interaction. This information is used in the simulate_timestep method to modify pattern evolution.
Environmental Coupling: The EnvironmentalCoupling class is introduced to generate temporally correlated (colored) noise, providing a more realistic simulation of environmental effects. This is integrated into the apply_environmental_effects method.
These enhancements allow for a more sophisticated analysis of the system's behavior, including oscillatory patterns and attractor properties. The pattern interactions are now more dynamic, and the environmental effects are more realistic with temporally correlated noise.
To fully integrate these changes, you'll need to update the simulate_timestep method to use the new pattern interaction calculations and modify the pattern evolution accordingly. You may also want to add logging or visualization of the phase space trajectory and attractor properties for a better understanding of the system's behavior.
.
These changes implement the suggested refinements:
1. In apply_field_effects, we've added amplitude-dependent damping to better control field strength growth. The decoherence rate now depends on the wave's amplitude, and we've introduced a damping factor for amplitude evolution.
The calculate_pattern_interaction method now includes stability controls. We calculate a stability factor based on the field strengths of the interacting patterns and use it to modify the interaction strength.
The enforce_energy_conservation method has been enhanced with soft clamping using the tanh function and a small dissipation term. This should provide more nuanced energy conservation while maintaining interesting dynamics.
These refinements should help control the field strength growth while maintaining interesting dynamics in the simulation. The amplitude-dependent damping will prevent excessive growth of individual wave amplitudes, the stability factors in pattern interactions will help manage the overall system behavior, and the enhanced energy conservation will provide a more balanced approach to maintaining the system's energy within desired bounds.
…InteractionAnalyzer, and NarrativeFieldMetrics. They are integrated into the NarrativeFieldSimulator class to provide enhanced analysis capabilities:

1. FrequencyAnalyzer: Implements wavelet-based frequency analysis to detect dominant frequencies at multiple scales.
StoryInteractionAnalyzer: Provides detailed analysis of story interactions, including eigenvalue decomposition and cluster detection.
NarrativeFieldMetrics: Calculates comprehensive metrics including field entropy, story diversity, and pattern stability.
To use these new features, you'll need to install the PyWavelets library
1. The torch.linalg.eigh error should be handled gracefully now.
The pattern_stability metric should provide more meaningful information.
We'll have more detailed logging about why stories are being added or removed.
Additionally, to get more insights into the simulation's behavior, you might want to add some periodic logging of the overall system state, perhaps every 10 or 20 timesteps. This could include things like the average story coherence, the total field energy, and other relevant metrics.
…nents we've discussed, including pattern detection, frequency analysis, interaction analysis, and comprehensive field metrics. The simulation loop demonstrates how to run the simulator for multiple timesteps, add new stories periodically, and check for emergent patterns.
Copy link
Contributor

sourcery-ai bot commented Oct 27, 2024

Reviewer's Guide by Sourcery

This pull request introduces a sophisticated narrative field simulation system that models stories as quantum wave functions in a semantic space. The implementation uses PyTorch for tensor operations and includes multiple specialized classes for handling different aspects of the simulation, such as pattern detection, frequency analysis, and field evolution.

Class diagram for Narrative Field Simulation System

classDiagram
    class NarrativeWave {
        +String content
        +torch.Tensor embedding
        +torch.Tensor amplitude
        +torch.Tensor phase
        +torch.Tensor coherence
        +Dict~str, float~ entanglement
        +torch.Tensor uncertainty
        +__post_init__()
    }

    class PatternMemory {
        +List~torch.Tensor~ patterns
        +torch.Tensor pattern_strengths
        +List~Dict~ pattern_history
        +float decay_rate
        +Dict~str, NarrativeWave~ story_dict
        +update_patterns(new_pattern: Dict~str, Any~, field_state: torch.Tensor)
        +encode_pattern(pattern: Dict~str, Any~) : torch.Tensor
    }

    class PatternEvolution {
        +Dict~int, List~Dict~~ pattern_trajectories
        +int next_pattern_id
        +update(current_patterns: List~Dict~, field_state: torch.Tensor)
        +calculate_pattern_similarity(pattern1: Dict, pattern2: Dict) : float
    }

    class PhaseSpaceTracker {
        +List trajectory
        +int phase_space_dim
        +record_state(field_state: torch.Tensor, patterns: List~Dict~)
        +analyze_attractor() : Dict
    }

    class EnvironmentalCoupling {
        +torch.Tensor temperature
        +List noise_history
        +torch.Tensor correlation_time
        +generate_colored_noise(shape: tuple) : torch.Tensor
    }

    class FrequencyAnalyzer {
        +int window_size
        +List frequency_history
        +analyze_frequencies(field_state: torch.Tensor, patterns: List~Dict~) : Dict
    }

    class StoryInteractionAnalyzer {
        +List interaction_history
        +analyze_interactions(stories: Dict~str, NarrativeWave~) : Dict
    }

    class NarrativeFieldMetrics {
        +List metrics_history
        +update(field_state: torch.Tensor, stories: Dict~str, NarrativeWave~, patterns: List~Dict~) : Dict
    }

    class NarrativeFieldSimulator {
        +AutoTokenizer tokenizer
        +AutoModel encoder
        +int quantum_dim
        +Dict~str, NarrativeWave~ stories
        +torch.Tensor field_state
        +float total_energy
        +float energy_threshold
        +PatternMemory pattern_memory
        +PatternEvolution pattern_evolution
        +PhaseSpaceTracker phase_space_tracker
        +EnvironmentalCoupling environmental_coupling
        +int current_timestep
        +FrequencyAnalyzer frequency_analyzer
        +StoryInteractionAnalyzer interaction_analyzer
        +NarrativeFieldMetrics field_metrics
        +create_wave_function(content: str, story_id: str) : NarrativeWave
        +quantum_interference(wave1: NarrativeWave, wave2: NarrativeWave) : float
        +apply_field_effects(wave: NarrativeWave, dt: float)
        +apply_environmental_effects(wave: NarrativeWave, dt: float)
        +enforce_energy_conservation()
        +update_field_state()
        +detect_emergence() : List~Dict~
        +calculate_pattern_interaction(pattern1: Dict, pattern2: Dict) : Dict
        +simulate_timestep(dt: float)
    }

    NarrativeFieldSimulator --> NarrativeWave
    NarrativeFieldSimulator --> PatternMemory
    NarrativeFieldSimulator --> PatternEvolution
    NarrativeFieldSimulator --> PhaseSpaceTracker
    NarrativeFieldSimulator --> EnvironmentalCoupling
    NarrativeFieldSimulator --> FrequencyAnalyzer
    NarrativeFieldSimulator --> StoryInteractionAnalyzer
    NarrativeFieldSimulator --> NarrativeFieldMetrics

    PatternMemory --> NarrativeWave
    StoryInteractionAnalyzer --> NarrativeWave
    NarrativeFieldMetrics --> NarrativeWave
    PatternEvolution --> NarrativeWave
    PhaseSpaceTracker --> NarrativeWave
Loading

File-Level Changes

Change Details Files
Implemented core narrative field simulation classes and data structures
  • Created NarrativeWave dataclass to represent stories as quantum wave functions
  • Implemented PatternMemory class for tracking emergent patterns
  • Added PatternEvolution class for monitoring pattern trajectories
  • Created PhaseSpaceTracker for analyzing system dynamics
src/nfs_story_waves.py
Added quantum-inspired field dynamics and interaction mechanisms
  • Implemented quantum interference calculations between stories
  • Added field effects and environmental coupling mechanisms
  • Created energy conservation enforcement system
  • Implemented non-linear field state updates
src/nfs_story_waves.py
Developed analysis and monitoring components
  • Added FrequencyAnalyzer for wavelet-based pattern analysis
  • Implemented StoryInteractionAnalyzer for relationship tracking
  • Created NarrativeFieldMetrics for comprehensive field monitoring
  • Added extensive logging and debugging capabilities
src/nfs_story_waves.py
Implemented main simulation control and integration
  • Created NarrativeFieldSimulator class to orchestrate the simulation
  • Added timestep-based simulation control
  • Implemented dynamic story management
  • Added example usage demonstration
src/nfs_story_waves.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@leonvanbokhorst leonvanbokhorst self-assigned this Oct 27, 2024
@leonvanbokhorst leonvanbokhorst added documentation Improvements or additions to documentation enhancement New feature or request labels Oct 27, 2024
@leonvanbokhorst leonvanbokhorst added this to the Phase 1 milestone Oct 27, 2024
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @leonvanbokhorst - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding comprehensive error handling around tensor operations, particularly in quantum_interference() and update_field_state() methods to prevent silent failures.
  • Add explicit GPU/device management for torch operations to allow for hardware acceleration and consistent device placement.
Here's what I looked at during the review
  • 🟡 General issues: 4 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@leonvanbokhorst leonvanbokhorst changed the title Nfs_story_waves @Sourcery-ai Oct 27, 2024
@sourcery-ai sourcery-ai bot changed the title @Sourcery-ai Implement quantum-inspired narrative field simulation framework Oct 27, 2024
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@leonvanbokhorst leonvanbokhorst merged commit 766c7cf into main Oct 27, 2024
1 check passed
@leonvanbokhorst leonvanbokhorst deleted the nfs_story_waves branch October 27, 2024 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant