001    package edu.nrao.sss.model.project.scheduling.constraint;
002    
003    import java.util.Set;
004    import java.util.List;
005    import java.util.HashSet;
006    
007    import javax.xml.bind.annotation.XmlRootElement;
008    
009    /**
010     * Computes a weighted average of the operation constraints as the probability
011     * that a scheduling block will be successful in realizing its scientific goals.
012     * @author slovelan
013     */
014    @XmlRootElement
015    public class ConstraintMeasureSum {
016            
017            private Set<Constraint> operatingConstraints = new HashSet<Constraint>();
018            private Float weight = null;
019            
020            public ConstraintMeasureSum (){
021                    operatingConstraints.add( new ConstraintWind());
022                    operatingConstraints.add( new ConstraintApi());
023                    operatingConstraints.add( new ConstraintPositionElevation());
024                    operatingConstraints.add( new ConstraintPositionMaximum());
025            }
026            
027            public ConstraintMeasureSum( ConstraintMeasureSum copy ){
028                    Set<Constraint> constraints = copy.getConstraints();
029                    for ( Constraint c : constraints ){
030                            operatingConstraints.add( c );
031                    }
032            }
033            
034            
035            public void clearOperatingConstraints(){
036                    operatingConstraints.clear();
037            }
038    
039            
040            public Set<Constraint> getConstraints(){
041                    Set<Constraint> copy = new HashSet<Constraint>();
042                    for ( Constraint c : operatingConstraints ){
043                            copy.add( ConstraintFactory.copy( c ));
044                    }
045                    return copy;
046            }
047            
048    
049            
050            /**
051             * Sets a new weight for this type of constraint.
052             * @param newWeight a float indicating the importance of this particular constraint.
053             */
054            
055            public void setWeight( float newWeight ){
056                    if ( newWeight >= 0 ){
057                            this.weight = newWeight;
058                    }
059                    else {
060                            throw new IllegalArgumentException( "A constraint cannot have a negative weight: "+newWeight );
061                    }
062            }
063            
064            /**
065             * Returns a new weight for this type of constraint.
066             * @return a float indicating the importance of this particular constraint compared to
067             *         others.
068             */
069    
070            public float getWeight(){
071                    return weight;
072            }
073            
074            public double getProbabilitySuccess( List<Constraint> sbConstraints ){
075                    //If there are no constraints then we should return the highest success probability possible.
076                    double sum = ConstraintProbability.SUCCESS;
077                    if ( sbConstraints != null && sbConstraints.size() > 0 ){
078                            sum = 0;
079                            float weightSum = 0;
080                            for ( Constraint oc : operatingConstraints ){
081                                    weightSum = weightSum + weight;
082                                    ConstraintProbability successProb = oc.getProbabilitySuccess( sbConstraints );
083                                    sum = sum + successProb.getProbabilitySuccess() * weight;
084                            }
085                            if ( weightSum != 0 ){
086                                    sum = sum / weightSum;
087                            }
088                    }
089                    return sum;
090            }
091            
092            
093    }