views:

289

answers:

2

Hey basically Im trying to store a "solution" and create a vector of these. The problem I'm having is with initialization. Heres my class for reference

class Solution
{
private:
      //  boost::thread m_Thread;
        int itt_found;
        int dim;
        pfn_fitness f;
        double value;
        std::vector<double> x;
public:

        Solution(size_t size, int funcNo) : itt_found(0), x(size, 0.0), value(0.0), dim(30), f(Eval_Functions[funcNo])
        {
            for (int i = 1; i < (int) size; i++) {
                x[i] = ((double)rand()/((double)RAND_MAX))*maxs[funcNo];
            }
        }

        Solution() : itt_found(0), x(31, 0.0), value(0.0), dim(30), f(Eval_Functions[1])
        {
            for (int i = 1; i < 31; i++) {
                x[i] = ((double)rand()/((double)RAND_MAX))*maxs[1];
            }
        } 
        Solution operator= (Solution S) 
        {
            x = S.GetX();
            itt_found = S.GetIttFound();
            dim = S.GetDim();
            f = S.GetFunc();
            value = S.GetValue();
            return *this;
        }
        void start()
        {
            value = f (dim, x);
        }
        /* plus additional getter/setter methods*/
}

Solution S(30, 1) or Solution(2, 5) work and initalizes everything, but I need X of these solution objects. std::vector<Solution> Parents(X) will create X solutions with the default constructor and i want to construct using the (int, int) constructor. Is there any easy(one liner?) way to do this? Or would i have to do something like:

size_t numparents = 10;
vector<Solution> Parents;
    Parents.reserve(numparents);
    for (int i = 0; i<(int)numparents; i++) {
        Solution S(31, 0);
        Parents.push_back(S);
    }
+1  A: 

I have used the Boost assignment library for tasks like this. You may find it useful....

jwismar
I will look into this as I already am using the boost threading library in this program
Flamewires
+1  A: 

the example I gave as a comment uses copy constructor to create new objects. You can do the following:

// override copy constructor
Solution(const Solution &solution) {
... copy from another solution
}

however be careful, as you no longer going to have exact object copy/construct if you introduce random generation in your copy constructor, i.e. Solution y = x; y != x

your best solution is something like you already have in my opinion

aaa
hm alright, thanks. I might just keep it as is.
Flamewires