Milan
Milan

Reputation: 205

Passing vector to function

While trying to clean up my code a bit I ran into a problem that I can't seem to figure out. I'm trying to pass vector values to a function so that I can do the evaluation in a function rather than in the main code, but I can't figure out how to actually pass the vector correctly.

#include "stdafx.h"
#include <vector>
#include <random>
#include <string>
#include <fstream>
#include <algorithm>
#include <iterator>

int population_size = 10;
int number_of_variables = 3;

struct one_individual
{
std::vector<std::vector<double>>chromosomes;
double evaluation1 = 0;
double evaluation2 = 0;
double evaluation3 = 0;
double fitness = 0;
double probability = 0;
};

std::vector<one_individual> individuals;

double evaluation(std::vector<one_individual> individuals[i])
{
individuals[i].evaluation1 = 1 * individuals[i].chromosomes[0].at(0) + 2 * individuals[i].chromosomes[0].at(1) + 3 * individuals[i].chromosomes[0].at(2);
individuals[i].evaluation2 = 1.5 * individuals[i].chromosomes[0].at(0) + 3 * individuals[i].chromosomes[0].at(1) + 4.5 * individuals[i].chromosomes[0].at(2);
individuals[i].evaluation3 = 2 * individuals[i].chromosomes[0].at(0) + 4 * individuals[i].chromosomes[0].at(1) + 6 * individuals[i].chromosomes[0].at(2);

individuals[i].fitness = 1 / (individuals[i].evaluation1 + individuals[i].evaluation2 + individuals[i].evaluation3);

return individuals[i].fitness;
}

int main()
{
    std::random_device rd;
    std::mt19937 rng(rd());    // random-number engine (Mersenne-Twister in this case)
    std::uniform_real_distribution<double> dist(-10.0, 10.0);

    for (int i = 0; i < population_size; i++)
    {
        std::vector<double>variables;
        for (int j = 0; j < number_of_variables; j++)
        {
            variables.push_back(dist(rng));
        }
        individuals[i].chromosomes.push_back(variables);
    }


    for (int i = 0; i < population_size; i++)
    {
        evaluation(individuals[i]);
        std::cout << "Individual " << i << " has fitness of " << individuals[i].fitness;
    }       
}

I've tried to condense the code as much as possible so that it's not too much to read, I think everything still in there does need to actually be in there.

Anyway, the ways I've tried to pass on the values of the vector to the function all aren't working and the answers I could find sadly don't clear up my mistake for me.

Any help would be very much appreciated!

Upvotes: 0

Views: 299

Answers (2)

Cory Kramer
Cory Kramer

Reputation: 118031

If you want to iterate through the vector and process one element at a time, you just have to pass the type that is being iterated over

double evaluation(one_individual& person)
{
    // do stuff with person
}

To iterate over your vector you would do something like

for (auto& person : individuals)
{
    evaluation(person);
}

Upvotes: 1

Lundin
Lundin

Reputation: 215350

double evaluation(std::vector<one_individual> individuals[i])

should be

double evaluation(std::vector<one_individual>& individuals)

Also get rid of the global variable with identical name as your parameter. There is no reason to declare it as global in the first place.

Upvotes: 0

Related Questions