Output Jsoncpp
-
Hi, ich habe mein Code in Json Datei geschrieben und möchte die ergebniss in ein neu Json Datei schreiben.
in Main Methode bekomme ich ein Speicherzugriffsfehler.void TspLibInstance::readJson(istream& r) { Json::Value root; int s1; int s2; r >> root; name = root.get("name","").asString(); type = root.get("type","").asString(); comment = root.get("comment","").asString(); _n = root.get("dimension",0).asInt(); edgeWeightType = root.get("edge_weight_type","").asString(); edgeWeightFormat = root.get("edge_weight_format","").asString(); edgeDataFormat = root.get("edge_data_format","").asString(); nodeCoordType = root.get("node_coord_type","").asString(); displayDataType = root.get("display_data_type","").asString(); // read edge weights if present s1 = root["edge_weights"].size(); //cout << "edge_weights.size() == " << s1 << endl; if (s1 > 0) { edgeWeight.clear(); edgeWeight.resize(s1); for (int i=0 ; i<s1 ; i++) { s2 = root["edge_weights"][i].size(); for (int j=0 ; j<s2 ; j++) { edgeWeight[i].push_back(root["edge_weights"][i][j].asInt()); } } } // read node coordinates if present s1 = root["node_coordinates"].size(); if (s1 > 0) { nodeCoordinates.clear(); nodeCoordinates.resize(s1); for (int i=0 ; i<s1 ; i++) { s2 = root["node_coordinates"][i].size(); for (int j=0 ; j<s2 ; j++) { nodeCoordinates[i].push_back(root["node_coordinates"][i][j].asDouble()); } } } // read display data if present s1 = root["display_data"].size(); if (s1 > 0) { displayData.clear(); displayData.resize(s1); for (int i=0 ; i<s1 ; i++) { s2 = root["display_data"][i].size(); for (int j=0 ; j<s2 ; j++) { displayData[i].push_back(root["display_data"][i][j].asDouble()); } } } // read edge data if present s1 = root["edge_data"].size(); if (s1 > 0) { edges.clear(); for (int i=0 ; i<s1 ; i++) { s2 = root["edge_data"][i].size(); assert(s2 == 2); Edge e; e.v = root["edge_data"][i][0].asInt(); e.w = root["edge_data"][i][1].asInt(); e.value = 0.0; edges.push_back(e); } } // bounds lb = root.get("lowerbound",-1).asInt64(); ub = root.get("upperbound",-1).asInt64(); tourLength = root.get("tourlength",-1).asInt64(); // read solutions s1 = root["tour"].size(); if (s1 > 0) { tour.resize(s1); for (int i=0 ; i<s1 ; i++) { tour[i] = root["tour"][i].asInt(); } } } void TspLibInstance::writeJson(ostream& w) { Json::Value root; Json::StreamWriterBuilder wbuilder; root["name"] = name; root["type"] = type; if (comment != "") { root["comment"] = comment; } root["dimension"] = _n; root["edge_weight_type"] = edgeWeightType; if (edgeWeightType == "EXPLICIT") { root["edge_weight_format"] = edgeWeightFormat; for (int i=0 ; i<edgeWeight.size() ; i++) { for (int j=0 ; j<edgeWeight[i].size() ; j++) { root["edge_weights"][i][j] = edgeWeight[i][j]; } } } if (nodeCoordType != "") { root["node_coord_type"] = nodeCoordType; } if (nodeCoordinates.size() > 0) { for (int i=0 ; i<_n ; i++) { for (int j=0 ; j<nodeCoordinates[i].size() ; j++) { root["node_coordinates"][i][j] = nodeCoordinates[i][j]; } } } if (displayDataType != "") { root["display_data_type"] = displayDataType; if (displayDataType == "TWOD DISPLAY") { for (int i=0 ; i<_n ; i++) { for (int j=0 ; j<displayData[i].size() ; j++) { root["display_data"][i][j] = displayData[i][j]; } } } } if (edgeDataFormat != "") { root["edge_data_format" ] = edgeDataFormat; if (edgeDataFormat == "EDGE_LIST") { for (int i=0 ; i<edges.size() ; i++) { root["edge_data"][i][0] = edges[i].v; root["edge_data"][i][1] = edges[i].w; } } } if (tourLength >= 0) { root["tourlength"] = tourLength; } if (lb >= 0) { root["lowerbound"] = lb; } if (ub >= 0) { root["upperbound"] = ub; } if (tour.size() > 0) { for (int i=0 ; i<_n ; i++) { root["tour"][i] = tour[i]; } } wbuilder["indentation"] = ""; // falls GEO Genauigkeit bei der Ausgabe auf 2 Stellen reduzieren //wbuilder.settings_["precision"] = 2; w << Json::writeString(wbuilder, root); w << endl; }
und den zweite code die include der erste Code
#include "b.cpp" double Graph::getPathLength(std::vector<int> finalPath) { double path_length = 0; for(int i=0; i<finalPath.size()-1; i++) { int first = finalPath[i]; int second = finalPath[i+1]; path_length += m_AdjGraph[first][second]; } return path_length; } int Graph::getNearestNeighbour(int curr_node, bool visited[]) { int minDist = 999999; int minIndex = -1; for(int i=0; i < m_NoOfNodes; i++) { if(m_AdjGraph[curr_node][i] < minDist && !visited[i]) { minDist = m_AdjGraph[curr_node][i]; minIndex = i; } } return minIndex; } void Graph::nearestNeighbourHeuristics(int startingNode) { int startingIndex = startingNode - 1; bool visited[m_NoOfNodes]; for (int i=0; i < m_NoOfNodes; i++) { visited[i] = false; } int noOfVisitedNodes = 1; std::vector<int> finalPath; // start from the starting index and loop until you visit all nodes int curr_node = startingIndex; visited[curr_node] = true; finalPath.push_back(curr_node); while(noOfVisitedNodes < m_NoOfNodes) { curr_node = getNearestNeighbour(curr_node, visited); if(curr_node != -1) { visited[curr_node] = true; finalPath.push_back(curr_node); } noOfVisitedNodes += 1; } std::cout << "Path with nearest neighbour approach" << std::endl; finalPath.push_back(startingIndex); // Print final path for(int i=0; i<finalPath.size(); i++) { std::cout << finalPath[i]+1 << " "; } std::cout << std::endl; // Final path length double path_length = getPathLength(finalPath); std::cout << "Path length with nearest neighbour: " << path_length << std::endl; } int main(int arg, char *argv[]){ vector<array<double,2>> nodesVector; std::ifstream file1; file1.open("bb.json"); Tsppd t(file1); t.nearestNeighbourHeuristics(1); return 0; }
-
@const Und wo genau soll der Speicherzugriffsfehler sein?
Deine Main ist da eher unverdächtig. Aber dein Code passt sonst nicht zur
main
. Du zeigst weder denTsppd
Konstruktor noch die darauf aufgerufenen Suche. Auch die Definitionen der Klassen fehlen.Und, warum inkludierst du eine "cpp" Datei?
Und, damit du auch wirklich Hilfe bekommen kannst, empfehle ich dir mal den Post zu lesen: Du brauchst Hilfe
-
@const sagte in Output Jsoncpp:
for(int i=0; i<finalPath.size()-1; i++) {
Das ist falsch, wenn
size()
0 ist.