File size: 2,709 Bytes
12ba16b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# src/simulation_environment.py
# A conceptual framework for the "Digital Crucible" simulation environment.
# Project Chimera would be trained within a far more advanced version of this.

import time
from src.utils.logger import log

class PhysicsEngine:
    """
    A simplified physics engine for the simulation.
    """
    def __init__(self):
        self.gravity = -9.81  # m/s^2

    def apply_force(self, obj, force):
        """Applies a force to an object."""
        # F = ma -> a = F/m
        acceleration = force / obj.mass
        obj.velocity += acceleration
        log("PhysicsEngine", f"Applied force to '{obj.name}'. New velocity: {obj.velocity:.2f} m/s")

    def update_position(self, obj):
        """Updates an object's position based on its velocity."""
        obj.position += obj.velocity
        log("PhysicsEngine", f"Updated position for '{obj.name}'. Current position: {obj.position:.2f} m")

class SimulatedObject:
    """Represents a generic object within the simulation."""
    def __init__(self, name, mass, initial_position=0, initial_velocity=0):
        self.name = name
        self.mass = mass
        self.position = initial_position
        self.velocity = initial_velocity
        log("SimulatedObject", f"Created '{name}' with mass {mass}kg.")

class Environment:
    """
    The main simulation environment class. Chimera would interact with this
    world to learn cause and effect.
    """
    def __init__(self):
        log("DigitalCrucible", "--- Environment Initializing ---", header=True)
        self.physics = PhysicsEngine()
        self.objects = []
        self.time_step = 0
        log("DigitalCrucible", "--- Environment Ready ---")

    def add_object(self, obj):
        """Adds an object to the simulation."""
        self.objects.append(obj)

    def run_step(self, chimera_action=None):
        """Runs a single step of the simulation."""
        self.time_step += 1
        log("DigitalCrucible", f"--- Simulation Step {self.time_step} ---", header=True)
        
        if chimera_action:
            target_obj_name = chimera_action.get("target")
            force_to_apply = chimera_action.get("force")
            target = next((obj for obj in self.objects if obj.name == target_obj_name), None)

            if target and force_to_apply:
                log("ChimeraAction", f"AI attempts to apply {force_to_apply}N of force to '{target.name}'.")
                self.physics.apply_force(target, force_to_apply)

        # Update all objects
        for obj in self.objects:
            gravity_force = self.physics.gravity * obj.mass
            self.physics.apply_force(obj, gravity_force)
            self.physics.update_position(obj)