1

I'm writing a program based on the multi-chain Metropolis algorithm to find minimum energy parameters for a given function. This program basically runs two times:

  1. First multi-chain burn-in run which sweeps the parameter space and stops when a given convergence criteria is met;
  2. Exploratory run which samples the various parameter possibilities around the convergence points calculated in the first step but only with one chain.

For each chain I save all the parameters it passed through and its corresponding energy. Each step of each chain runs (among other things) the following code:

int Step(double * current_energy, vector<double> & energy){

    energy.push_back(current_energy);

    return 0;
}

The problem is that for the first part of the program, I'm storing the energies in a

vector< vector<double> > burn_energy;

to allow me to save multiple series of energies in just one place. In the second part, I store the energies in

vector<double> explore_energy;

because I only have to save one series of energy.

The problem arises when I have to call these steps. For the first part I would call them this way:

Step(& current_energy[j], burn_energy[j]);

in which j indicates the number of the chain. In the second part, as there is only one chain, the call would be something like:

Step(& current_energy, explore_energy);

I can't see what is wrong with my implementation but XCode gives me the following error in the first part:

error: no matching function for call to 'std::vector<double, std::allocator<double> >::push_back(double*&)'

Is this an error with the passing of the vector energy or the element of the array current_energy?

2 Answers 2

2
int Step(double * current_energy, vector<double> & energy)

should be

int Step(double current_energy, vector<double> & energy)

Edit:

After the OP's comment I suggest this;

int Step(double& current_energy, vector<double> & energy)
             ^^^^   

in this case you can change current energy without any pointer syntax. It's called a reference.

Sign up to request clarification or add additional context in comments.

1 Comment

The problem is that the function also alters the value in current_energy and I need it for other things outside the Step :\
1

energy is a vector of doubles, but you are trying to push a double* onto it. Don't do that.

2 Comments

So the problem is in the passing of current_energy, no? I still didn't quite get the whole reference and pointer thing :\
Yes, current_energy is a pointer. You say you have to alter the value, that's fine — to pass the value into push_back, you should use energy.push_back(*current_energy).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.