Start line:  
End line:  

Snippet Preview

Snippet HTML Code

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

A default implementation of the proposal evaluator interface. The implementation determines acceptable proposals by comparing proposals or evaluations to a given limit value.

The evaluation process implemented in the evaluateProposals() method is distributed across three methods, which can be separately overwritten if needed, while reusing functionality of the other methods.

  1. The proposals are evaluated by calling the the evaluateProposal() method for each of the proposals. The evaluation result is written back into the proposal. The default implementation just checks, if the proposal object itself is suitable as an evaluation (i.e. if it is comparable).
  2. For each of the proposals, the acceptability is determined. By default, the given string constants are interpreted or, if a limit value is given, the proposal evaluations are compared to the limit value.
  3. Finally, the acceptable proposals are ordered by preference. As a default, the proposals are compared to each other and sorted according to the given ordering.
 
 public class ProposalEvaluator implements IProposalEvaluator
 {
 	//-------- constants --------
 	
Evaluation value indicating an inacceptable proposal that should be excluded.
 
 	public static final String	EVALUATION_INACCEPTABLE"evaluation-inacceptable";

Evaluation value indicating an acceptable proposal that should be considered in further negotiation rounds.
 
 	public static final String	EVALUATION_ACCEPTABLE"evaluation-acceptable";
 	
 	//-------- attributes --------
 	
A comparator used for comparing evaluations.
 
Limit determining the acceptability of an evaluation.
 
 	protected Object	evaluation_limit;

Flag indicating if evaluations are rated ascending (the higher the better) or the other way round.
 
 	protected boolean	ascending;
 
 	//-------- constructors --------
 	
Create a default proposal evaluator. This (empty) constructor cannot be used directly, as it requires at least the isProposalAcceptable() method to be overwritten.
 
 	protected ProposalEvaluator() {}

Create a default proposal evaluator with a given limit value. This constructor can be used without overwriting any methods, if the proposal objects are comparable to each other and the limit value. Otherwise, the evaluateProposal() method should be overwritten to provide comparable evaluation values for the proposal objects.

Parameters:
evaluation_limit The limit specifying which proposals are acceptable.
ascending Sort order, which specifies that all evaluations below or equal (true) or above or equal (false) to the limit are acceptable.
 
 	public ProposalEvaluator(Object evaluation_limitboolean ascending)
 	{
 		this.evaluation_limit;
 		this.ascending;
 	}

Create a default proposal evaluator with a given limit value. This constructor can be used without overwriting any methods, if the proposal objects are comparable to each other and the limit value using the given comparator. Otherwise, the evaluateProposal() method should be overwritten to provide comparable evaluation values for the proposal objects.

Parameters:
evaluation_comparator A comparator used to compare proposal evaluations.
evaluation_limit The limit specifying which proposals are acceptable.
ascending Sort order, which specifies that all evaluations below or equal (true) or above or equal (false) to the limit are acceptable.
 
 	public ProposalEvaluator(Comparator evaluation_comparatorObject evaluation_limitboolean ascending)
 	{
 		this.evaluation_comparator;
 		this.evaluation_limit;
 		this.ascending;
 	}
 
 	//-------- IProposalEvaluator interface --------
Evaluate the given proposals and determine winning proposals.

Parameters:
cfp The original call-for-proposal object.
cfp_info Local meta information associated to the interaction.
history The history of negotiation rounds.
proposals The received proposals.
Returns:
The winners among the proposals.
	public ParticipantProposal[] evaluateProposals(Object cfpObject cfp_infoNegotiationRecord[] historyParticipantProposal[] proposals)
	{
		// Determine evaluations for each of the proposals.
		for(int i=0; i<proposals.lengthi++)
		{
			proposals[i].setEvaluation(evaluateProposal(cfpcfp_infohistoryproposals[i]));
		}
		// Determine acceptable proposals.
		List	acceptablesnew ArrayList();
		for(int i=0; i<proposals.lengthi++)
		{
			if(isProposalAcceptable(cfpcfp_infohistoryproposals[i]))
			{
				acceptables.add(proposals[i]);
			}
		}
		// Order acceptable proposals by preference.
		ParticipantProposal[]	orderedorderAcceptables(cfpcfp_infohistory,
			(ParticipantProposal[])acceptables.toArray(new ParticipantProposal[acceptables.size()]));
		return ordered;
	}
	//-------- template methods --------
Evaluate the given proposal. An implementation may use the defined constants for specifying evaluation results, but custom evaluation values are also allowed. This default implementation justs uses the proposal object itself as its evaluation, as long as it is comparable in itself or with a given comparator.

Parameters:
cfp The original call-for-proposal object.
cfp_info Local meta information associated to the interaction.
history The history of negotiation rounds.
proposal A received proposal.
Returns:
The proposal evaluation.
	protected Object	evaluateProposal(Object cfpObject cfp_infoNegotiationRecord[] historyParticipantProposal proposal)
	{
		Object	retnull;
		{
			retproposal.getProposal();
		}
		return ret;
	}

Check if a proposal is acceptable. This default implementation checks for one of the evaluation constants and otherwise tries to use the given evaluation limit (if any) and compares it to the proposals evaluation value. Proposals without evaluation or which are not comparable are deemed inacceptable by default.

Parameters:
cfp The original call-for-proposal object.
cfp_info Local meta information associated to the interaction.
history The history of negotiation rounds.
proposal A received proposal.
Returns:
The proposal evaluation.
	protected boolean	isProposalAcceptable(Object cfpObject cfp_infoNegotiationRecord[] historyParticipantProposal proposal)
	{
		boolean	ret;
		// Acceptability aready set in proposal.
		{
		}
		// Use limit to determine acceptability
		else if(proposal.getEvaluation()!=null && !=null)
		{
			int	eval;
			{
			}
			else
			{
			}
			ret ? eval<=0 : eval>=0;
		}
		else
		{
			retfalse;
		}
		return ret;
	}

Order acceptable proposals by preference. This default implementation tries to compare the proposal evaluations directly or using the given comparator. If some proposal evaluations are not comparable, these are returned in the original order (after comparable proposals, if any).

Parameters:
cfp The original call-for-proposal object.
cfp_info Local meta information associated to the interaction.
history The history of negotiation rounds.
proposals The acceptable proposals.
Returns:
The ordered acceptable proposals.
	protected ParticipantProposal[]	orderAcceptables(Object cfpObject cfp_infoNegotiationRecord[] historyParticipantProposal[] proposals)
	{
		List comparablesnew ArrayList();
		List uncomparablesnew ArrayList();
		for(int i=0; i<proposals.lengthi++)
		{
			if(isValueComparable(proposals[i].getEvaluation()))
			{
				comparables.add(proposals[i]);
			}
			else
			{
				uncomparables.add(proposals[i]);
			}
		}
		Collections.sort(comparablesnew Comparator()
		{
			public int compare(Object o1Object o2)
			{
				int	ret;
				{
				}
				else
				{
					ret	= ((Comparable)prop1.getEvaluation()).compareTo(prop2.getEvaluation());
				}
				return  ? ret : -ret;
			}
		});
		comparables.addAll(uncomparables);
		return (ParticipantProposal[])comparables.toArray(new ParticipantProposal[comparables.size()]);
	}
	//-------- helper methods --------
Test if a value is comparable.
	private boolean isValueComparable(Object value)
	{
		boolean	ret;
		{
			try
			{
				rettrue;
			}
			catch(Exception e)
			{
				retfalse;
			}
		}
		else
		{
			retvalue instanceof Comparable;
		}
		return ret;
	}
New to GrepCode? Check out our FAQ X