📄 reverseenglishauction.java
字号:
/*
* Title: GridSim Toolkit
* Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation
* of Parallel and Distributed Systems such as Clusters and Grids
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
*
* $Id: ReverseEnglishAuction.java,v 1.4 2006/03/23 03:40:57 anthony Exp $
*/
package gridsim.auction;
import java.util.LinkedList;
import eduni.simjava.Sim_port;
/**
* This class represents a Reverse English Auction.
* In a reverse auction, buyers start the auction and the
* lowest bid is considered the best. Therefore, the
* English auction becomes descending and starts with
* the max price going until the min price.
*
* @author Marcos Dias de Assuncao
* @since GridSim Toolkit 4.0
* @see gridsim.auction.Auction
* @see gridsim.auction.OneSidedAuction
* @see gridsim.auction.AuctionTags
*/
public class ReverseEnglishAuction extends OneSidedAuction {
private MessageBid bestBid;
private boolean shouldDecrease = false;
private Object syncIncr = new Object();
private LinkedList initialBidders;
/**
* Constructor
* @param auctionName a name for the auction
* @param auctioneerID the GridSim id of the auctioneer
* @param durationOfRounds simulation time of the duration of each round
* @param totalRound the number of rounds
* @param output the auctioneer's output port
* @throws Exception
* @see gridsim.GridSim
*/
public ReverseEnglishAuction(String auctionName, int auctioneerID,
double durationOfRounds,int totalRound, Sim_port output) throws Exception {
super(auctionName, auctioneerID, AuctionTags.REVERSE_ENGLISH_AUCTION,
durationOfRounds, totalRound, output);
}
/**
* Constructor
* @param auctionName a name for the auction
* @param durationOfRounds simulation time of the duration of each round
* @param totalRound the number of rounds
* @throws Exception
* @see gridsim.GridSim
*/
public ReverseEnglishAuction(String auctionName,
double durationOfRounds,int totalRound) throws Exception {
super(auctionName, AuctionTags.REVERSE_ENGLISH_AUCTION,
durationOfRounds, totalRound);
}
/**
* This method is called when a round is started
* @see gridsim.auction.OneSidedAuction#onStart(int)
*/
public void onStart(int round){
if(round == 1){
initialBidders = super.getBidders();
super.setCurrentPrice(super.getMaxPrice());
}
MessageCallForBids msg = new MessageCallForBids(super.getAuctionID(),
super.getAuctionProtocol(),
super.getCurrentPrice(), super.currentRound());
msg.setAttributes(super.getAttributes());
super.broadcastMessage(msg);
}
/**
* This method is invoked when a round finishes
* @see gridsim.auction.OneSidedAuction#onClose(int)
*/
public void onClose(int round){
boolean stop = false;
synchronized (syncIncr) {
if (!shouldDecrease || round == super.getNumberOfRounds()) {
stop = true;
} else {
shouldDecrease = false;
double decrease = ((super.getMaxPrice() - super.getMinPrice())
/ (super.getNumberOfRounds() - 1));
super.setCurrentPrice(super.getCurrentPrice() - decrease);
}
if (stop) {
if (bestBid != null) {
double price = bestBid.getPrice();
if (price <= super.getReservePrice()) {
super.setFinalPrice(price);
super.setWinner(bestBid.getBidder());
} else {
super.setFinalPrice(super.getCurrentPrice());
}
} else {
super.setFinalPrice(super.getCurrentPrice());
}
closeAuction();
}
}
}
/**
* This method is called when the auction finishes
* @see gridsim.auction.OneSidedAuction#onStop()
*/
public void onStop(){
int winner = super.getWinner();
MessageInformOutcome iout = new MessageInformOutcome(super
.getAuctionID(), super.getAuctionProtocol(), winner, super
.getFinalPrice());
super.setBidders(initialBidders);
iout.setAttributes(super.getAttributes());
super.broadcastMessage(iout);
}
/**
* This method is called when a bid is received.
* @see gridsim.auction.OneSidedAuction#onReceiveBid(gridsim.auction.MessageBid)
*/
public void onReceiveBid(MessageBid bid){
if (bid.getRound() == super.currentRound()) {
if (bestBid == null) {
bestBid = bid;
synchronized (syncIncr) {
shouldDecrease = true;
}
} else {
if (bestBid.getRound() < bid.getRound()) {
bestBid = bid;
synchronized (syncIncr) {
shouldDecrease = true;
}
}
}
}
}
/**
* Called when a reject bid is received.
* @see gridsim.auction.OneSidedAuction#onReceiveRejectCallForBid(gridsim.auction.MessageRejectCallForBid)
*/
public void onReceiveRejectCallForBid(MessageRejectCallForBid mrej){
// remove bidder from the list. So, next call for bids will not be sent to this buyer
Integer bidder = new Integer(mrej.getSourceID());
LinkedList bidders = super.getBidders();
if(bidders.contains(bidder))
bidders.remove(bidder);
super.setBidders(bidders);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -