/*
* File name: TestAgent.java (package eas.users.fredy)
* Author(s): Lukas König
* Java version: 8.0 (at generation time)
* Generation date: 23.05.2014 (15:29:20)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.users.fredy.grid.mgm;
import java.util.ArrayList;
import eas.simulation.Wink;
import eas.simulation.event.EASEvent;
import eas.startSetup.ParCollection;
import eas.users.fredy.grid.AbstractFredyAgent;
import eas.users.fredy.grid.FredyEnvironment;
import eas.users.fredy.grid.ParameterClass;
import eas.users.fredy.grid.mgm.components.StimuliTranslator;
import eas.users.fredy.grid.mgm.components.repository.GridIdealLoad;
import eas.users.fredy.grid.mgm.components.repository.GridRealLoad;
import eas.users.fredy.grid.prosumer.ProsumerAgent;
import eas.users.fredy.grid.prosumer.repository.schedule.Schedule;
import eas.users.fredy.grid.utiles.stimuli.Stimuli;
import eas.users.fredy.grid.utiles.stimuli.StimuliStorage;
/**
* Another test.
*
* @author Lukas König
*/
public class ManagerAgent extends AbstractFredyAgent {
private static final long serialVersionUID = 8299734622594133734L;
private int i_current_timeslot;
private ArrayList<GridRealLoad> al_realLoads;
private ArrayList<GridIdealLoad> al_idealLoads;
private static StimuliStorage ss_stimuli;
public ManagerAgent(int id, FredyEnvironment env, ParCollection params) {
super(id, env, params);
System.out.println("MGM: MGM initialized at timeslot 0");
/*this.al_realLoads = new ArrayList<GridRealLoad>();
GridRealLoad grl_first = new GridRealLoad(0);
this.al_realLoads.add(grl_first);
this.al_realLoads.get(0).toString();
this.al_idealLoads = new ArrayList<GridIdealLoad>();
GridIdealLoad gil_first = new GridIdealLoad(0);
this.al_idealLoads.add(gil_first);
this.al_idealLoads.get(0).toString();
*/
ManagerAgent.ss_stimuli = new StimuliStorage();
}
@Override
public void handleEvent(EASEvent e, Wink lastActionTime) {
}
public int getMyIntegerParameter() {
return ParameterClass.getNumberOfProsumers();
}
@Override
public void step(Wink simTime) {
super.step(simTime);
this.i_current_timeslot = (int) Math.round(simTime.getCurrentTime());
System.out.println("-MGM: Here is manager agent " + this.id()
+ " at timeslot " + this.i_current_timeslot+"...");
Stimuli s_aux = null;
if( this.i_current_timeslot > 0 ) {
/*
* Checking stimuli construction process: real vs ideal load
*/
s_aux = StimuliTranslator.getStimuliSignal(
this.al_realLoads.get(this.al_realLoads.size() - 1),
this.al_idealLoads.get(this.al_idealLoads.size() - 1),
this.i_current_timeslot);
ManagerAgent.ss_stimuli.addStimuli(s_aux, this.i_current_timeslot);
//System.out.println("-MGM -STEP: Printing stimuli at "+this.i_current_timeslot+"\n"+s_aux.toString());
}
else{
s_aux = StimuliTranslator.getStimuliSignal(
this.al_realLoads.get(0),
this.al_idealLoads.get(0),
0);
ManagerAgent.ss_stimuli.addStimuli(s_aux, this.i_current_timeslot);
//System.out.println("-MGM -STEP: Printing stimuli at "+this.i_current_timeslot+"\n"+s_aux.toString());
}
System.out.println("-MGM -STEP: Broadcasting message at "+this.i_current_timeslot);
//System.out.println("-MGM -STEP: Printing stimuli to be broadcasted\n"+s_aux.toString());
/*System.out.println("-MGM -STEP: Printing stimuli and loads\n");
for(int i = 0; i < s_aux.getStimuli().length ;i++){
System.out.println("-\t timeslot "+(i+this.i_current_timeslot)+" - stimuli "
+ s_aux.getStimuli()[i]
+ " - Real Load "
+ this.al_realLoads.get(this.al_realLoads.size() - 1)
.getGridLoad()[i+this.i_current_timeslot]
+ " - Ideal Load "
+ this.al_idealLoads.get(this.al_idealLoads.size() - 1)
.getIdealLoad()[i+this.i_current_timeslot]);
}*/
this.broadcastMessageToProsumers(s_aux);
}
protected void receiveMessageFromProsumer(String message, int id) {
System.out.println("-MGM : Message " + message + " received from agent " + id);
}
/**
* Receives a schedule from a prosumer with identification number id
* and add the loads to the total of the micro-grid.
*
* @param obj
* @param id
*/
public void receiveFirstScheduleFromProsumer(Object obj, int id) {
System.out.println("-MGM: First Object received from Prosumer " + id);
if(this.al_idealLoads == null || this.al_realLoads == null){
System.out.println("-MGM: Initializing load arrays...");
this.al_realLoads = new ArrayList<GridRealLoad>();
GridRealLoad grl_first = new GridRealLoad(0);
this.al_realLoads.add(grl_first);
//System.out.println("\tPrinting first load received:\n\t"+this.al_realLoads.get(0).toString());
/*
* Checking initiation of ideal loads
* CHECKED!!!!
*/
this.al_idealLoads = new ArrayList<GridIdealLoad>();
GridIdealLoad gil_first = new GridIdealLoad(0);
this.al_idealLoads.add(gil_first);
//System.out.println("\tPrinting ideal load:\n\t"+this.al_idealLoads.get(0).toString());
}
try{
Schedule s_schedule = (Schedule) obj;
this.al_realLoads.get(0).addLoads(s_schedule);
}
catch(Exception e){
System.out.println(e);
e.printStackTrace();
}
}
/**
* Receives a schedule from a prosumer with identification number id
* and add the loads to the total of the micro-grid.
*
* @param obj
* @param id
*/
public void receiveMessageFromProsumer(Object obj, int id) {
System.out.println("-MGM: Object received from agent " + id);
try{
Schedule s_schedule = (Schedule) obj;
this.al_realLoads.get(this.al_realLoads.size() - 1).addLoads(s_schedule);
}
catch(Exception e){
System.out.println(e);
System.out.println("-MGM: Failure with object received from Prosumer "+id);
}
/*Iterator<GridRealLoad> itr = this.al_realLoads.iterator();
int counter = 0;
while(itr.hasNext()){
if(itr.next().getValidity() == s_schedule.getValidityOfSchedule()){
itr.next().addLoads(s_schedule);
}
} */
}
/**
* Broadcasts a message to every prosumer with the new stmuli values considering the information
* of the current timeslot.
*
*/
protected void broadcastMessageToProsumers(Stimuli s_stimuli) {
//System.out.println("-MGM: Message broadcast by Micro-Grid Manager, ID " + this.id());
//System.out.println("-MGM -BROADCAST: Printing stimuli to be broadcasted\n"+s_stimuli.toString());
for (AbstractFredyAgent a : this.getEnvironment().getAgents()) {
//System.out.println("-MGM: Inside loop - 'broadcastMessageToProsumers()'");
try {
//System.out.println("-MGM: Inside 'try' - 'broadcastMessageToProsumers()'");
if(ProsumerAgent.class.isInstance(a)){
//System.out.println("-MGM -BROADCAST: Printing stimuli to be broadcasted\n"+s_stimuli.toString());
((ProsumerAgent) a).receiveMessageFromManager(this.id(),s_stimuli);
/*System.out
.println("-MGM -BROADCAST: Stimuli broadcasted to PROSUMER "
+ ((ProsumerAgent) a).id()
+ " at "
+ this.i_current_timeslot);*/
}
//((ProsumerAgent) a).receiveMessageFromManager(this.id(),
// this.ss_stimuli.getStimuli(i_current_timeslot));
} catch (Exception e) {
System.out.println("-MGM -BROADCAST: Error in delivering message to prosumers");
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
}