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);
    }
5
  • you can initialize to value like this: vector(size, Solution(21, 0)); for reference cplusplus.com/reference/stl/vector/vector Commented May 22, 2010 at 1:46
  • wonderful, thanks thats exactly what i wanted. Commented May 22, 2010 at 1:48
  • @aaa, should have been an answer... Commented May 22, 2010 at 2:11
  • Actually... after testing when I do it this way all the x's get the same value in each Solution(i.e Parents[i].GetX()[j] = Parents[i+1].GetX().[j], so this isnt exactly what i wanted Commented May 22, 2010 at 2:29
  • couple points : first element of x never gets initialized (perhaps by design?). Your copy assignment appears to be somewhat wasteful and possibly incorrect, you probably want to pass solution as reference and return *this as reference. That way you have now, you create new solution and then return yet another new solution. If this is by design, please ignore Commented May 22, 2010 at 3:05

2 Answers 2

1

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

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

Comments

1

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

1 Comment

I will look into this as I already am using the boost threading library in this program

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.