
io.github.agentsoz.conservation.outputwriters.AgentsStatisticsWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of conservation-ethics Show documentation
Show all versions of conservation-ethics Show documentation
Application using JACK-GAMS integration
The newest version!
package io.github.agentsoz.conservation.outputwriters;
/*
* #%L
* BDI-ABM Integration Package
* %%
* Copyright (C) 2014 - 2015 by its authors. See AUTHORS file.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import io.github.agentsoz.conservation.Log;
import io.github.agentsoz.conservation.LandholderHistory.AuctionRound;
import java.io.FileWriter;
import java.io.IOException;
import java.util.TreeMap;
/**
* This class writes statistics about agents to csv files. A separate file is
* generated for each repeat. Rows of the output file represent auction cycles.
* Columns represent agents.
*
* The statistics written in to different output files:
*
* agents_ce_.csv : conservation ethic barometers of agents
*
* agents_pm_.csv : profit motive barometers of agents
*
* number_of_bids_per_agent.csv : number of bids made by each agent in
* each auction cycle.
*
* number_of_successful_bids_per_agent.csv : number of successful bids
* made by each agent in each auction cycle.
*
* total_opportunity_cost_per_agent.csv : total opportunity cost of all
* bids made by each agent in each auction cycle
*
* successful_opportunity_cost_per_agent.csv : total opportunity cost of
* all successful bids made by each agent in each auction cycle
*
* total_bid_price_per_agent.csv : total price of all bids made by each
* agent in each auction cycle
*
* successful_bid_price_per_agent.csv : total price of all successful
* bids made by each agent in each auction cycle
*
* @author Sewwandi Perera
*/
public class AgentsStatisticsWriter {
/**
* Current repeat
*/
private int repeat;
/**
* Singleton instance
*/
private static AgentsStatisticsWriter instance = new AgentsStatisticsWriter();
/**
* All agents conservation ethic barometers in each auction cycle. Key
* fields of the TreeMap contain the auction cycles. Value fields contain
* another TreeMap of agent names and their CE barometers.
*/
private TreeMap> agentsCE;
/**
* All agents profit motive barometers in each auction cycle. Key fields of
* the TreeMap contain the auction cycles. Value fields contain a TreeMap of
* agent names and their PM barometers.
*/
private TreeMap> agentsPM;
/**
* Number of bids made by each agent in each auction cycle. Key fields of
* the TreeMap contain the auction cycles. Value fields contain a TreeMap of
* agent names and number of bids they made.
*/
private TreeMap> numberOfBids;
/**
* Number of successful bids made by each agent in each auction cycle. Key
* fields of the TreeMap contain the auction cycles. Value fields contain a
* TreeMap of agent names and the number of successful bids they made.
*/
private TreeMap> numberOfSuccessfulBids;
/**
* Total opportunity cost of all bids made by each agent in each auction
* cycle. Key fields of the TreeMap contain the auction cycles. Value fields
* contain a TreeMap of agent names and total opportunity cost of all bids
* they made.
*/
private TreeMap> totalOpportunityCost;
/**
* Total opportunity cost of successful bids made by each agent in each
* auction cycle. Key fields of the TreeMap contain the auction cycles.
* Value fields contain a TreeMap of agent names and total opportunity cost
* of successful bids they made.
*/
private TreeMap> totalOpportunityCostOfSuccessfulBids;
/**
* Total price of all bids made by each agent in each auction cycle. Key
* fields of the TreeMap contain the auction cycles. Value fields contain a
* TreeMap of agent names and total price of all bids they made.
*/
private TreeMap> totalBidPrices;
/**
* Total price of successful bids made by each agent in each auction cycle.
* Key fields of the TreeMap contain the auction cycles. Value fields
* contain a TreeMap of agent names and total price of successful bids they
* made.
*/
private TreeMap> totalBidPricesOfSuccessfulBids;
/**
* Number of agents in the simulation
*/
private int numOfAgents;
/**
* @return singleton instance of the class
*/
public static AgentsStatisticsWriter getInstance() {
return instance;
}
/**
* Open the {@link AgentsStatisticsWriter}.
*
* @param repeat
* current repeat number
* @param numOfAgents
* number of agents in the simulation
*/
public void open(int repeat, int numOfAgents) {
this.repeat = repeat;
this.numOfAgents = numOfAgents;
}
/**
* Private constructor
*/
private AgentsStatisticsWriter() {
numOfAgents = 0;
// Initialise all TreeMaps that are used to store agents' statistics
agentsCE = new TreeMap>();
agentsPM = new TreeMap>();
numberOfBids = new TreeMap>();
numberOfSuccessfulBids = new TreeMap>();
totalOpportunityCost = new TreeMap>();
totalOpportunityCostOfSuccessfulBids = new TreeMap>();
totalBidPrices = new TreeMap>();
totalBidPricesOfSuccessfulBids = new TreeMap>();
}
/**
* Should be called at the end of the repeat to flush everything.
*/
public void flush() {
try {
FileWriter ceWriter = new FileWriter(
ConstantFileNames.getAgentsCEFileName(repeat));
writeAndClose(ceWriter, agentsCE);
FileWriter pmWriter = new FileWriter(
ConstantFileNames.getAgentsPmFile(repeat));
writeAndClose(pmWriter, agentsPM);
FileWriter numOfBidsWriter = new FileWriter(
ConstantFileNames.getNumberOfBidsPerAgent(repeat));
writeAndClose(numOfBidsWriter, numberOfBids);
FileWriter numOfSuccessBidsWriter = new FileWriter(
ConstantFileNames.getNumberOfSuccessfulBidsPerAgent(repeat));
writeAndClose(numOfSuccessBidsWriter, numberOfSuccessfulBids);
FileWriter totalOpportunityCostWriter = new FileWriter(
ConstantFileNames.getTotalOpportunityCostPerAgent(repeat));
writeAndClose(totalOpportunityCostWriter, totalOpportunityCost);
FileWriter successOpportunityCostWriter = new FileWriter(
ConstantFileNames
.getSuccessfulOpportunityCostPerAgent(repeat));
writeAndClose(successOpportunityCostWriter,
totalOpportunityCostOfSuccessfulBids);
FileWriter totalBidPricesWriter = new FileWriter(
ConstantFileNames.getTotalBidPricePerAgent(repeat));
writeAndClose(totalBidPricesWriter, totalBidPrices);
FileWriter successBidPricesWriter = new FileWriter(
ConstantFileNames.getSuccessfulBidPricePerAgent(repeat));
writeAndClose(successBidPricesWriter,
totalBidPricesOfSuccessfulBids);
} catch (IOException e) {
Log.error(e.getMessage());
}
}
/**
* This private method is used by "flush" method to write statistics to
* relevant output csv file
*
* @param writer
* @param values
*/
private void writeAndClose(FileWriter writer,
TreeMap> values) {
try {
// append header
writer.append("cycle_number");
for (int i = 1; i <= numOfAgents; i++) {
writer.append(",");
writer.append("agent" + i);
}
writer.append("\n");
// append values
for (int cycleNumber : values.keySet()) {
TreeMap perAgentValues = values
.get(cycleNumber);
writer.append(Integer.toString(cycleNumber));
for (int i = 1; i <= numOfAgents; i++) {
writer.append(",");
writer.append(Double.toString(perAgentValues.get(Integer
.toString(i))));
}
writer.append("\n");
}
writer.flush();
writer.close();
} catch (IOException e) {
Log.error(e.getMessage());
}
}
/**
* Stores conservation ethic barometers of agents
*
* @param cycle
* current auction cycle
* @param agentName
* agents name
* @param value
* conservation ethic barometer of the agent
*/
public void addAgentsCE(int cycle, String agentName, double value) {
TreeMap storedInfo = agentsCE.get(cycle);
if (storedInfo == null) {
storedInfo = new TreeMap();
}
storedInfo.put(agentName, value);
agentsCE.put(cycle, storedInfo);
}
/**
* Stores profit motive barometers of agents
*
* @param cycle
* current auction cycle
* @param agentName
* agents name
* @param value
* profit motive barometer of the agent
*/
public void addAgentsPM(int cycle, String agentName, double value) {
TreeMap storedInfo = agentsPM.get(cycle);
if (storedInfo == null) {
storedInfo = new TreeMap();
}
storedInfo.put(agentName, value);
agentsPM.put(cycle, storedInfo);
}
/**
* Stores number of bids made by agents
*
* @param cycle
* current auction cycle
* @param agentName
* agents name
* @param value
* number of bids made by the agent
*/
public void addNumberOfBids(int cycle, String agentName, double value) {
TreeMap storedInfo = numberOfBids.get(cycle);
if (storedInfo == null) {
storedInfo = new TreeMap();
}
storedInfo.put(agentName, value);
numberOfBids.put(cycle, storedInfo);
}
/**
* Stores number of successful bids made by agents
*
* @param cycle
* current auction cycle
* @param agentName
* agents name
* @param value
* number of successful bids made by the agent
*/
public void addNumberOfSuccessfulBids(int cycle, String agentName,
double value) {
TreeMap storedInfo = numberOfSuccessfulBids.get(cycle);
if (storedInfo == null) {
storedInfo = new TreeMap();
}
storedInfo.put(agentName, value);
numberOfSuccessfulBids.put(cycle, storedInfo);
}
/**
* Stores total opportunity cost of all bids made by agents
*
* @param cycle
* current auction cycle
* @param agentName
* agents name
* @param value
* total opportunity cost of all bids made by the agent
*/
public void addTotalOpportunityCost(int cycle, String agentName,
double value) {
TreeMap storedInfo = totalOpportunityCost.get(cycle);
if (storedInfo == null) {
storedInfo = new TreeMap();
}
storedInfo.put(agentName, value);
totalOpportunityCost.put(cycle, storedInfo);
}
/**
* Stores total opportunity cost of successful bids made by agents
*
* @param cycle
* current auction cycle
* @param agentName
* agents name
* @param value
* total opportunity cost of successful bids made by the agent
*/
public void addTotalOpportunityCostOfSuccessfulBids(int cycle,
String agentName, double value) {
TreeMap storedInfo = totalOpportunityCostOfSuccessfulBids
.get(cycle);
if (storedInfo == null) {
storedInfo = new TreeMap();
}
storedInfo.put(agentName, value);
totalOpportunityCostOfSuccessfulBids.put(cycle, storedInfo);
}
/**
* Stores total price of all bids made by agents
*
* @param cycle
* current auction cycle
* @param agentName
* agents name
* @param value
* total price of all bids made by the agent
*/
public void addTotalBidPrices(int cycle, String agentName, double value) {
TreeMap storedInfo = totalBidPrices.get(cycle);
if (storedInfo == null) {
storedInfo = new TreeMap();
}
storedInfo.put(agentName, value);
totalBidPrices.put(cycle, storedInfo);
}
/**
* Stores total price of successful bids made by agents
*
* @param cycle
* current auction cycle
* @param agentName
* agents name
* @param value
* total price of successful bids made by the agent
*/
public void addTotalBidPricesOfSuccessfulBids(int cycle, String agentName,
double value) {
TreeMap storedInfo = totalBidPricesOfSuccessfulBids
.get(cycle);
if (storedInfo == null) {
storedInfo = new TreeMap();
}
storedInfo.put(agentName, value);
totalBidPricesOfSuccessfulBids.put(cycle, storedInfo);
}
/**
* Store all statistics of agent for current auction cycle
*
* @param cycle
* current cycle number
* @param agentName
* name of the agent
* @param CE
* agent's CE barometer
* @param PM
* agent's PM barometer
* @param lastAuctionRound
* agents results of current auction cycle
*/
public void addAllInfo(int cycle, String agentName, double CE, double PM,
AuctionRound lastAuctionRound) {
addAgentsCE(cycle, agentName, CE);
addAgentsPM(cycle, agentName, PM);
if (cycle == 0) {
addNumberOfBids(cycle, agentName, 0);
addNumberOfSuccessfulBids(cycle, agentName, 0);
addTotalOpportunityCost(cycle, agentName, 0);
addTotalOpportunityCostOfSuccessfulBids(cycle, agentName, 0);
addTotalBidPrices(cycle, agentName, 0);
addTotalBidPricesOfSuccessfulBids(cycle, agentName, 0);
} else {
addNumberOfBids(cycle, agentName,
lastAuctionRound.getNumberOfBids());
addNumberOfSuccessfulBids(cycle, agentName,
lastAuctionRound.getNumberOfSuccessfulBids());
addTotalOpportunityCost(cycle, agentName,
lastAuctionRound.getTotalOpportunityCostOfAllMyBids());
addTotalOpportunityCostOfSuccessfulBids(cycle, agentName,
lastAuctionRound
.getTotalOpportunityCostOfMySuccessfulBids());
addTotalBidPrices(cycle, agentName,
lastAuctionRound.getTotalPriceOfAllMyBids());
addTotalBidPricesOfSuccessfulBids(cycle, agentName,
lastAuctionRound.getTotalPriceOfMySuccessfulBids());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy