Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package jadex.bdi.planlib.protocols;
  
  
  
  import java.util.List;

A sample implementation of the IPriceCalculator. Calculates the price for the next round of an english auction.
 
 public class ExponentialPriceCalculator implements IOfferGenerator
 {
 	//-------- attributes --------
 
The negotiation round.
 
 	protected int round;

The start price.
 
 	protected double startprice;

The limit price.
 
 	protected double limitprice;

The prices.
 
 	protected List prices;

The variation.
 
 	protected double variation;

The base (base^round).
 
 	protected double base;

The minimum step width.
 
 	protected double minstepwidth;

Flag indicating if increasing or decreasing prices are calculated.
 
 	protected boolean increasing;
 
 	//-------- constructors --------
 
Create a new exponential price calculator.

Parameters:
startprice The start price.
limitprice The limit price (min or max) for a auction.
base The base for the exponential function.
 
 	public ExponentialPriceCalculator(double startpricedouble limitpricedouble base)
 	{
 		this(startpricelimitpricebase, 0.0, 0.0);
 	}

Create a new exponential price calculator.

Parameters:
startprice the start price
limitprice the minimal price for a successful auction
base The base for the exponential function.
variation The variation in percentage (0->1) for the increment. The increment will be used to adapt the increment by multiplying it with (+/-) a random value that has maximal the variation percentage influence, e.g. setting 0.1 will cause the increment being maximally +/-10% changed.
minstepwidth The minimum step width (a positive value that describes the minimum price change in each round).
 
 	public ExponentialPriceCalculator(double startpricedouble limitprice,
 		double basedouble variationdouble minstepwidth)
 	{
 		this. = startprice;
 		this. = SCollection.createArrayList();
 		this..add(new Double(startprice));
 		this. = limitprice;
 		this. = base;
 		this. = variation;
 		this. = minstepwidth;
 		if(minstepwidth<0)
 			throw new IllegalArgumentException("Step width need to be > 0.");
 		
 		this. = 1;
 		this. = limitprice > startprice;
 	}
 	
 	//-------- methods --------
 
Get the price for the current round of this auction.

Returns:
The price.
 
 	{
 		double ret;
 		if(.size() == getRound())
 		{
 			ret = ((Double).get(getRound()-1)).doubleValue();
 		}
 		else
 		{
 			// Exponential growth: price(round) = price(0)*(rate^(+/-)round + var)
 			
			double var = (Math.random()-0.5)*2*;
				ret = (Math.pow()+var)*;
			else
				ret = (Math.pow(, -)+var)*
			double lastprice = ((Double).get(getRound()-2)).doubleValue();
			// Ensure monotony
			if( && ret<lastprice)
				ret = lastprice+;
			else if(! && ret>lastprice)
				ret = lastprice-;
			// Ensure that price is not out of bounds
			if(( && ret>) || (! && ret<))
				ret = ;
			.add(new Double(ret));
		}
		return new Double(ret);
	}

Returns the offer for the last round.

Returns:
The last offer.
	{
		return .size()>1? (Comparable).get(.size()-2): null;
	}

Get the min price.

Returns:
The minprice.
	{
		return new Double();
	}

Get the start price.

Returns:
The startprice
	{
		return new Double();
	}

Get the round.

Returns:
The round.
	public int getRound()
	{
		return ;
	}

Increase the round.
	public void setNextRound()
	{
	}

Main for testing.
	public static void main(String[] args)
	{
		IOfferGenerator pc = new ExponentialPriceCalculator(100, 10, 1.1, 0.1, 1.0);
		for(int i=0; i<20; i++)
		{
			..println("Price "+i+": "+pc.getCurrentOffer());
				..println("Limit reached");
		}
		pc = new ExponentialPriceCalculator(100, 500, 1.1, 0.1, 1.0);
		for(int i=0; i<20; i++)
		{
			..println("Price "+i+": "+pc.getCurrentOffer());
				..println("Limit reached");
		}
	}
New to GrepCode? Check out our FAQ X