/*
* 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.prosumer;
import java.util.ArrayList;
import java.util.Random;
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.prosumer.components.Rescheduler;
import eas.users.fredy.grid.prosumer.components.StimuliProcessor;
import eas.users.fredy.grid.prosumer.repository.RepositoryManager;
import eas.users.fredy.grid.prosumer.repository.heuristics.HeuristicValue;
import eas.users.fredy.grid.prosumer.repository.heuristics.HeuristicValues;
import eas.users.fredy.grid.prosumer.repository.schedule.Programm;
import eas.users.fredy.grid.prosumer.repository.schedule.Schedule;
import eas.users.fredy.grid.prosumer.repository.schedule.ScheduleManager;
import eas.users.fredy.grid.utiles.stimuli.Stimuli;
import eas.users.fredy.grid.utiles.stimuli.StimuliStorage;
/**
* @author Lukas König
*/
public class ProsumerAgent extends AbstractFredyAgent {
//This is a change!
private static final long serialVersionUID = 8299734622594133734L;
//private AbstractStimuli as_stimuli;
@SuppressWarnings("unused")
private RepositoryManager rm_repManager;
private ScheduleManager sm_scheduleManager;
private HeuristicValues hv_heuristicValues;
private StimuliStorage ss_stimuli;
private int i_currentTimeslot;
private Random rand;
public ProsumerAgent(int id, FredyEnvironment env, ParCollection params, Random rand) {
super(id, env, params);
this.rand = rand;
System.out.println("-PROSUMER "+this.id()+": Initialized, creating first scchedule...");
sm_scheduleManager = new ScheduleManager();
rm_repManager = new RepositoryManager();
this.sendFirstScheduleToManager();
ss_stimuli = new StimuliStorage();
hv_heuristicValues = new HeuristicValues();
}
@Override
public void handleEvent(EASEvent e, Wink lastActionTime) {
}
public int getMyIntegerParameter() {
return ParameterClass.getNumberOfProsumers();
}
@Override
public void step(Wink simTime) {
/*
* Each timeslot (simTime) represent five minutes in the schedule.
*/
super.step(simTime);
this.i_currentTimeslot = (int) Math.round(simTime.getCurrentTime());
System.out.println("-PROSUMER "+this.id()+": Here is prosumer agent " + this.id()+" time is: "+i_currentTimeslot);
/*if (Rescheduler.reschedule((int) Math.round(simTime.getCurrentTime())))
System.out.println("Reschedule stored...");
else
System.out.println("Error in storing the new schedule");
*/
//System.out.println("-PROSUMER: Schedule: "+this.sm_scheduleManager.toString());
if((int) Math.round(simTime.getCurrentTime()) > 0){
//System.out.println("-PROSUMER "+this.id()+": Schedule: "+this.sm_scheduleManager.toString());
/*
* Check the correct behavior of method "getReschedulableAppliances"
* CHECKED!!!
*/
ArrayList<Programm> al_reschedubleApp = this.sm_scheduleManager.getReschedulableAppliances(i_currentTimeslot);
if(al_reschedubleApp != null){
/*
* The stimuli of the previous timeslot are requested.
* CHECKED!!!
*/
//System.out.println("-PROSUMER "+this.id()+" Printing stimuli:\n\t"+this.ss_stimuli.toString());
Stimuli s_stimuliForHeuristics = (Stimuli) this.ss_stimuli.getStimuli(this.i_currentTimeslot - 1);
//System.out.println("-PROSUMER "+this.id()+": Getting heuristic values "+s_stimuliForHeuristics.toString());
/*
* Checking method "getHeuristicValues"
* CHECKED!!!
*/
double[] d_heuristics = StimuliProcessor.getHeuristicValues(s_stimuliForHeuristics);
/*
* Checking rescheduling process
* CHECKED!
*/
//System.out.println("-PROSUMER "+this.id()+": Initiating rescheduling process...");
Schedule s_updatedSchedule = Rescheduler.reschedule(
this.i_currentTimeslot, al_reschedubleApp, d_heuristics, rand);
//System.out.println("-PROSUMER "+this.id()+": Rescheduled appliances\n\t"+s_updatedSchedule.toString());
/*
* Checking schedule updating process
* CHECKED!!!
*/
this.sm_scheduleManager.updateSchedule(s_updatedSchedule, i_currentTimeslot);
//System.out.println("-PROSUMER "+this.id()+": Printing schedules\n\t"+this.sm_scheduleManager.toString());
this.hv_heuristicValues.addHeuristicValue(new HeuristicValue(this.i_currentTimeslot,d_heuristics));
}
else{
this.sm_scheduleManager.updateSchedule(null, i_currentTimeslot);
this.hv_heuristicValues.addHeuristicValue(new HeuristicValue(this.i_currentTimeslot,null));
}
this.sendMessageToManager();
}
}
public void receiveMessageFromManager(int id, String message) {
System.out.println("-PROSUMER "+this.id()+": Message " + message + " received from MGM " + id);
}
@SuppressWarnings("unused")
public void receiveMessageFromManager(int id, Object obj) {
try{
Stimuli s_aux = (Stimuli) obj;
this.ss_stimuli.addStimuli(s_aux, s_aux.getTimeSlot());
System.out.println("-PROSUMER " + this.id()
+ ": Stimuli received from MGM at timeslot "
+ this.i_currentTimeslot);
//System.out.println("-PROSUMER "+this.id()+" Printing stimuli\n" + s_aux.toString());
}
catch(Exception e){
System.out.println("-PROSUMER "+this.id()+": Error in translating sitmuli...");
System.out.println(e.getMessage());
}
}
protected void sendMessageToManager() {
/*System.out.println("-PROSUMER " + this.id()
+ ": Sending message to MGM at time: "
+ i_currentTimeslot);*/
this.getEnvironment()
.getManagerAgent()
.receiveMessageFromProsumer(
this.sm_scheduleManager.getSchedule(i_currentTimeslot), this.id());
}
protected void sendFirstScheduleToManager() {
/*System.out.println("-PROSUMER " + this.id()
+ ": Sending first schedule to MGM at time");*/
this.getEnvironment()
.getManagerAgent()
.receiveFirstScheduleFromProsumer(
this.sm_scheduleManager.getFirstSchedule(), this.id());
}
}