Exemple #1
0
        public OptimizationProblem CreateProblem()
        {
            // create an empty mapping of curves to annotations, as we have no annotations
            var curvesToAnnotations = new Dictionary <FeatureCurve, ISet <Annotation> >();

            foreach (var fc in sessionData.FeatureCurves)
            {
                curvesToAnnotations[fc] = new HashSet <Annotation>();
            }

            // now we are going to transform the objective and the constraints
            var primitiveWriter = primitivesReaderWriterFactory.CreateWriter();

            primitiveWriter.Write(snappedPrimitive);

            // now we construct all we need for the optimization problem
            var variables                = primitiveWriter.GetVariables();
            var initialValues            = primitiveWriter.GetValues();
            var objectiveWithConstraints = primitiveReconstructor.Reconstruct(snappedPrimitive, curvesToAnnotations);

            return(new OptimizationProblem
            {
                Objective = objectiveWithConstraints.Item1,
                Constraints = objectiveWithConstraints.Item2,
                Variables = variables,
                InitialValue = initialValues,
            });
        }
Exemple #2
0
        private void DoUpdate()
        {
            if (optimizationTask != null)
            {
                shouldOptimizeAgain = true;
                return;
            }

            oldSnappedPrimitive = snappedPrimitive;
            snappedPrimitive    = snappersManager.Create(newPrimitive);
            snappedPrimitive.UpdateFeatureCurves();

            var emptyCurvesToAnnotations = new Dictionary <FeatureCurve, ISet <Annotation> >();

            var objectivesAndConstraints = snappersManager.Reconstruct(snappedPrimitive, emptyCurvesToAnnotations);
            var objective   = objectivesAndConstraints.Item1;
            var constraints = objectivesAndConstraints.Item2;

            var primitivesWriter = primitivesReaderWriterFactory.CreateWriter();

            primitivesWriter.Write(snappedPrimitive);

            var vars = primitivesWriter.GetVariables();
            var vals = primitivesWriter.GetValues();

            optimizationTask = Task.Factory.StartNew <double[]>(
                _ => optimizer.Minimize(objective, constraints, vars, vals).Last(), TaskScheduler.Default)
                               .ContinueWith(task =>
            {
                if (disposed)
                {
                    return;
                }

                var optimum = task.Result;

                // update primitives from the optimal values
                primitivesReaderWriterFactory.CreateReader().Read(optimum, snappedPrimitive);
                snappedPrimitive.UpdateFeatureCurves();

                // update the task managment fields.
                sessionData.SnappedPrimitives.Remove(oldSnappedPrimitive);
                optimizationTask = null;
                if (shouldOptimizeAgain)
                {
                    shouldOptimizeAgain = false;
                    DoUpdate();
                }
                else
                {
                    sessionData.SnappedPrimitives.Add(snappedPrimitive);
                    eventAggregator.GetEvent <SnapCompleteEvent>().Publish(null);
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }
        public OptimizationProblem CreateProblem()
        {
            var curvesToAnnotations = GetCurvesToAnnotationsMapping();

            // get objectives and constraints for primitives
            var constraints = new List <Term>();
            var objectives  = new List <Term>();

            foreach (var snappedPrimitive in sessionData.SnappedPrimitives)
            {
                var objectiveAndConstraints = snappersManager.Reconstruct(snappedPrimitive, curvesToAnnotations);
                objectives.Add(objectiveAndConstraints.Item1);
                constraints.AddRange(objectiveAndConstraints.Item2);
            }

            // add constraints extracted from the annotations
            var annotationConstraints = from annotation in sessionData.Annotations
                                        from constraint in annotationConstraintsExtractor.GetConstraints(annotation)
                                        select constraint;

            constraints.AddRange(annotationConstraints);

            // perform the optimization.
            var primitivesWriter = primitivesReaderWriterFactory.CreateWriter();

            primitivesWriter.Write(sessionData.SnappedPrimitives);

            var variables = primitivesWriter.GetVariables();
            var values    = primitivesWriter.GetValues();

            var finalObjective = TermUtils.SafeSum(objectives);

            return(new OptimizationProblem
            {
                Objective = finalObjective,
                Constraints = constraints.ToArray(),
                Variables = variables,
                InitialValue = values,
            });
        }