File size: 2,195 Bytes
db3c893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#pragma once
#include <vector>
#include <cstdint>

/**

 * Fungal (mycelium) population in SoA layout for coalesced GPU access.

 * Each fungus contributes a local basis φ_h(x,y) to build amplitude/phase masks.

 */
struct FungiSoA {
  int F = 0;                  // number of fungi
  int H = 0, W = 0;           // mask geometry
  // Genome / state
  std::vector<float> x, y;    // position in [0, W-1] & [0, H-1]
  std::vector<float> sigma;   // radius
  std::vector<float> alpha;   // anisotropy (ellipse)
  std::vector<float> theta;   // orientation
  std::vector<float> a_base;  // amplitude base coeff
  std::vector<float> p_base;  // phase base coeff
  // Energy & life-cycle
  std::vector<float> energy;
  std::vector<float> mass;
  std::vector<int>   age;

  void resize(int F_, int H_, int W_);
  void init_random(unsigned seed, float sigma_min=1.5f, float sigma_max=5.5f);
  void adjust_population(int newF, unsigned seed);
};

/** Build masks A(x,y), P(x,y) from fungi population (GPU). */
void fungi_build_masks_GPU(const FungiSoA& pop,

                           float* d_A, float* d_P, // [H*W]

                           int tiles_y=7, int tiles_x=7);

/** Evolution step (GPU): compute per-fungus reward and update energy, size, reproduction. */
void fungi_evolve_GPU(FungiSoA& pop,

                      const float* d_grad_map, // e.g., gradient magnitude per pixel [H*W]

                      int evo_pairs,

                      float food=0.05f, float decay=0.98f, float death_th=-0.5f,

                      float cost=1e-3f, unsigned seed=1337);

/** Host helpers: download masks (optional debug). */
void download_mask(float* h, const float* d, int HW);

/** DIAGNOSTIC TOOLS: Visual debugging for fungi evolution */
void fungi_export_debug_images(const FungiSoA& pop,

                               const float* d_A, const float* d_P,

                               const float* d_grad_map,

                               const char* prefix = "debug");

void fungi_create_test_pattern(float* h_pattern, int H, int W, int pattern_type = 0);
void fungi_analyze_mask_statistics(const float* d_A, const float* d_P, int HW);