コード例 #1
0
ファイル: graph.cpp プロジェクト: harry830622/smart-grid
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;
}