Java Reference
In-Depth Information
public
Proposal bestProposal(){
Proposal best
#
super.bestProposal();
if
(resources.valid(best))
return
best;
return
closest(best);
}
// find the closest valid proposal
private
Proposal closest(Proposal proposal){
String[] names
#
new
String[proposal.getAttributes().
size()];
Iterator it
#
proposal.getAttributes().iterator();
Iterator[] values
#
new
Iterator[names.length];
Proposal candidate
#
new
Proposal(proposal);
for
(
int
i
#
0; i<values.length;
!!
i){
names[i]
#
((Attribute)it.next()).getName();
AttributeSpec attribute
#
spec.attribute(names[i]);
Collection admissibles
#
attribute.getValues();
values[i]
#
admissibles.iterator();
candidate.add(names[i],(Value)values[i].next(),
admissibles);
}
double
min
#
2;
double
proposed
#
evaluate(proposal);
Proposal closest
#
null
;
while
(
true
){
if
(resources.valid(candidate)){
double delta
#
Math.abs(evaluate(candidate)
-proposed);
if
(delta<min){
min
#
delta;
closest
#
new
Proposal(candidate);
}
}
if
(!explore(0,names,values,candidate)) break;
}
return
closest;
}
// explores all the possible combinations of values for
// the attributes of the proposal, each invocation
// modifies the proposal
boolean
explore(
int
i,String[] names, Iterator[] values,
Proposal p){
if
(i
##
names.length)
return false
;
Value value;
boolean
nested
#
explore(i
!
1,names,values,p);
if
(nested){
return
nested;