0

I have a initialization function, lets say "Solution" with a set of Inputs. In this Solution, I initiate 3 functions, lets say "Initial Train", "Refresh Friend", and "Initial Vehicles". The flow is like this: Start Solution -> (Some input 1) -> Initial Train -> (Some input 2) -> Refresh Friend -> (Some input 3) -> Initial Vehicles -> End Solution.

The (Some input x) here can be some code to check whether or not the function(s) are successfully initiated and complete or not. The problem is after "Refresh Friend" is successfully called and executed, the code immediate stops without continues (Some input 3), lets say " cout << "Cannot Continue Point"; ". This problem is quite strange, it takes me days to fix it but I can't, can you helps me explaining this problem and how should I fix it?

I will attach the code here: The main() call:

#include "Input.h"
#include "InstanceRead.h" //Reading Txt Files
#include "csvfile.h"
#include "datfile.h"
#include "SideFunc.h" //Some random functions that is global used
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include "CreateRoute.h" //Aiding the functions in "Create Solution.h"
#include "CreateSolution.h" //Solution

using namespace std;
int main(){
    srand(time(NULL));
    string File = "Data_C1_6_1_02_Data.txt";
    string OFile = "Data_C1_6_1_02";
    DataSet InputSet(File, 6);
    Solution InitialSolution(InputSet.First,InputSet.Second,InputSet.Third, InputSet.Fourth, 1000);
};

The "Solution" call:

#include "Input.h"
#include "CreateRoute.h"
#include "CreateSolution.h"
#include "SideFunc.h"
#include "csvfile.h"
#include "datfile.h"
#include <utility>


Solution::Solution(vector<int> InputSet1, vector<vector<vector<int>>> InputSet2, vector<pair<int, int>> InputSet3, vector<int> RailWay, int seed){
    Seed = seed;
    srand(Seed);
    OperationTime = TotalSupply = MovingTime = UsedTruck = UsedHandler = 0;
    Vnum = InputSet1[0];
    Vcap = InputSet1[1];
    HandCap = InputSet1[2];
    Tnode = InputSet1[3];
    Hnode = InputSet1[4];
    Mnode = InputSet1[5];
    DOnode = InputSet1[6];
    Mcover = InputSet1[7];
    NumClose = InputSet1[8];
    Tstart = InputSet1[9];
    Tend = InputSet1[10];
    Mstart = InputSet1[11];
    MDend = InputSet1[14];
    SetData = InputSet2[0];
    DistanceSer = Distance = InputSet2[1];
    FriendDist = InputSet2[3];
    FriendSer = InputSet2[4];
    DOPairs = InputSet3;
    RailLine = RailWay;
    vector<int> Numdex(SetData.size());
    iota(Numdex.begin(),Numdex.end(),0);
    NodesIndex = Numdex; 
    vector<int> DOChosen(DOnode,1);
    DOSelection = DOChosen;
    copy(NodesIndex.begin() + InputSet1[11], NodesIndex.begin() + InputSet1[12] + 1, back_inserter(MRTNodes));        
    for(int i = 0; i < DOnode; i++){
        if(DOSelection[i] == 1){
            VehicleNodes.push_back(DOPairs[i].second);
        }else{
            MRTNodes.push_back(DOPairs[i].first);
        }}
    for(int i = 0; i < Tend; i++){
        TerminalRoute.push_back(make_pair(i, -1));}    
    InitialTrain();
    for(int i = 0; i < TrainSet.size(); i++){
        VehicleNodes.push_back(TrainSet[i].TerminalID);}
    copy(NodesIndex.begin() + InputSet1[15], NodesIndex.begin() + InputSet1[16] + 1, back_inserter(VehicleNodes));    
    system("pause");
    RefreshFriend();
    cout << "Cannot Continue Point ";
    system("pause");
    InitialVehicle();
    system("pause");
};

The "Refresh Friend" call:

