0

I have the following struct defs

pub struct Connection {
    in_node_id: usize,
    out_node_id: usize,
    weight: f64,
    innovation_num: usize
}

pub struct Network<'a> {
    n_sensor_nodes: usize,
    n_output_nodes: usize,
    n_hidden_nodes: usize,
    genome: Vec<Connection>,
    input_connections: Vec<Vec<&'a Connection>>,
    nodes_with_active_inputs: Vec<bool>,
    active_nodes: Vec<bool>,
    active_sums: Vec<f64>,
    node_values: Vec<f64>
}

and the following function

fn set_phenotype(mut network: Network) -> Network {
    let n_activateable = network.n_output_nodes + network.n_hidden_nodes;
    network.input_connections = (0..n_activateable).map(|_| Vec::new()).collect();
    for conn in network.genome.iter() {
        network.input_connections[conn.out_node_id].push(conn)
    }
    network
}

The compiler complains that (in the function) network.genome does not live long enough. I have tried various combinations of making things references or not. I would like to keep the signature of the function intact and avoid any deep copying if posible. Not sure if these are 2 incompatable objectives.

As an aside, ChatGpt's solution is to convert input_connections into Vec<Vec<usize>> and use as index into genome. This would work, but in the interest of learning, wondering what else I can do.

Chechy Levas
  • 2,206
  • 1
  • 13
  • 28

0 Answers0