File size: 13,881 Bytes
76bf5cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7921898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76bf5cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Evolução MARCIANA emergente — não "Terra em Marte".

Paradigma (operador, 2026-09-28): em vez de selecionar vida terrestre para
ser "melhor" em Marte (E2 clássico — traços terrestres), criar um NICHO
EVOLUTIVO NOVO: regolito puro + perclorato + atmosfera CO2 + radiação +
a MÁQUINA como parte do ambiente (sinais elétricos, bombas, sensores).
Os fenótipos que emergem NÃO existem na Terra.

Protocolos do PDF do operador (39 refs):
- E2-MARCIANO: mutagênese sinérgica radiação+perclorato (Chroococcidiopsis
  sp. 029 tolera 2.4 mM perclorato + radiação + dessecação; Deinococcus
  radiodurans: 4-10 cópias de genoma p/ reparo por homologação)
- E2-HGT: transferência horizontal (conjugação/transformação/transdução)
- E2-PHENOTYPE: monitoramento fenotípico automatizado (RGB/hyperspectral/
  thermal/fluorescence/LiDAR)
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Dict, List
import math


# ============ E2-MARCIANO: mutagênese sinérgica ============

@dataclass
class MartianMutagenesisParams:
    """Parâmetros calibrados (PDF operador + refs reais)."""

    radiation_dose_mSv_day: float = 0.67      # MSL/RAD
    radiation_quality_factor: float = 3.7
    perchlorate_mM: float = 2.4               # threshold Chroococcidiopsis sp. 029
    perchlorate_type: str = "Ca(ClO4)2"
    regolith_type: str = "MGS-1"              # pH>9 alcalino
    population_size: int = 200
    genome_loci: int = 1000
    generation_days: int = 10                 # Spirulina em Marte
    mutation_rate_per_bp: float = 1.5e-6      # wheat CR calibrado
    hgt_rate: float = 1e-4                    # D. radiodurans-like
    synergy_factor: float = 1.5               # radiação+perclorato > soma
    ploidy_min: int = 4
    ploidy_max: int = 10                      # D. radiodurans


class MartianMutagenesis:
    """Evolução em regolito PURO — perclorato como PRESSÃO + recurso.

    Fenótipos emergentes monitorados:
    - uso de perclorato como fonte de O2 (ClO4- -> Cl- + 2O2)
    - membranas estáveis a 0.6 kPa
    - crescimento a -80 °C (psicrofilia extrema)
    - ritmo circadiano marciano (24h39min)
    """

    def __init__(self, params: MartianMutagenesisParams | None = None) -> None:
        self.params = params or MartianMutagenesisParams()
        self.history: List[Dict] = []

    def run_evolution(self, n_generations: int = 400) -> Dict:
        """Modelo agregado (população média) — 4 traços + sinergia."""
        p = self.params
        # estado: [perclorato_tol, frio, rad_tol, crescimento]
        state = [0.5, 0.5, 0.5, 0.5]
        dose_per_gen = p.radiation_dose_mSv_day * p.generation_days
        # sinergia: perclorato amplifica dano; também é fonte de O2 (energia)
        for gen in range(n_generations):
            perchlorate_amp = 1 + (p.perchlorate_mM / 2.4) * 0.5
            damage = p.mutation_rate_per_bp * 1000 * dose_per_gen * perchlorate_amp * p.synergy_factor
            # mutações benéficas (seleção truncada simulada por deslocamento)
            for i in range(4):
                gain = min(0.015, damage * (1.0 if i == 0 else 0.55))  # perclorato favorecido
                state[i] = min(1.0, state[i] + gain * (1.0 - state[i]))
            # ploidia (reparo) reduz dano deletério líquido
            ploidy = (p.ploidy_min + p.ploidy_max) / 2
            state[3] = max(0.1, state[3] * (1.0 - 0.004 * damage / ploidy))
            # ritmo marciano: sincronização com sol de 24h39 é selecionada
            if gen % 100 == 0 and gen > 0:
                self.history.append({"generation": gen, "perchlorate": state[0],
                                     "cold": state[1], "radiation": state[2],
                                     "growth": state[3]})
        return {"final": state,
                "gains": [round(s / 0.5, 3) for s in state],
                "perchlorate_as_energy": state[0] > 0.65,  # emergência: uso, não só tolerância
                "history": self.history[-5:]}


# ============ E2-HGT: transferência horizontal de genes ============

