/** * This is a Janet Compose task for a generic branch & bound algorithm. */ package janet.services.tasks; import janet.applications.branchandbound.*; import janet.services.*; import janet.services.shared.*; import java.rmi.*; /** A compositional Task, whose inputs & output are Solution objects, * used in branch & bound applications. */ public final class MinSolution extends Compose { /** This method returns the Solution Object among its inputs, whose IntUpperBound * has minimal intValue, provided that it is at least as low as the Shared * objects intValue. Otherwise, it returns null, indicating that none of its * input Solution objects is the current best. * @param environment The environment's Shared Object defines the currently best known * minimal intValue associated with a Solution. It is possible, * though unlikely, that the Task's input Solutions contain a better * one than the TaskServer knows about, as reflected in the * Environment's Shared object's intValue. If this occurs, a side-effect * of the execute method is to update the Environment's Shared object accordingly. * @return the Solution Object among its inputs, whose IntUpperBound * has minimal intValue, provided that it is at least as low as the Shared * objects intValue. Otherwise, it returns null, indicating that none of its * input Solution objects is the current best. */ public Object execute ( Environment environment ) { Solution currentBest = null; for ( int i = 0; i < numInputs(); i++ ) { Solution reportedSolution = (Solution) getInput( i ); if ( reportedSolution == null ) { continue; // subTask: "No better solution found." } int lowerBound = reportedSolution.getLowerBound( environment ); /** The = of <= is needed to return the shared upper bound solution * which occurs when that solution is among this task's inputs. */ if ( lowerBound <= sharedUpperBoundValue( environment ) ) { environment.setShared( new IntUpperBound( lowerBound ) ); currentBest = reportedSolution; } } return currentBest; } // return int value of shared IntUpperBound private int sharedUpperBoundValue( Environment environment ) { return ( (Integer) environment.getShared().get() ).intValue(); } }