Commit 3c797eb2 authored by Sonja Happ's avatar Sonja Happ

modified forward backward sweeping to reduce number of unneccessary sweeps

parent 9c4492c7
......@@ -123,9 +123,9 @@ public:
std::complex<double> get_voltage();
int get_next_action_expected();
bool get_convergence();
virtual void do_backward_sweep(repast::SharedNetwork<Agent, Edge<Agent>,
virtual bool do_backward_sweep(repast::SharedNetwork<Agent, Edge<Agent>,
Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec);
virtual bool do_forward_sweep(repast::SharedNetwork<Agent, Edge<Agent>,
virtual uint do_forward_sweep(repast::SharedNetwork<Agent, Edge<Agent>,
Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec);
virtual void calculate(); // virtual function, to be re-implemented in all derived agent classes
......
......@@ -50,8 +50,8 @@ public:
void step() override;
void do_backward_sweep(repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) override;
bool do_forward_sweep(repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) override;
bool do_backward_sweep(repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) override;
uint do_forward_sweep(repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) override;
private:
......
......@@ -49,8 +49,8 @@ public:
void step() override;
void calculate() override;
void do_backward_sweep(repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) override;
bool do_forward_sweep(repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) override;
bool do_backward_sweep(repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) override;
uint do_forward_sweep(repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) override;
private:
Slack_data model_data; //!< Model data specific for slack
......
......@@ -61,10 +61,10 @@ public:
void calculate() override;
void do_backward_sweep(
bool do_backward_sweep(
repast::SharedNetwork <Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent>> *agent_network_elec) override;
bool do_forward_sweep(
uint do_forward_sweep(
repast::SharedNetwork <Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent>> *agent_network_elec) override;
private:
......
......@@ -114,6 +114,10 @@
#define BACKWARD_SWEEP 0
#define FORWARD_SWEEP 1
#define CONVERGENCE_CHECK 2
#define DONE_NOTHING 0
#define DONE_FORWARD_SWEEP 1
#define DONE_CONVERGENCE_CHECK 2
#define ALREADY_FINISHED 3
/*default communication delay (seconds)*/
#define COMM_DELAY_DEFAULT 0.01
......
......@@ -225,9 +225,10 @@ void Agent::save_adjacent_agents(
*
* virtual function, to be re-implemented in all derived agent classes that are involved in FBS algorithm
* */
void Agent::do_backward_sweep(
bool Agent::do_backward_sweep(
repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) {
//dummy
return false;
}
/*! \brief calculation of forward sweep
......@@ -235,10 +236,10 @@ void Agent::do_backward_sweep(
*
* virtual function, to be re-implemented in all derived agent classes that are involved in FBS algorithm
* */
bool Agent::do_forward_sweep(
uint Agent::do_forward_sweep(
repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) {
//dummy
return false;
return DONE_NOTHING;
}
/*! \brief process all messages that are received in the current simulation step
......
......@@ -121,8 +121,9 @@ void Node_agent::step() {
/*! \brief perform actions of the backward sweep in FBS algorithm
* \param agent_network_elec RepastHPC Shared Network containing electrical connections of agents in this process
* \return true if agent has computed current in this call, false if nothing was done
* */
void Node_agent::do_backward_sweep(
bool Node_agent::do_backward_sweep(
repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) {
//determine if all next nodes have finished BACKWARD sweep
......@@ -160,9 +161,11 @@ void Node_agent::do_backward_sweep(
current = current_n + current_c + i_leak;
next_action_expected=FORWARD_SWEEP;
IO->log_info("calculated current_backward_sweep = (" + std::to_string(current.real()) + "," + std::to_string(current.imag()) + ")");
return true;
}
else{
IO->log_info("\tNothing to do.");
return false;
}
......@@ -171,7 +174,7 @@ void Node_agent::do_backward_sweep(
/*! \brief perform actions of the forward sweep in FBS algorithm
* \param agent_network_elec RepastHPC Shared Network containing electrical connections of agents in this process
* */
bool Node_agent::do_forward_sweep(
uint Node_agent::do_forward_sweep(
repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) {
......@@ -202,9 +205,9 @@ bool Node_agent::do_forward_sweep(
next_action_expected = CONVERGENCE_CHECK;
}
return false;
//return false;
}
else if(next_action_expected==CONVERGENCE_CHECK){
if(next_action_expected==CONVERGENCE_CHECK){
//determine if all next nodes expect BACKWARD sweep next
bool all_next_nodes_expect_BACKWARD = true;
......@@ -242,13 +245,18 @@ bool Node_agent::do_forward_sweep(
next_action_expected = BACKWARD_SWEEP;
IO->log_info("determined convergence to = " + std::to_string(convergence));
return true;
return DONE_CONVERGENCE_CHECK;
}
return false;
return DONE_FORWARD_SWEEP;
}
else{
IO->log_info("\tNothing to do.");
return false;
if(next_action_expected == BACKWARD_SWEEP){
IO->log_info("\tNothing to do (already finished in this sweep).");
return ALREADY_FINISHED;
} else {
IO->log_info("\tNothing to do.");
return DONE_NOTHING;
}
}
}
......
......@@ -155,8 +155,9 @@ void Slack_agent::calculate(){
/*! \brief perform actions of the backward sweep in FBS algorithm
* \param agent_network_elec RepastHPC Shared Network containing electrical connections of agents in this process
* \return true of agent has computed current in this call, false otherwise
* */
void Slack_agent::do_backward_sweep(
bool Slack_agent::do_backward_sweep(
repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) {
//determine if all next nodes have finished BACKWARD sweep
bool all_next_nodes_finished_BACKWARD = true;
......@@ -181,16 +182,18 @@ void Slack_agent::do_backward_sweep(
next_action_expected=FORWARD_SWEEP;
IO->log_info("calculated current_backward_sweep = (" + std::to_string(current.real()) + "," + std::to_string(current.imag()) + ")");
//cerr << current.real() << " " << current.imag() << std::endl;
return true;
}
else{
IO->log_info("\tNothing to do.");
return false;
}
}
/*! \brief perform actions of the forward sweep in FBS algorithm
* \param agent_network_elec RepastHPC Shared Network containing electrical connections of agents in this process
* */
bool Slack_agent::do_forward_sweep(
uint Slack_agent::do_forward_sweep(
repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) {
if(next_action_expected == FORWARD_SWEEP){
......@@ -217,9 +220,9 @@ bool Slack_agent::do_forward_sweep(
std::to_string(voltage.imag()) + ")");
next_action_expected = CONVERGENCE_CHECK;
}
return false;
//return false;
}
else if(next_action_expected==CONVERGENCE_CHECK){
if(next_action_expected==CONVERGENCE_CHECK){
//determine if all next nodes expect BACKWARD sweep next
bool all_next_nodes_expect_BACKWARD = true;
......@@ -255,13 +258,18 @@ bool Slack_agent::do_forward_sweep(
model_data.number_of_iterations++;
next_action_expected = BACKWARD_SWEEP;
IO->log_info("determined convergence to = " + std::to_string(convergence));
return true;
return DONE_CONVERGENCE_CHECK;
}
return false;
return DONE_FORWARD_SWEEP;
}
else{
IO->log_info("\tNothing to do.");
return false;
if(next_action_expected == BACKWARD_SWEEP){
IO->log_info("\tNothing to do (already finished in this sweep).");
return ALREADY_FINISHED;
} else {
IO->log_info("\tNothing to do.");
return DONE_NOTHING;
}
}
......
......@@ -212,7 +212,7 @@ void Transformer_agent::calculate() {
/*! \brief perform actions of the backward sweep in FBS algorithm
* \param agent_network_elec RepastHPC Shared Network containing electrical connections of agents in this process
* */
void Transformer_agent::do_backward_sweep(
bool Transformer_agent::do_backward_sweep(
repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) {
if(substation_data.number_of_solves == 0) {
......@@ -257,9 +257,11 @@ void Transformer_agent::do_backward_sweep(
next_action_expected=FORWARD_SWEEP;
IO->log_info("calculated current_backward_sweep = (" + std::to_string(current.real()) + "," + std::to_string(current.imag()) + ") ratio=" + (std::to_string(substation_data.ratio * ratio_mul)));
return true;
}
else{
IO->log_info("\tNothing to do.");
return false;
}
......@@ -268,7 +270,7 @@ void Transformer_agent::do_backward_sweep(
/*! \brief perform actions of the forward sweep in FBS algorithm
* \param agent_network_elec RepastHPC Shared Network containing electrical connections of agents in this process
* */
bool Transformer_agent::do_forward_sweep(
uint Transformer_agent::do_forward_sweep(
repast::SharedNetwork<Agent, Edge<Agent>, Edge_content<Agent>, Edge_content_manager<Agent> > *agent_network_elec) {
......@@ -304,9 +306,10 @@ bool Transformer_agent::do_forward_sweep(
std::to_string(voltage.imag()) + ")");
next_action_expected = CONVERGENCE_CHECK;
}
return false;
//return false;
}
else if(next_action_expected==CONVERGENCE_CHECK){
if(next_action_expected==CONVERGENCE_CHECK){
//determine if all next nodes expect BACKWARD sweep next
bool all_next_nodes_expect_BACKWARD = true;
......@@ -342,12 +345,17 @@ bool Transformer_agent::do_forward_sweep(
substation_data.number_of_solves++;
next_action_expected = BACKWARD_SWEEP;
IO->log_info("determined convergence to = " + std::to_string(convergence));
return true;
return DONE_CONVERGENCE_CHECK;
}
return false;
return DONE_FORWARD_SWEEP;
}
else{
IO->log_info("\tNothing to do.");
return false;
if(next_action_expected == BACKWARD_SWEEP){
IO->log_info("\tNothing to do (already finished in this sweep).");
return ALREADY_FINISHED;
} else {
IO->log_info("\tNothing to do.");
return DONE_NOTHING;
}
}
}
......@@ -1569,13 +1569,22 @@ void Model::do_backward_sweep() {
IO->start_time_measurement(IO->tm_backward_sweep);
IO->log_info("now in backward sweep");
int counter= 0;
uint have_backward_sweeped = 0;
while(!backward_sweep_finished) {
//perform backward sweep for all node agents
for(auto i : agents_scheduling){ // iterate forward
i->do_backward_sweep(agent_network_elec);
}
if(have_backward_sweeped < agents_scheduling.size()){
//perform backward sweep for all node agents if not all node agents have finished backward sweeping
for(auto i : agents_scheduling){ // iterate forward
bool done_something = false;
done_something = i->do_backward_sweep(agent_network_elec);
if(done_something){
have_backward_sweeped++;
}
IO->log_info("Backward sweep for AgentID " + std::to_string(i->getId().id()) + " done_something=" + std::to_string(done_something) + " (iterating forward)");
}
} else {
IO->log_info("Skipping ...");
}
//check if finished
if (context.contains(repast::AgentId(SLACK_ID, rank, TYPE_SLACK_INT, rank))) {
......@@ -1613,18 +1622,41 @@ void Model::do_forward_sweep() {
IO->log_info("now in forward sweep");
int counter = 0;
bool started_convergence_check = false;
uint checked_convergence = 0;
while(!forward_sweep_finished){
if(!started_convergence_check) {
//perform forward sweep for all node agents
for (unsigned long i = agents_scheduling.size(); i > 0; i--) { // iterate backwards...
started_convergence_check = agents_scheduling[i-1]->do_forward_sweep(agent_network_elec);
if(checked_convergence < agents_scheduling.size()){
if(!started_convergence_check) {
//perform forward sweep for all node agents
for (unsigned long i = agents_scheduling.size(); i > 0; i--) { // iterate backwards...
uint answer = agents_scheduling[i-1]->do_forward_sweep(agent_network_elec);
IO->log_info("Forward sweep for AgentID " + std::to_string(agents_scheduling[i-1]->getId().id()) + "answer=" + std::to_string(answer));
if (answer == DONE_CONVERGENCE_CHECK){
started_convergence_check = true;
checked_convergence++;
} else{
started_convergence_check = false;
}
if (answer == DONE_NOTHING){
break; // stop iteration if one agent could not complete forward sweep because previous agents have not completed yet
}
}
}
}
else{
//perform convergence check for all node agents
for (auto i : agents_scheduling) { // iterate forward...
i->do_forward_sweep(agent_network_elec);
else{
//perform convergence check for all node agents
for (auto i : agents_scheduling) { // iterate forward...
uint answer = i->do_forward_sweep(agent_network_elec);
IO->log_info("Convergence check for Agent ID " + std::to_string(i->getId().id()) + " answer="+ std::to_string(answer));
if(answer == DONE_CONVERGENCE_CHECK){
checked_convergence++;
}
if (answer == DONE_NOTHING){
break; // stop iteration if one agent could to nothing because it is likely that the following agents cannot to anything either
}
}
}
} else {
IO->log_info("Skipping ...");
}
//check if finished
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment