Deque (C ++): Abort trap 6

0

I'm trying to call a function that I did, but this error always appears:

libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: deque

EXECUTAR FINISHED; Abort trap: 6; tempo real: 230ms; usuário: 0ms; sistema: 0ms

Here is the code for the function:

int Perturba(deque<int>* sack_profit, deque<int>* sack_weight, int items_profit[], int items_weight[], int num_items, int T)
{
    deque<int> solution_profitV;
    deque<int> solution_weightV;
    int item_position;
    int sack_total_weight = 0;
    int sack_total_profit = 0;
    int total_weightV = 0;
    int total_profitV = 0;
    int i = 0;
    int delta;

    while(i <= sack_weight->size())
    {
        solution_profitV.push_back(sack_profit->at(i));
        solution_weightV.push_back(sack_weight->at(i));
        i++;
    }

    srand( (unsigned) time (NULL) );
    item_position = rand() % num_items;

    solution_profitV.push_back(items_profit[item_position]);
    solution_weightV.push_back(items_weight[item_position]);

    i=0;

    while(i <= sack_weight->size())
    {
        sack_total_weight += sack_weight->at(i);
        sack_total_profit += sack_profit->at(i);
        i++;
    }

    i = 0;

    while(i <= solution_weightV.size())
    {
        total_weightV += solution_weightV[i];
        total_profitV += solution_profitV[i];
        i++;
    }

    delta = total_profitV - sack_total_profit + total_weightV - sack_total_profit;

    if(delta <= 0 || exp(-delta/T) > Random())
    {
        sack_profit->swap(solution_profitV);
        sack_weight->swap(solution_weightV);
        return 1;
    }
    else
        return 0;
}

This function basically randomly modifies the current combination of items in the past, if it satisfies the explicit condition in the code.

I think the problem is in modifying the decks passed as pointers, but I'm not sure if that's what it is or how to solve it.

How can I resolve this problem?

    
asked by anonymous 05.10.2016 / 19:41

1 answer

0

Change this here

while(i <= sack_weight->size())

So here

while (i < sack_weight->size())

Change also in other similar commands.

    
06.10.2016 / 03:35