void Solution::RefreshFriend(){  
    for(int i = 0; i < DistanceSer.size(); i++){
        for(int j = (DistanceSer.size() - 1); j >= i; j--){
            DistanceSer[i][j] += SetData[j][6];
            DistanceSer[j][i] += SetData[i][6];
        }
    }
    vector<vector<int>> FrClose, FrStays;
    Circle = VehicleNodes.size();
    FrClose.resize(Distance.size() - 1,vector<int> (Circle - 1, 0));
    FrStays = FrClose;
    for(int i = 0; i <= VehicleNodes.size(); i++){
        vector<pair<int, int>> d, dS;
        if(i == VehicleNodes.size()){
            for(int pos = 0; pos < VehicleNodes.size(); pos++){
                d.push_back(make_pair(Distance[0][VehicleNodes[pos]],VehicleNodes[pos]));
                dS.push_back(make_pair(DistanceSer[0][VehicleNodes[pos]],VehicleNodes[pos]));             
            }
        }else{            
            for(int pos = 0; pos < VehicleNodes.size(); pos++){                
                d.push_back(make_pair(Distance[VehicleNodes[i]][VehicleNodes[pos]],VehicleNodes[pos]));
                dS.push_back(make_pair(DistanceSer[VehicleNodes[i]][VehicleNodes[pos]],VehicleNodes[pos]));             
            }
        }                
        sort(d.begin(), d.end());        
        sort(dS.begin(), dS.end()); 
        if(i == VehicleNodes.size()){
            for (int neigh = 0; neigh < (Circle - 1); neigh++){
                FrClose[0][neigh-1] = d[neigh].second;
                FrStays[0][neigh-1] = dS[neigh].second;
            }
        }else{
            int placebo = 1;
            for (int neigh = 1; neigh < Circle; neigh++){                    
                if(d[neigh].second == 0){
                    placebo++;
                    FrClose[VehicleNodes[i]][neigh-1] = d[placebo].second;
                    FrStays[VehicleNodes[i]][neigh-1] = dS[placebo].second;
                }else{            
                    FrClose[VehicleNodes[i]][neigh-1] = d[placebo].second;
                    FrStays[VehicleNodes[i]][neigh-1] = dS[placebo].second; 
                }
                placebo++;                                   
            }
        }        
    }
    for(int i = Mstart; i <= MDend; i++){
        vector<pair<int, int>> d;
        for(int pos = Tstart; pos <= Tend; pos++){
            d.push_back(make_pair(Distance[i][pos],pos));
        }
        sort(d.begin(), d.end());
        for (int pos = 0; pos < Tnode; pos++){
            FrClose[i][pos] = d[pos].second;
            FrStays[i][pos] = d[pos].second;
        }
    }    
    FriendDist = FrClose;
    FriendSer = FrStays;
    cout << "FriendSer.size() = " << FriendSer.size() << "\t";
    cout << "Complete!";
};

In details, the "cout << "Complete!";" is successfully initiated the the code is expected to continue printing the quote "Cannot Continue Point " but it immediately stops there and do nothing, which is very weird. Can you help me or provide me any documents that direct on this issue? Thank you a lot.

//I have identified the fault part: The fault is here:

   if(i == VehicleNodes.size()){
        for (int neigh = 0; neigh < (Circle - 1); neigh++){
            FrClose[0][neigh-1] = d[neigh].second;
            FrStays[0][neigh-1] = dS[neigh].second;
        }
    }

It breaks because of the out of boundary in which FrClose and FrStays will retrieve information [0][-1] Thank you all!

  • 7
    Use a debugger. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jason Mar 07 '23 at 03:50
  • 1
    Replace `InputSet1[0]` with `InputSet1.at(0)`, `InputSet1[1]` with `InputSet1.at(1)`, and so on. Do this for every vector. For all 2D vectors use `at()` for each one of their dimensions. If you do that, the bug should be immediately triggered and easily isolated in your debugger. – Sam Varshavchik Mar 07 '23 at 03:59
  • 1
    If you don't return after a function then you are trashing the stack. First you create two vectors: `vector> FrClose, FrStays;` and then you do this: `FrStays = FrClose;` which probably doesn't do what you think it does. – Jerry Jeremiah Mar 07 '23 at 04:00
  • 1
    By your description maybe your bug is in `InitialVehicle();` In either case instead of guessing what the problem could be use a debugger and step through the code. Perhaps place a breakpoint at `cout << "Cannot Continue Point ";` and see what happens when you have your debugger step 1 line at a time through the `InitialVehicle()` function while you look at the variables and flow after each individual line is executed. – drescherjm Mar 07 '23 at 04:01
  • Dear all, thank you for all of your comments, I have tried all the solutions given and identified the fault. The fault is because of the iterator update mechanism in the "Refresh Friend" between two different size. Thank you a lot. Sincerely! – Minh Nguyen Mar 08 '23 at 06:02
  • 1
    If you found a solution, please post it as an answer. Thank you. – Friedrich Mar 08 '23 at 06:10

1 Answers1

0

The code's fault lies at the "Refresh Friend" part on the operator updates, the second for loop, second if condition:

    if(i == VehicleNodes.size()){
        for (int neigh = 0; neigh < (Circle - 1); neigh++){
            FrClose[0][neigh-1] = d[neigh].second;
            FrStays[0][neigh-1] = dS[neigh].second;
        }
    }

The break happens due to the out of boundary in which "FrClose" and "FrStays" will retrieve information at [0][-1] with neigh = 0. Thank you all for your assists!