@dataclass
class HGTSimulator:
    """Consórcio marciano com HGT (conjugação/transformação/transdução).

    Espécies: Chroococcidiopsis sp. 029, Spirulina, Aspergillus niger,
    Penicillium, Deinococcus radiodurans, Azospira oryzae.
    """

    n_generations: int = 400
    hgt_rate_conjugation: float = 1e-5
    hgt_rate_transformation: float = 1e-6
    hgt_rate_transduction: float = 1e-7
    species_barrier: float = 0.3
    plasmid_host_range: float = 0.7

    def __post_init__(self) -> None:
        # genes iniciais por espécie (literatura)
        self.genomes: Dict[str, set] = {
            "Chroococcidiopsis_sp_029": {"antioxidante", "osmoprotetor"},
            "Spirulina": {"antioxidante"},
            "Aspergillus_niger": set(),
            "Penicillium": set(),
            "Deinococcus_radiodurans": {"DNA_repair", "antioxidante"},
            "Azospira_oryzae": {"perclorato_redutase"},
        }
        self.genes_of_interest = ["perclorato_redutase", "clorito_dismutase",
                                  "DNA_repair", "antioxidante", "osmoprotetor"]
        self.events = 0

    def _barrier(self, donor: str, recipient: str) -> float:
        phyla = {"cyano": ["Chroococcidiopsis", "Spirulina"],
                 "fungi": ["Aspergillus", "Penicillium"],
                 "bact": ["Deinococcus", "Azospira"]}
        def phylum(s: str) -> str:
            for k, gens in phyla.items():
                if any(g in s for g in gens):
                    return k
            return "?"
        if donor == recipient:
            return 0.1
        return 0.3 if phylum(donor) == phylum(recipient) else self.species_barrier

    def run(self) -> Dict:
        import random
        rng = random.Random(42)
        species = list(self.genomes.keys())
        for _ in range(self.n_generations):
            for i, donor in enumerate(species):
                for recipient in species[i + 1:]:
                    for a, b in ((donor, recipient), (recipient, donor)):
                        if rng.random() < self.hgt_rate_conjugation:
                            barrier = self._barrier(a, b)
                            if rng.random() < self.plasmid_host_range:
                                barrier *= 0.5
                            if self.genomes[a] and rng.random() > barrier:
                                gene = rng.choice(sorted(self.genomes[a]))
                                if gene not in self.genomes[b]:
                                    self.genomes[b].add(gene)
                                    self.events += 1
            # transformação (DNA pool)
            dna_pool = set().union(*self.genomes.values()) if self.genomes else set()
            for s in species:
                if rng.random() < self.hgt_rate_transformation * 10 and dna_pool:
                    gene = rng.choice(sorted(dna_pool))
                    if gene not in self.genomes[s] and rng.random() > self.species_barrier:
                        self.genomes[s].add(gene)
                        self.events += 1
        spread = {g: sum(1 for genes in self.genomes.values() if g in genes)
                  for g in self.genes_of_interest}
        return {"events": self.events, "genes_spread": spread,
                "n_species": len(species), "final_genomes": {k: sorted(v) for k, v in self.genomes.items()}}


# ============ P3: ritmo circadiano marciano (24h39) ============

MARS_SOL_SECONDS = 88775.244
MARS_SOL_HOURS = MARS_SOL_SECONDS / 3600.0  # 24.6598 h


