
io.github.agentsoz.conservation.outputwriters.BidsWriter 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.Bid;
import io.github.agentsoz.conservation.Log;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Write information about all bids to an output csv file named
* bids_.csv
*
* @author Sewwandi Perera
*/
public class BidsWriter {
/**
* {@link FileWriter} instance
*/
private FileWriter writer;
/**
* Current auction cycle number
*/
private static int cycleNumber;
/**
* Singleton instance of the class
*/
private static BidsWriter instance = new BidsWriter();
/**
* This is used to lock methods when multiple threads are trying to access
* them at the same time.
*/
private Lock lock = new ReentrantLock();
/**
* @return singleton instance of the class
*/
public static BidsWriter getInstance() {
return instance;
}
/**
* Private constructor of the singleton class
*/
private BidsWriter() {
}
/**
* Initialise the {@link BidsWriter}
*
* @param repeat
* current repeat number
*/
public void init(int repeat) {
try {
cycleNumber = 0;
writer = new FileWriter(ConstantFileNames.getBidsFileName(repeat));
writer.append("agentId,cycle_number,bidNumber,packageId,bidPrice\n");
writer.flush();
} catch (IOException e) {
Log.error(e.getMessage());
}
}
/**
* Write information about a bid to output file
*
* @param agentName
* name of the agent who made the bid
* @param bidNumber
* this number is used to differentiate multiple bids made by the
* same agent
* @param bid
* information about the bid as an instance of {@link Bid}
*/
public void writeBid(String agentName, int bidNumber, Bid bid) {
lock.lock();
try {
writer.append(agentName);
writer.append(",");
writer.append(Integer.toString(cycleNumber));
writer.append(",");
writer.append(Integer.toString(bidNumber));
writer.append(",");
writer.append(Integer.toString(bid.id));
writer.append(",");
writer.append(Double.toString(bid.price));
writer.append("\n");
} catch (IOException e) {
Log.error(e.getMessage());
} finally {
lock.unlock();
}
}
/**
* Set current auction cycle number
*
* @param number
* current cycle number
*/
public void setCycleNumber(int number) {
cycleNumber = number;
}
/**
* Close the writer
*/
public void close() {
try {
writer.close();
} catch (IOException e) {
Log.error(e.getMessage());
}
}
/**
* Write information about a list of bids made by an agent
*
* @param name
* name of the agent
* @param selectedBids
* bids made by the agent
*/
public void writeBids(String name, ArrayList selectedBids) {
int bidNumber = 1;
for (Bid bid : selectedBids) {
writeBid(name, bidNumber, bid);
bidNumber++;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy