Exemplo n.º 1
0
        public virtual int[] FindBestUsingAnnealing(ISequenceModel model, CoolingSchedule schedule, int[] initialSequence)
        {
            if (verbose > 0)
            {
                log.Info("Doing annealing");
            }
            listener.SetInitialSequence(initialSequence);
            IList result = new ArrayList();

            // so we don't change the initial, or the one we just stored
            int[]  sequence  = Copy(initialSequence);
            int[]  best      = null;
            double bestScore = double.NegativeInfinity;
            double score     = double.NegativeInfinity;
            // if (!returnLastFoundSequence) {
            //   score = model.scoreOf(sequence);
            // }
            ICollection <int> positionsChanged = null;

            if (speedUpThreshold > 0)
            {
                positionsChanged = Generics.NewHashSet();
            }
            for (int i = 0; i < schedule.NumIterations(); i++)
            {
                if (Thread.Interrupted())
                {
                    // Allow interrupting the parser
                    throw new RuntimeInterruptedException();
                }
                double temperature = schedule.GetTemperature(i);
                if (speedUpThreshold <= 0)
                {
                    score = SampleSequenceForward(model, sequence, temperature, null);
                }
                else
                {
                    // modifies tagSequence
                    if (i < speedUpThreshold)
                    {
                        score = SampleSequenceForward(model, sequence, temperature, null);
                        // modifies tagSequence
                        for (int j = 0; j < sequence.Length; j++)
                        {
                            if (sequence[j] != initialSequence[j])
                            {
                                positionsChanged.Add(j);
                            }
                        }
                    }
                    else
                    {
                        score = SampleSequenceForward(model, sequence, temperature, positionsChanged);
                    }
                }
                // modifies tagSequence
                result.Add(sequence);
                if (returnLastFoundSequence)
                {
                    best = sequence;
                }
                else
                {
                    // score = model.scoreOf(sequence);
                    //log.info(i+" "+score+" "+Arrays.toString(sequence));
                    if (score > bestScore)
                    {
                        best      = sequence;
                        bestScore = score;
                    }
                }
                if (i % 50 == 0)
                {
                    if (verbose > 1)
                    {
                        log.Info("itr " + i + ": " + bestScore + "\t");
                    }
                }
                if (verbose > 0)
                {
                    log.Info(".");
                }
            }
            if (verbose > 1)
            {
                log.Info();
                PrintSamples(result, System.Console.Error);
            }
            if (verbose > 0)
            {
                log.Info("done.");
            }
            //return sequence;
            return(best);
        }
Exemplo n.º 2
0
 public virtual int[] FindBestUsingAnnealing(ISequenceModel model, CoolingSchedule schedule)
 {
     int[] initialSequence = GetRandomSequence(model);
     return(FindBestUsingAnnealing(model, schedule, initialSequence));
 }