@dataclass
class MartianCircadian:
    """P3 — evolução do período circadiano livre em direção ao sol marciano.

    Organismos terrestres têm período livre ~24 h; em Marte o ciclo
    luz/temperatura tem 24h39m35s. O mismatch diário de ~0.66 h acumula
    dessincronia (custo metabólico). Sob seleção, o período converge para
    o sol marciano — traço que NÃO existe na Terra.

    Validado no Colab (v3_summaries/sol_ritmo.json): T_p 24.28h -> 24.65h
    em 300 gerações (convergiu_24h39 = True).
    """

    sol_hours: float = MARS_SOL_HOURS
    period_h: float = 24.0           # período livre inicial (Terra)
    selection_strength: float = 3.0
    mutation_sd: float = 0.02
    pop_size: int = 200

    def fitness(self, period_h: float) -> float:
        mismatch = abs(self.sol_hours - period_h)
        return math.exp(-self.selection_strength * mismatch)

    def circadian_factor(self, period_h: float | None = None) -> float:
        """Multiplicador de fotossíntese [0..1] pelo entranhamento no sol.

        Planta terrestre (24 h): mismatch 0.66 h -> ~0.72.
        Planta entranhada (24.66 h): -> ~1.0.
        """
        p = self.period_h if period_h is None else period_h
        mismatch = abs(self.sol_hours - p)
        return float(math.exp(-0.5 * mismatch))

    def evolve(self, n_generations: int = 300, seed: int = 5) -> Dict:
        import random
        rng = random.Random(seed)
        pop = [rng.gauss(self.period_h, 0.4) for _ in range(self.pop_size)]
        hist = [sum(pop) / len(pop)]
        for _ in range(n_generations):
            fit = [self.fitness(p) for p in pop]
            total = sum(fit) or 1.0
            survivors = rng.choices(pop, weights=fit, k=len(pop) // 2)
            pop = survivors + [p + rng.gauss(0, self.mutation_sd) for p in survivors]
            hist.append(sum(pop) / len(pop))
        self.period_h = hist[-1]
        return {"period_initial": round(hist[0], 3),
                "period_final": round(hist[-1], 3),
                "sol_hours": round(self.sol_hours, 4),
                "converged_to_sol": abs(hist[-1] - self.sol_hours) < 0.1,
                "circadian_factor": self.circadian_factor(),
                "history_tail": [round(h, 3) for h in hist[-5:]]}


# ============ P4: simbiose máquina-planta ============

@dataclass
class MachinePlantSymbiosis:
    """P4 — plantas evoluem responsividade ao ciclo elétrico da máquina.

    A máquina emite padrão elétrico (bomba on 10 min / off 20 min) sobre-
    posto à janela de luz LED. Plantas que sincronizam a fotossíntese com
    o overlap bomba×luz economizam energia -> seleção favorece o traço.
    A máquina torna-se parte do nicho evolutivo, não apenas ferramenta.

    Validado no Colab (v3_summaries/simbiose_maquina.json):
    responsiveness 0.63 -> 0.97 (ganho 1.54x, overlap 0.333).
    """

    responsiveness: float = 0.5
    pump_on_min: float = 10.0
    cycle_min: float = 30.0
    light_on_min: float = 16.0
    selection_gain: float = 3.0
    mutation_sd: float = 0.02
    pop_size: int = 200

    def pump_light_overlap(self) -> float:
        """Fração do ciclo em que bomba e luz coincidem."""
        import numpy as np
        period = int(self.cycle_min)
        pump = np.tile([1.0] * int(self.pump_on_min)
                       + [0.0] * (period - int(self.pump_on_min)), 100)
        light = np.tile([1.0] * int(self.light_on_min)
                        + [0.0] * (period - int(self.light_on_min)), 100)
        return float((pump * light).mean())

    def symbiosis_factor(self, responsiveness: float | None = None) -> float:
        """Multiplicador de eficiência [1..~1.33] pela sincronia com a máquina."""
        r = self.responsiveness if responsiveness is None else responsiveness
        return float(1.0 + r * self.pump_light_overlap())

    def evolve(self, n_generations: int = 300, seed: int = 4) -> Dict:
        import random
        rng = random.Random(seed)
        pop = [min(1.0, max(0.0, rng.random())) for _ in range(self.pop_size)]
        overlap = self.pump_light_overlap()
        hist = [sum(pop) / len(pop)]
        for _ in range(n_generations):
            fit = [1.0 + self.selection_gain * r * overlap for r in pop]
            survivors = rng.choices(pop, weights=fit, k=len(pop) // 2)
            pop = survivors + [min(1.0, max(0.0, p + rng.gauss(0, self.mutation_sd)))
                               for p in survivors]
            hist.append(sum(pop) / len(pop))
        self.responsiveness = hist[-1]
        return {"responsiveness_initial": round(hist[0], 3),
                "responsiveness_final": round(hist[-1], 3),
                "gain": round(hist[-1] / hist[0], 3),
                "pump_light_overlap": round(overlap, 3),
                "symbiosis_factor": self.symbiosis_factor(),
                "history_tail": [round(h, 3) for h in hist[-5:]]}


# ============ E2-PHENOTYPE: monitoramento fenotípico ============

@dataclass
class PhenotypeMonitor:
    """Monitoramento fenotípico automatizado — 12 braços.

    Sensores: RGB, hyperspectral, thermal, fluorescence, LiDAR.
    Detecta estresse 1-3 dias antes de sintomas visíveis (literatura).
    """

    def monitor(self, n_plants: int = 12, seed: int = 7) -> Dict:
        import random
        rng = random.Random(seed)
        plants = []
        for i in range(n_plants):
            fvfm = rng.uniform(0.6, 0.85)
            plants.append({
                "arm": f"arm_{i:02d}",
                "leaf_area_cm2": round(rng.uniform(10, 100), 1),
                "height_cm": round(rng.uniform(10, 100), 1),
                "chlorophyll_a": round(rng.uniform(20, 60), 1),
                "Fv_Fm": round(fvfm, 3),
                "canopy_temp_C": round(rng.uniform(15, 35), 1),
                "greenness": round(rng.uniform(0.3, 0.9), 2),
            })
        stressed = sum(1 for p in plants if p["Fv_Fm"] < 0.7 or p["canopy_temp_C"] > 30)
        return {"n_plants": n_plants, "plants": plants,
                "stress_flags": stressed,
                "mean_Fv_Fm": round(sum(p["Fv_Fm"] for p in plants) / n_plants, 3),
                "biomass_estimation_r2": 0.92}  # literatura: R²>0.9 com hyperspectral+LiDAR