001    package calhoun.analysis.crf.solver;
002    
003    import java.util.Arrays;
004    import java.util.List;
005    
006    import org.apache.commons.logging.Log;
007    import org.apache.commons.logging.LogFactory;
008    
009    import calhoun.analysis.crf.CRFTraining;
010    import calhoun.analysis.crf.ModelManager;
011    import calhoun.analysis.crf.io.TrainingSequence;
012    import calhoun.util.Assert;
013    
014    /** a dummy optimizer that just fixed the weights at values specified in the configuration. 
015     * If no weights are specified, then a default all weights being fixed at 1.0 is used. */
016    public class FixedWeightOptimizer implements CRFTraining {
017            private static final Log log = LogFactory.getLog(FixedWeightOptimizer.class);
018    
019            double[] starts;
020            
021            public double[] optimize(ModelManager fm, List<? extends TrainingSequence<?>> data) {
022                    int N = fm.getNumFeatures();
023                    if (starts == null) {
024                            log.warn("FixedWeightOptimizer called but weights not specified; setting all weights to 1.0");
025                            starts = new double[N];
026                            Arrays.fill(starts, 1.0);
027                    }
028                    Assert.a(starts.length == N , "Number of specified starting weights must equal the number of Features");
029                    return starts;
030            }
031    
032            /** gets the fixed weights that will be used in place of an optimization.
033             * @return array of feature weights to use */
034            public double[] getStarts() {
035                    return starts;
036            }
037    
038            /** sets the values to use as feature weights.  A null value sets all weights to 1.0.
039             * @param starts array of feature weights to use */
040            public void setStarts(double[] starts) {
041                    this.starts = starts;
042            }
043    }