コード例 #1
0
// Run a simulation finding the shortest paths in a given graph 
void MonteCarlo::run(Graph g)
{
  static int turn=0;
  
  cout << endl << "=== RUNNING SIMULATION No. " << ++turn << " ===" << endl;
  
  // Print out graph information
  double d = static_cast<double>(g.E())/((static_cast<double>(g.V())*static_cast<double>(g.V())-1)/2)*100;	// Calculate real density reached
  cout << "Vertices: " << g.V() << endl;
  cout << "Edges: " << g.E() << " (density: " << d << "%)" << endl;
  cout << "Graph: " << endl;
  g.show();

  // Print out shortest path information
  list<char> v = g.vertices();
  cout << endl << "Vertices: " << v << endl; 
  int reachVert=0, sumPathSize=0, avgPathSize=0;
  ShortestPath sp(g);
  for (list<char>::iterator i=++v.begin(); i != v.end(); ++i) 
  {
    char src = v.front();
    char dst = (*i);
    list<char> p = sp.path(src,dst);
    int ps = sp.path_size(src,dst);
    if (ps != INFINIT)
      cout << "ShortestPath (" << src << " to " << dst << "): " << ps << " -> " << p << endl;
    else
      cout << "ShortestPath (" << src << " to " << dst << "): " << "** UNREACHABLE **" << endl;      
    if (ps!=INFINIT)
    {
      reachVert++;		// Sum up reached nodes 
      sumPathSize += ps;	// Sum up shortest paths found
    }
  }  
  
  // Calculate average shortest path and print it out
  if (reachVert!=0)
    avgPathSize = sumPathSize / reachVert;	
  else
    avgPathSize = 0;
  cout << endl << "AVG ShortestPath Size (reachVert: " << reachVert << " - sumPathSize: " << sumPathSize << "): " << avgPathSize << endl;
}
コード例 #2
0
// Run a simulation finding the Prim's and Kruskal's MST in a given graph 
void MonteCarlo::run(Graph g)
{
  static int turn=0;
  
  cout << endl << "=== RUNNING SIMULATION No. " << ++turn << " ===" << endl;
  
  // Print out graph information
  double d = static_cast<double>(g.E())/((static_cast<double>(g.V())*static_cast<double>(g.V())-1)/2)*100;	// Calculate real density reached
  cout << "Vertices: " << g.V() << endl;
  cout << "Edges: " << g.E() << " (density: " << d << "%)" << endl;
  cout << "Nodes: " << g.vertices() << endl;
  cout << "Graph: " << g << endl;
  cout << "Graph (.dot format): " << endl;
  cout << "graph Homework3 {" << endl;
  // Insert all edges from the graph into priority queue
  list<Node> allNodes = g.vertices(); 
  PriorityQueue<Edge> p;
  for(list<Node>::iterator i=allNodes.begin(); i != allNodes.end(); ++i) {
    list<Node> iEdges = g.neighbors((*i).getNodeNumber());
    for(list<Node>::iterator j=iEdges.begin(); j != iEdges.end(); ++j) {
      Edge e((*i).getNodeNumber(),(*j).getNodeNumber(),(*j).getEdgeWeight());
      p.insert(e);
    }
  }
  cout << p;
  cout << "}" << endl;
  
  PrimMST prim(g);
  cout << "Prim MST (.dot format): " << endl;
  cout << "graph PrimMST {" << endl;
  cout << prim.tree();
  cout << "}" << endl;
  cout << "Prim MST cost: " << prim.cost() << endl;
  cout << endl;

  KruskalMST kruskal(g);
  cout << "Kruskal MST (.dot format): " << endl;
  cout << "graph KruskalMST {" << endl;
  cout << kruskal.tree();
  cout << "}" << endl;
  cout << "Kruskal MST cost: " << kruskal.cost() << endl;
}
コード例 #3
0
std::vector<Edge> edges(Graph &G){
	int E = 0;
	std::vector<Edge> a(G.E());
	for(int v=0; v<G.V(); v++){
		typename Graph::adjIterator A(G, v);
		for(int w = A.beg(); !A.end(); w = A.nxt())
			if(G.directed() || v<w)
				a[E++] = Edge(v, w);
	}
	return a;
}
コード例 #4
0
ファイル: evnav.cpp プロジェクト: hexa00/evnav
void Evnav::route(EvnavRequest &req, QJsonObject &json)
{
    Trip trip;
    Graph g;

    VertexId srcId = -1;
    VertexId dstId = -2;

    double SOC_dyn = req.m_SOC_max - req.m_SOC_min;
    double batt_act = req.m_battery * req.m_SOC_act; // kWh
    double batt_dyn = req.m_battery * SOC_dyn;

    if (computeTrip(req.m_src, req.m_dst, trip) == Status::Ok) {
        double e = computeEnergy(trip, req.m_efficiency);
        double e_otw = 0; // kWh
        if (e > batt_act) {
            e_otw = e - batt_act;
        }
        qDebug() << "energy required  : " << e << "kWh";
        qDebug() << "energy start     : " << batt_act << "kWh";
        qDebug() << "energy on the way: " << e_otw << "kWh";
        if (e < batt_act) {
            qDebug() << "reaching destination without charging";

            // FIXME: collect result processing
            json["code"] = "Ok";
            json["message"] = "reachable";

            QJsonObject summary;
            summary["distance"] = trip.dist_m;
            summary["duration"] = trip.time_s;
            summary["energy"] = e;
            summary["driving_duration"] = trip.time_s;
            summary["charging_duration"] = 0;
            summary["charging_cost"] = 0;
            json["route_summary"] = summary;
            json["charging_steps"] = QJsonArray{};
            return;
        } else {
            int min_stops = std::ceil(e_otw / batt_dyn);
            double charging_time = computeChargingTime(e_otw, req.m_power_avg);
            qDebug() << "charging min_stops:" << min_stops;
            qDebug() << "charging min_time :" << formatTime(charging_time);
        }
    }


    makeGraph(g, req, srcId, dstId);
    qDebug() << "graph size:" << g.E();
    // TODO: write the graph as Json

    ShortestPath sp(g, srcId);
    if (sp.hasPathTo(dstId)) {
        QVector<Edge> path = sp.pathTo(dstId).toVector();
        write(path, json);
    } else {
        json["code"] = "Ok";
        json["message"] = "unreachable";
        qDebug() << "cannot reach destination with this electric car";
    }

}