Ejemplo n.º 1
0
void FindArticulationPoints(Vertex* vertex, int depth)
{
  vertex->SetDepth(depth);
  vertex->SetLow(depth);

  for (int i = 0; i < vertex->GetIncidentEdgesNum(); ++i) {
    Vertex* child = vertex->GetIncidentEdge(i)->GetNeighbor(vertex);

    if (child != vertex->GetParent()) {
      if (!child->GetIsVisited()) {
        child->SetParent(vertex);
        vertex->AddChild(child);

        FindArticulationPoints(child, depth + 1);

        if (child->GetLow() >= vertex->GetDepth()) {
          vertex->SetIsArticulate(true);
          continue;
        }

        if (child->GetLow() < vertex->GetLow()) {
          vertex->SetLow(child->GetLow());
        }
      } else {
        if (child->GetDepth() < vertex->GetLow()) {
          vertex->SetLow(child->GetDepth());
        }
      }
    }
  }
}
Ejemplo n.º 2
0
void Graph::CountCost (){
  ResetVerticesMarks();

  for (auto source:source_vertices_) {   //bfs for each source;
    ResetVerticesMarks();
    queue<Vertex*> bfs_queue;
    bfs_queue.push(source);
    source->SetIsVisted(true);
    string source_name = source ->GetRaw()->GetName();
    vector <Vertex*> vector_leafs;
    //cout <<"Start:"<<source_name<<endl;
    while (!bfs_queue.empty()){
      Vertex* front = bfs_queue.front();
      bfs_queue.pop();
      for (int i=0; i<front->GetIncidentEdgesNum();++i){
        Vertex* child = front->GetIncidentEdge(i)->GetNeighbor(front);
        Edge* connect_edge = front->GetIncidentEdge(i);
        if (child->GetType()==Vertex::Type::PSEUDO){
          child = dynamic_cast<PseudoVertex*>(child)->GetBoundaryVertex(front->GetIncidentEdge(i));
          connect_edge = edges_[front->GetIncidentEdge(i)->GetRaw()->GetName()];
        }

        if (child->GetIsVisited() || child->GetAssignSource()!=source_name){
          continue;
        }
        child->SetIsVisted(true);
        child->SetParent(front);
        front->AddChild(child);
        bfs_queue.push(child);
        child->SetParentEdge(connect_edge);
      }
      if (front->GetChildrenNum() == 0){
        vector_leafs.push_back(front);
      } else if (front->GetType() == Vertex::Type::RESIDENT ){
        cout <<"error!!! :resident has more one edge"<<endl;
      }
    }
    /*
       for (auto pair: vertices_) {
       if (!pair.second->GetIsVisited() && pair.second->GetAssignSource()==source_name){
       cout <<"someting wrong!!"<<endl;
       }
       }
       */
    cout <<"process:"<<source_name<<" leafnum:"<<vector_leafs.size()<<endl;
    for (auto child : vector_leafs){
      //Vertex* child = pair.second;
      if (child->GetType() == Vertex::Type::RESIDENT && child->GetAssignSource() == source_name) {
        //cout <<"IN!! "<<endl;
        double load_current = dynamic_cast<ResidentVertex*>(child)->GetConsumingPower()/child->GetVoltage();
        //cout <<"load:"<< load_current<<endl;
        while (child!=source) {
          Vertex* parent = child->GetParent();
          Edge* connect_edge = child->GetParentEdge();
          assert(connect_edge->GetNeighbor(child)==parent);
          connect_edge->SetCurrent(connect_edge->GetCurrent()+load_current);
          child = parent;
        }
      }
    }
    //cout <<"finish:"<<source_name<<endl;
  }

  //cout <<"counting..."<<endl;
  wire_upgrade = 0.0;
  switch_change =0;

  for (auto pair:edges_) {
    if (pair.second->GetType()==Edge::Type::EDGE){
      Wire* wire = pair.second->GetRaw();
      if (pair.second->GetCurrent() > wire->GetCurrentLimit()){
        //cout <<"Current:"<< pair.second->GetCurrent()<< "  Limit:"<< wire->GetCurrentLimit()<<endl;
        wire_upgrade += pair.second->GetCurrent()-wire->GetCurrentLimit();
      }
    }else if(pair.second->GetType()==Edge::Type::SWITCH){
      Switch* switches = dynamic_cast<Switch*> (pair.second->GetRaw());
      Vertex* a = pair.second->GetIncidentVertex(0);
      Vertex* b = pair.second->GetIncidentVertex(1);

      if ( (a->GetAssignSource() != b->GetAssignSource() && switches->GetIsOn()) || (a->GetAssignSource() == b->GetAssignSource() && !switches->GetIsOn() ))
        switch_change++;
    }
  }
  cout << "wire upgrade :" << wire_upgrade << endl;
  cout << "wire change  :" << switch_change << endl;
}
Ejemplo n.º 3
0
Graph* Graph::ShrinkByArticulationPoints()
{
  Graph* shrinked_graph = new Graph(grid_);

  const string pseudo_vertex_prefix("PAV");
  int pseudo_vertex_counter = 0;

  ResetVerticesMarks();

  map<Vertex*, PseudoVertex*> pseudo_vertices;

  queue<Vertex*> bfs_queue;
  root_->SetIsVisted();
  bfs_queue.push(root_);
  while (!bfs_queue.empty()) {
    Vertex* front = bfs_queue.front();
    bfs_queue.pop();

    shrinked_graph->AddVertex(front);

    for (int i = 0; i < front->GetIncidentEdgesNum(); ++i) {
      Edge* incident_edge = front->GetIncidentEdge(i);
      Vertex* child = incident_edge->GetNeighbor(front);

      if (child != front->GetParent()) {
        if (!child->GetIsVisited()) {
          child->SetIsVisted();

          front->AddChild(child);
          child->SetParent(front);

          if (child->GetIsArticulate()) {
            const string pseudo_vertex_name(pseudo_vertex_prefix + to_string(pseudo_vertex_counter++));

            Node* new_node = new Node(pseudo_vertex_name);

            grid_->GetSmartGrid()->AddEquipment(new_node);

            PseudoVertex* pseudo_vertex = new PseudoVertex(new_node);

            Edge* new_edge = new Edge(incident_edge->GetRaw());

            pseudo_vertex->AddBoundaryVertex(new_edge, child);

            pseudo_vertex->AddIncidentEdge(new_edge);

            new_edge->AddIncidentVertex(front);
            new_edge->AddIncidentVertex(pseudo_vertex);

            front->SetIncidentEdge(i, new_edge);

            shrinked_graph->AddVertex(pseudo_vertex);
            shrinked_graph->AddEdge(new_edge);

            pseudo_vertices.insert(make_pair(child, pseudo_vertex));
          } else {
            bfs_queue.push(child);

            shrinked_graph->AddVertex(child);
            shrinked_graph->AddEdge(incident_edge);
          }
        } else {
          if (child->GetIsArticulate()) {
            assert(pseudo_vertices.count(child) == 1);

            PseudoVertex* pseudo_vertex = pseudo_vertices.find(child)->second;

            Edge* new_edge = new Edge(incident_edge->GetRaw());

            pseudo_vertex->AddBoundaryVertex(new_edge, child);

            pseudo_vertex->AddIncidentEdge(new_edge);

            new_edge->AddIncidentVertex(front);
            new_edge->AddIncidentVertex(pseudo_vertex);

            front->SetIncidentEdge(i, new_edge);

            shrinked_graph->AddEdge(new_edge);
          } else {
            shrinked_graph->AddEdge(incident_edge);
          }
        }
      }
    }
  }

  for (auto pair : pseudo_vertices) {
    pair.second->MergeDescendants(pair.first);
  }

  return shrinked_graph;
}