Пример #1
0
 /// <summary>
 /// Construct the object, weighted problems.
 /// </summary>
 /// <param name="optimizer">Optimize to be used.</param>
 /// <param name="weightedProblems">Array of weighted problems to be optimized.</param>
 /// <param name="numRuns">Number of optimization runs per problem.</param>
 /// <param name="maxIterations">Max number of optimization iterations.</param>
 public MetaFitness(Optimizer optimizer, WeightedProblem[] weightedProblems, int numRuns, int maxIterations)
     : base(maxIterations)
 {
     Optimizer = optimizer;
     NumRuns = numRuns;
     ProblemIndex = new ProblemIndex(weightedProblems);
 }
Пример #2
0
 private void command_BeforeExecute(Command source, Notation notation, Optimizer optimizer, QueryContext context)
 {
     this.context = context;
     Notation.Record[] recs = notation.Select(Descriptor.Root, 1);
     if (recs.Length > 0)
     {
         Notation.Record[] recsd = notation.Select(recs[0].Arg0, Descriptor.Binding, 1);
         if (recsd.Length > 0)
             throw new ESQLException("Query parameters is not supported in XQueryConsole", null);
     }
     if (context.UseSampleData)
     {
         String path = Path.Combine(
             Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Data");
         if (Directory.Exists(path))
             context.DatabaseDictionary.SearchPath = path;                
     }
 }
Пример #3
0
 public void ShouldThrowExceptionWhenFileNameIsEmpty()
 {
     Assert.Throws <ArgumentException>("fileName", () => Optimizer.Compress(string.Empty));
 }
Пример #4
0
        private static double evaluate(TorchTensor eval_data, TransformerModel model, Loss criterion, double lr, int bptt, int ntokens, Optimizer optimizer)
        {
            model.Eval();

            var total_loss = 0.0f;
            var src_mask   = model.GenerateSquareSubsequentMask(bptt);
            var batch      = 0;

            for (int i = 0; i < eval_data.shape[0] - 1; batch++, i += bptt)
            {
                var(data, targets) = GetBatch(eval_data, i, bptt);
                if (data.shape[0] != bptt)
                {
                    src_mask.Dispose();
                    src_mask = model.GenerateSquareSubsequentMask(data.shape[0]);
                }
                var output = model.forward(data, src_mask);
                var loss   = criterion(output.view(-1, ntokens), targets);
                total_loss += data.shape[0] * loss.to(Device.CPU).DataItem <float>();

                data.Dispose();
                targets.Dispose();

                GC.Collect();
            }

            return(total_loss / eval_data.shape[0]);
        }
Пример #5
0
        private Expression ReduceCore()
        {
            //
            // First, check to see whether await expressions occur in forbidden places such as the body
            // of a lock statement or a filter of a catch clause. This is done using a C# visitor so we
            // can analyze the original (non-reduced) intent of the nodes.
            //
            AwaitChecker.Check(Body);

            const int ExprCount = 1 /* new builder */ + 1 /* new state machine */ + 1 /* initial state */ + 1 /* start state machine */;

            //
            // Next, analyze what kind of async method builder we need by analyzing the return type of
            // the async lambda. Based on this we will determine how many expressions we need in the
            // rewritten top-level async method which sets up the builder, state machine, and kicks off
            // the async logic.
            //
            var invokeMethod = typeof(TDelegate).GetMethod("Invoke");
            var returnType   = invokeMethod.ReturnType;

            var builderType = default(Type);
            var exprs       = default(Expression[]);

            if (returnType == typeof(void))
            {
                builderType = typeof(AsyncVoidMethodBuilder);
                exprs       = new Expression[ExprCount];
            }
            else if (returnType == typeof(Task))
            {
                builderType = typeof(AsyncTaskMethodBuilder);
                exprs       = new Expression[ExprCount + 1];
            }
            else
            {
                Debug.Assert(returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task <>));
                builderType = typeof(AsyncTaskMethodBuilder <>).MakeGenericType(returnType.GetGenericArguments()[0]);
                exprs       = new Expression[ExprCount + 1];
            }

            var i = 0;

            var builderVar      = Expression.Parameter(builderType, "__builder");
            var stateMachineVar = Expression.Parameter(typeof(RuntimeAsyncStateMachine), "__statemachine");
            var stateVar        = Expression.Parameter(typeof(int), "__state");

            //
            // __builder = ATMB.Create();
            //
            var builderCreateMethod = builderType.GetMethod("Create", BindingFlags.Public | BindingFlags.Static);
            var createBuilder       = Expression.Assign(builderVar, Expression.Call(builderCreateMethod));

            exprs[i++] = createBuilder;

            //
            // This is where we rewrite the body of the async lambda into an Action delegate we can pass
            // to the runtime async state machine. The collection of variables returned by the rewrite
            // step will contain all the variables that need to be hoisted to the heap. We do this by
            // simply declaring them in the scope around the rewritten (synchronous) body lambda, thus
            // causing the lambda compiler to create a closure.
            //
            // Note we could take the rewritten body and the variables collection and construct a struct
            // implementing IAsyncStateMachine here. However, we don't readily have access to a TypeBuilder
            // for the dynamically generated method created by the lambda compiler higher up, so we'd
            // have to go through some hoops here. For now, we'll rely on a delegate with a closure that
            // consists of a set of StrongBox objects and gets passed to a RuntimeAsyncStateMachine. We
            // can decide to optimize this using a struct later (but then it may be worth making closures
            // in the lambda compiler a bit cheaper by creating a display class as well).
            //
            var body = RewriteBody(stateVar, builderVar, stateMachineVar, out IEnumerable <ParameterExpression> variables);

            //
            // __statemachine = new RuntimeAsyncStateMachine(body);
            //
            var stateMachineCtor   = stateMachineVar.Type.GetConstructor(new[] { typeof(Action) });
            var createStateMachine = Expression.Assign(stateMachineVar, Expression.New(stateMachineCtor, body));

            exprs[i++] = createStateMachine;

            //
            // __state = -1;
            //
            exprs[i++] = Expression.Assign(stateVar, Helpers.CreateConstantInt32(-1));

            //
            // __builder.Start(ref __statemachine);
            //
            var startMethod = builderType.GetMethod("Start", BindingFlags.Public | BindingFlags.Instance);

            exprs[i++] = Expression.Call(builderVar, startMethod.MakeGenericMethod(typeof(RuntimeAsyncStateMachine)), stateMachineVar);

            //
            // return __builder.Task;
            //
            if (returnType != typeof(void))
            {
                exprs[i] = Expression.Property(builderVar, "Task");
            }

            //
            // The body of the top-level reduced method contains the setup and kick off of the state
            // machine. Note the variables returned from the rewrite step of the body are included here
            // in order to hoist them to the heap.
            //

            // REVIEW: Should we ensure all hoisted variables get assigned default values? This could
            //         matter if the resulting lambda gets inlined in an invocation expression and is
            //         invoked repeatedly, causing locals to be reused. Or should we assume definite
            //         assignment here (or enforce it)?
            var rewritten = Expression.Block(new[] { builderVar, stateMachineVar, stateVar }.Concat(variables), exprs);

            //
            // Finally, run a fairly trivial optimizer to reduce excessively nested blocks that were
            // introduced by the rewrite steps above. This is strictly optional and we could consider
            // an optimization flag of LambdaExpression and AsyncLambdaCSharpExpression that's more
            // generally useful (see ExpressionOptimizationExtensions for an early sketch of some of
            // these optimizations).
            //
            var optimized = Optimizer.Optimize(rewritten);

            //
            // The result is a synchronous lambda that kicks off the async state machine and returns the
            // resulting task (if non-void-returning).
            //
            var res = Expression.Lambda <TDelegate>(optimized, Parameters);

            return(res);
        }
Пример #6
0
        public int PrepareToFit(FloatTensor input, FloatTensor target, Loss.Loss criterion, Optimizer optimizer, int batch_size)
        {
            if (input.Shape[0] != target.Shape[0])
            {
                throw new InvalidDataException("Input and Target tensors don't seem to have the right dims");
            }

            _input_tensor_origin  = input;
            _target_tensor_origin = target;

            int[] input_buffer_shape = new int[input.Shape.Length];
            input_buffer_shape[0] = batch_size;
            for (int i = 1; i < input.Shape.Length; i++)
            {
                input_buffer_shape[i] = input.Shape[i];
            }

            input_buffer = controller.floatTensorFactory.Create(_shape: input_buffer_shape, _autograd: true);
            int[] target_buffer_shape = new int[target.Shape.Length];
            target_buffer_shape[0] = batch_size;
            for (int i = 1; i < target.Shape.Length; i++)
            {
                target_buffer_shape[i] = target.Shape[i];
            }

            target_buffer = controller.floatTensorFactory.Create(_shape: target_buffer_shape, _autograd: true);

            this._batch_size = batch_size;
            this._criterion  = criterion;
            this._optimizer  = optimizer;

            this._input_batch_offset = batch_size;
            for (int i = 1; i < input.Shape.Length; i++)
            {
                this._input_batch_offset *= input.Shape[i];
            }

            this._target_batch_offset = batch_size;
            for (int i = 1; i < target.Shape.Length; i++)
            {
                this._target_batch_offset *= target.Shape[i];
            }

            return((int)(input.Shape[0] / batch_size));
        }
Пример #7
0
        /*
         * URL : https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.Ldflda(v=vs.110).aspx
         * Description : Finds the address of a field in the object whose reference is currently on the evaluation stack.
         */
        internal override void Execute(Options Config, OpCodeType xOp, MethodBase method, Optimizer Optimizer)
        {
            if (Optimizer.vStack.Count < 1)
            {
                throw new Exception("Internal Compiler Error: vStack.Count < 1");
            }

            var field  = ((OpField)xOp).Value;
            var offset = Helper.GetFieldOffset(field.DeclaringType, field, Config.TargetPlatform);

            /* The stack transitional behavior, in sequential order, is:
             * An object reference (or pointer) is pushed onto the stack.
             * The object reference (or pointer) is popped from the stack; the address of the specified field in the object is found.
             * The address of the specified field is pushed onto the stack.
             */

            var item = Optimizer.vStack.Pop();

            switch (Config.TargetPlatform)
            {
            case Architecture.x86:
            {
                if (!item.SystemStack)
                {
                    throw new Exception(string.Format("UnImplemented-RegisterType '{0}'", msIL));
                }

                new Add {
                    DestinationReg = Register.ESP, DestinationIndirect = true, SourceRef = "0x" + offset.ToString("X")
                };
            }
            break;

            default:
                throw new Exception(string.Format("Unsupported target platform '{0}' for MSIL '{1}'", Config.TargetPlatform, msIL));
            }

            Optimizer.vStack.Push(new StackItem(typeof(uint)));
            Optimizer.SaveStack(xOp.NextPosition);
        }
Пример #8
0
 /// <summary>
 /// Construct the object.
 /// </summary>
 /// <param name="optimizer">Optimizer to use.</param>
 /// <param name="numRuns">Number of optimization runs to perform.</param>
 public RepeatMin(Optimizer optimizer, int numRuns)
     : base(optimizer, numRuns)
 {
 }
Пример #9
0
        /*
         * URL : https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.Ldloc(v=vs.110).aspx
         * Description : Loads the local variable at a specific index onto the evaluation stack.
         */
        internal override void Execute(Options Config, OpCodeType xOp, MethodBase method, Optimizer Optimizer)
        {
            var index = ((OpVar)xOp).Value;

            var body      = method.GetMethodBody();
            var EBPoffset = Helper.GetVariableOffset(body, index, Config.TargetPlatform);

            var varType = body.LocalVariables[index].LocalType;
            var size    = Helper.GetTypeSize(varType, Config.TargetPlatform);

            /* The stack transitional behavior, in sequential order, is:
             * The local variable value at the specified index is pushed onto the stack.
             */

            switch (Config.TargetPlatform)
            {
            case Architecture.x86:
            {
                if (size > 4)
                {
                    throw new Exception("LocalVariable size > 4 not supported");
                }

                new Push {
                    DestinationReg = Register.EBP, DestinationIndirect = true, DestinationDisplacement = -EBPoffset
                };
            }
            break;

            default:
                throw new Exception(string.Format("Unsupported target platform '{0}' for MSIL '{1}'", Config.TargetPlatform, msIL));
            }

            Optimizer.vStack.Push(new StackItem(varType));
            Optimizer.SaveStack(xOp.NextPosition);
        }
Пример #10
0
        /*
         * URL : https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.Stind_R4(v=vs.110).aspx
         * Description : Stores a value of type float32 at a supplied address.
         */
        internal override void Execute(Options Config, OpCodeType xOp, MethodBase method, Optimizer Optimizer)
        {
            if (Optimizer.vStack.Count < 2)
            {
                throw new Exception("Internal Compiler Error: vStack.Count < 2");
            }

            /* The stack transitional behavior, in sequential order, is:
             * An address is pushed onto the stack.
             * A value is pushed onto the stack.
             * The value and the address are popped from the stack; the value is stored at the address.
             */

            var itemA = Optimizer.vStack.Pop();
            var itemB = Optimizer.vStack.Pop();

            switch (Config.TargetPlatform)
            {
            case Architecture.x86:
            {
                if (!itemA.SystemStack)
                {
                    throw new Exception(string.Format("UnImplemented-RegisterType '{0}'", msIL));
                }

                if (!itemB.SystemStack)
                {
                    throw new Exception(string.Format("UnImplemented-RegisterType '{0}'", msIL));
                }

                Stind_I_il.Executex86(4);
            }
            break;

            default:
                throw new Exception(string.Format("Unsupported target platform '{0}' for MSIL '{1}'", Config.TargetPlatform, msIL));
            }

            Optimizer.SaveStack(xOp.NextPosition);
        }
Пример #11
0
        public void Fit(DataIter train_data, DataIter eval_data = null, string eval_metric = "acc",
                        IEpochEndCallback[] epoch_end_callback  = null, IBatchEndCallback[] batch_end_callback = null,
                        string kvstore      = "local",
                        Optimizer optimizer = null, Dictionary <string, object> optimizer_params = null,
                        IScoreEndCallback[] eval_end_callback       = null,
                        IBatchEndCallback[] eval_batch_end_callback = null, Initializer initializer = null,
                        NDArrayDict arg_params = null,
                        NDArrayDict aux_params = null, bool allow_missing = false, bool force_rebind = false,
                        bool force_init        = false, int begin_epoch   = 0, int?num_epoch = null, EvalMetric validation_metric = null,
                        Monitor monitor        = null, Func <DataBatch, NDArrayDict> sparse_row_id_fn = null)
        {
            object val;
            object name;

            if (optimizer == null)
            {
                optimizer = new SGD();
            }
            Debug.Assert(num_epoch != null, "please specify number of epochs");
            this.Bind(data_shapes: train_data.ProvideData, label_shapes: train_data.ProvideLabel, for_training: true, force_rebind: force_rebind);
            if (monitor != null)
            {
                this.InstallMonitor(monitor);
            }

            this.InitParams(initializer: initializer, arg_params: arg_params, aux_params: aux_params, allow_missing: allow_missing, force_init: force_init);
            this.InitOptimizer(kvstore: kvstore, optimizer: optimizer, optimizer_params: optimizer_params);
            if (validation_metric == null)
            {
                validation_metric = eval_metric;
            }

            var eval_metric_func = EvalMetric.Create(eval_metric, null);

            //###############################################################################
            // training loop
            //###############################################################################
            foreach (var epoch in Enumerable.Range(begin_epoch, num_epoch.Value - begin_epoch))
            {
                var tic = DateTime.Now;
                eval_metric_func.Reset();
                var nbatch          = 0;
                var data_iter       = train_data;
                var end_of_batch    = false;
                var next_data_batch = data_iter.Next();
                Dictionary <string, float> eval_name_vals = new Dictionary <string, float>();
                while (!end_of_batch)
                {
                    var data_batch = next_data_batch;
                    if (monitor != null)
                    {
                        monitor.Tic();
                    }

                    this.ForwardBackward(data_batch);
                    this.Update();

                    UpdateMetric(eval_metric_func, data_batch.Label);

                    try
                    {
                        // pre fetch next batch
                        next_data_batch = data_iter.Next();
                        this.Prepare(next_data_batch, sparse_row_id_fn: sparse_row_id_fn);
                    }
                    catch (StopIteration)
                    {
                        end_of_batch = true;
                    }
                    if (monitor != null)
                    {
                        monitor.TocPrint();
                    }

                    if (end_of_batch)
                    {
                        eval_name_vals = eval_metric_func.GetGlobalNameValue();
                    }

                    if (batch_end_callback != null)
                    {
                        foreach (var callback in batch_end_callback)
                        {
                            callback.Invoke(epoch: epoch, nbatch: nbatch, eval_metric: eval_metric);
                        }
                    }
                    nbatch += 1;
                }

                // one epoch of training is finished
                foreach (var item in eval_name_vals)
                {
                    name = item.Key;
                    val  = item.Value;
                    Logger.Info($"Epoch[{epoch}] Train-{name}={val}");
                }

                var toc = DateTime.Now;

                Logger.Info($"Epoch[{epoch}] Time cost={(toc - tic).TotalSeconds}");
                // sync aux params across devices
                (arg_params, aux_params) = this.GetParams();
                this.SetParams(arg_params, aux_params);
                if (epoch_end_callback != null)
                {
                    foreach (var callback in epoch_end_callback)
                    {
                        callback.Invoke(epoch, this.Symbol, arg_params, aux_params);
                    }
                }
                //----------------------------------------
                // evaluation on validation set
                if (eval_data != null)
                {
                    var res = this.Score(eval_data, validation_metric, score_end_callback: eval_end_callback, batch_end_callback: eval_batch_end_callback, epoch: epoch);
                    //TODO: pull this into default
                    foreach (var item in res)
                    {
                        name = item.Key;
                        val  = item.Value;
                        Logger.Info($"Epoch[{epoch}] Validation-{name}={val}");
                    }
                }
                // end of 1 epoch, reset the data-iter for another epoch
                train_data.Reset();
            }
        }
Пример #12
0
        /*
         * URL : https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.Ldelem_Ref(v=vs.110).aspx
         * Description : Loads the element containing an object reference at a specified array index onto the top of the evaluation stack as type O (object reference).
         */
        internal override void Execute(Options Config, OpCodeType xOp, MethodBase method, Optimizer Optimizer)
        {
            if (Optimizer.vStack.Count < 2)
            {
                throw new Exception("Internal Compiler Error: vStack.Count < 2");
            }

            /* The stack transitional behavior, in sequential order, is:
             * An object reference array is pushed onto the stack.
             * An index value index is pushed onto the stack.
             * index and array are popped from the stack; the value stored at position index in array is looked up.
             * The value is pushed onto the stack.
             */

            var itemA = Optimizer.vStack.Pop();
            var itemB = Optimizer.vStack.Pop();

            switch (Config.TargetPlatform)
            {
            case Architecture.x86:
            {
                if (!itemA.SystemStack)
                {
                    throw new Exception(string.Format("UnImplemented-RegisterType '{0}'", msIL));
                }

                if (!itemB.SystemStack)
                {
                    throw new Exception(string.Format("UnImplemented-RegisterType '{0}'", msIL));
                }

                Ldelem_il.Executex86(4, false);
                Optimizer.vStack.Push(new StackItem(typeof(uint)));
                Optimizer.SaveStack(xOp.NextPosition);
            }
            break;

            default:
                throw new Exception(string.Format("Unsupported target platform '{0}' for MSIL '{1}'", Config.TargetPlatform, msIL));
            }
        }
Пример #13
0
        public CodeGenerator(
            MethodSymbol method,
            BoundStatement boundBody,
            ILBuilder builder,
            PEModuleBuilder moduleBuilder,
            DiagnosticBag diagnostics,
            OptimizationLevel optimizations,
            bool emittingPdb)
        {
            Debug.Assert((object)method != null);
            Debug.Assert(boundBody != null);
            Debug.Assert(builder != null);
            Debug.Assert(moduleBuilder != null);
            Debug.Assert(diagnostics != null);

            _method      = method;
            _boundBody   = boundBody;
            _builder     = builder;
            _module      = moduleBuilder;
            _diagnostics = diagnostics;

            if (!method.GenerateDebugInfo)
            {
                // Always optimize synthesized methods that don't contain user code.
                //
                // Specifically, always optimize synthesized explicit interface implementation methods
                // (aka bridge methods) with by-ref returns because peverify produces errors if we
                // return a ref local (which the return local will be in such cases).
                _ilEmitStyle = ILEmitStyle.Release;
            }
            else
            {
                if (optimizations == OptimizationLevel.Debug)
                {
                    _ilEmitStyle = ILEmitStyle.Debug;
                }
                else
                {
                    _ilEmitStyle = IsDebugPlus() ?
                                   ILEmitStyle.DebugFriendlyRelease :
                                   ILEmitStyle.Release;
                }
            }

            // Emit sequence points unless
            // - the PDBs are not being generated
            // - debug information for the method is not generated since the method does not contain
            //   user code that can be stepped through, or changed during EnC.
            //
            // This setting only affects generating PDB sequence points, it shall not affect generated IL in any way.
            _emitPdbSequencePoints = emittingPdb && method.GenerateDebugInfo;

            try
            {
                _boundBody = Optimizer.Optimize(
                    boundBody,
                    debugFriendly: _ilEmitStyle != ILEmitStyle.Release,
                    stackLocals: out _stackLocals);
            }
            catch (BoundTreeVisitor.CancelledByStackGuardException ex)
            {
                ex.AddAnError(diagnostics);
                _boundBody = boundBody;
            }

            _methodBodySyntaxOpt = (method as SourceMethodSymbol)?.BodySyntax;
        }
Пример #14
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Optimizer obj) {
   return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
 }
Пример #15
0
 protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
 {
     if (behavior != CommandBehavior.Default)
         throw new NotSupportedException();
     Notation notation = new Notation();
     YYParser parser = new YYParser(notation);
     object result = parser.yyparseSafe(new Tokenizer(_commandText));
     QueryContext context = new QueryContext(DatabaseDictionary);
     context.DatabaseDictionary.SearchPath = _searchPath;
     Optimizer optimizer = new Optimizer(context);
     optimizer.Process(notation);
     QueryBinder binder = new QueryBinder();
     binder.IsServerQuery = optimizer.IsServerQuery;
     binder.Process(notation);
     optimizer.PostProcess(notation);
     if (BeforeExecute != null)
         BeforeExecute(this, notation, optimizer, context);
     Notation.Record[] recs = notation.Select(Descriptor.Root, 1);
     Object[] raw_parameters = null;
     if (recs.Length > 0)
     {
         Notation.Record[] recsd = notation.Select(recs[0].Arg0, Descriptor.Binding, 1);
         if (recsd.Length > 0)
         {
             Symbol[] bindings = Lisp.ToArray<Symbol>(recsd[0].args[0]);
             raw_parameters = new object[bindings.Length];
             for (int k = 0; k < bindings.Length; k++)
             {
                 recsd = notation.Select(bindings[k], Descriptor.Link, 1);
                 if (recsd.Length > 0)
                 {
                     DataEngine.Parameter p = (DataEngine.Parameter)recsd[0].Arg0;
                     raw_parameters[k] = _parameters[p.ParameterName].Value;
                 }
             }
         }
     }
     Translator translator = new Translator(context);
     QueryNode rootNode = translator.Process(notation);
     Resultset rs = rootNode.Get(context, raw_parameters);
     optimizer.ProcessResults(rs);
     return new DataReader(rs, context);
 }
Пример #16
0
 /// <summary>
 /// Construct the object.
 /// </summary>
 /// <param name="optimizer">Optimizer to use.</param>
 /// <param name="numRuns">Number of optimization runs.</param>
 public Repeat(Optimizer optimizer, int numRuns)
     : base()
 {
     Optimizer = optimizer;
     NumRuns = numRuns;
 }
Пример #17
0
 public void ShouldThrowExceptionWhenFileIsNull()
 {
     Assert.Throws <ArgumentNullException>("file", () => Optimizer.Compress((FileInfo)null));
 }
Пример #18
0
 public abstract void InitOptimizer(string kvstore = "local", Optimizer optimizer = null,
                                    Dictionary <string, object> optimizer_params = null, bool force_init = false);
Пример #19
0
        public void OptimizerCombinators_ArgumentChecks()
        {
            AssertEx.ThrowsException <ArgumentNullException>(() => Optimizer.Then(first: null, Optimizer.Nop()), AssertParameterName("first"));
            AssertEx.ThrowsException <ArgumentNullException>(() => Optimizer.Nop().Then(second: null), AssertParameterName("second"));

            AssertEx.ThrowsException <ArgumentNullException>(() => Optimizer.FixedPoint(optimizer: null), AssertParameterName("optimizer"));

            AssertEx.ThrowsException <ArgumentNullException>(() => Optimizer.FixedPoint(optimizer: null, throwOnCycle: true), AssertParameterName("optimizer"));

            AssertEx.ThrowsException <ArgumentNullException>(() => Optimizer.FixedPoint(optimizer: null, throwOnCycle: true, maxIterations: 0), AssertParameterName("optimizer"));
            AssertEx.ThrowsException <ArgumentOutOfRangeException>(() => Optimizer.Nop().FixedPoint(throwOnCycle: true, maxIterations: -1), AssertParameterName("maxIterations"));
        }
Пример #20
0
        // destination with hint in which direction to go
        public Tuple <Point, Direction> GetDestination(Man man)
        {
            var enemies = MenOfRace(Enemy(man.Race)).ToArray();

            {
                var enimiesDistance = enemies.ToDictionary(e => e, e => e.Position.Distance(man.Position)).ToArray();
                var minDistanceAsIs = enimiesDistance.Min(p => p.Value);
                Assert.True(minDistanceAsIs > 0, "never enemies on the same position");
                if (minDistanceAsIs == 1)
                {
                    // we are in position to attack an enemy, no need to move
                    return(null);
                }
            }

            // positions in range versus enemies
            var inRange = enemies
                          .SelectMany(e => _map.Directions(e.Position).Select(e.Position.Go))
                          .ToHashSet()
                          .ToDictionary(p => p, p => p.Distance(man.Position))
                          .ToArray()
            ;                     // different enemies can have same in range positions

            // it could happen that all enemies (ie the only one) are rounded from all sides
            // in this way no way to go
            if (!inRange.Any())
            {
                return(null);
            }

            // simplest strategy: select path with minimal distance
            var minDistance = inRange.Min(p => p.Value);

            if (minDistance == 0)
            {
                return(null);                              // no way to go
            }
            // more difficult task
            var optimizer = new Optimizer(_map, man.Position);

            var partialResult      = new Dictionary <Point, int>();
            int?curMinimalDistance = null;

            foreach (var inRangePoint in inRange.OrderBy(p => p.Value))
            {
                // we try from  by air shortest distance
                if (curMinimalDistance.HasValue)
                {
                    if (curMinimalDistance.Value < inRangePoint.Value)
                    {
                        // the distance already found is less then shortest candidate by air
                        // we may terminate
                        break;
                    }
                }

                var distance = optimizer.Distance(inRangePoint.Key);
                if (distance.HasValue)
                {
                    partialResult.Add(inRangePoint.Key, distance.Value);
                    curMinimalDistance = curMinimalDistance.HasValue
                                                ? Math.Min(curMinimalDistance.Value, distance.Value) : distance.Value;
                }
            }

            if (!curMinimalDistance.HasValue)
            {
                // no way to connect two points
                return(null);
            }

            var destinationList     = partialResult.Where(p => p.Value == curMinimalDistance.Value).Select(p => p.Key);
            var selectedDestination = destinationList.OrderBy(p => p.ReadingOrder).First();

            var optimizer1 = new Optimizer(_map, selectedDestination);
            var options    = _map.Directions(man.Position)
                             .ToDictionary(d => d, d => man.Position.Go(d))
                             .OrderBy(p => p.Value.ReadingOrder)
                             .ToArray();

            foreach (var o in options)
            {
                var distance = optimizer1.Distance(o.Value);                 // decreased distance due to the step ahead to the target
                if (distance.HasValue)
                {
                    if ((curMinimalDistance.Value - 1) == distance.Value)
                    {
                        // this is the correct direction and expected distance
                        return(Tuple.Create(selectedDestination, o.Key));
                    }
                }
            }
            throw new Exception("unexpected processing of options");
        }
Пример #21
0
        /// <summary>
        /// Call the Optimizer
        /// </summary>
        private void ShowOptimizer()
        {
            // Put the Strategy into the Undo Stack
            Data.StackStrategy.Push(Data.Strategy.Clone());

            var optimizer = new Optimizer {SetParrentForm = this};
            optimizer.ShowDialog();

            if (optimizer.DialogResult == DialogResult.OK)
            {
                // We accept the optimized strategy
                Text = Path.GetFileNameWithoutExtension(Data.StrategyName) + "* - " + Data.ProgramName;
                Data.IsStrategyChanged = true;
                RepaintStrategyLayout();
                Calculate(true);
            }
            else
            {
                // If we cancel the optimizing, we return the original strategy.
                UndoStrategy();
            }

            Data.OptimizerStarts++;
        }
Пример #22
0
        /*
         * URL : https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.Ldstr(v=vs.110).aspx
         * Description : Pushes a new object reference to a string literal stored in the metadata.
         */
        internal override void Execute(Options Config, OpCodeType xOp, MethodBase method, Optimizer Optimizer)
        {
            var str = ((OpString)xOp).Value;

            /* The stack transitional behavior, in sequential order, is:
             * An object reference to a string is pushed onto the stack.
             */

            switch (Config.TargetPlatform)
            {
            case Architecture.x86:
            {
                new Push {
                    DestinationRef = Helper.GetResolvedStringLabel(str)
                };

                Optimizer.vStack.Push(new StackItem(typeof(string)));
                Optimizer.SaveStack(xOp.NextPosition);
            }
            break;

            default:
                throw new Exception(string.Format("Unsupported target platform '{0}' for MSIL '{1}'", Config.TargetPlatform, msIL));
            }
        }
Пример #23
0
        /*
         * URL : https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.Blt_Un(v=vs.110).aspx
         * Description : Transfers control to a target instruction if the first value is less than the second value, when comparing unsigned integer values or unordered float values.
         */
        internal override void Execute(Options Config, OpCodeType xOp, MethodBase method, Optimizer Optimizer)
        {
            if (Optimizer.vStack.Count < 2)
            {
                throw new Exception("Internal Compiler Error: vStack.Count < 2");
            }

            var offset = ((OpBranch)xOp).Value;
            var itemA  = Optimizer.vStack.Pop();
            var itemB  = Optimizer.vStack.Pop();

            var xTrueLabel = Helper.GetLabel(offset);

            var size = Math.Max(Helper.GetTypeSize(itemA.OperandType, Config.TargetPlatform),
                                Helper.GetTypeSize(itemB.OperandType, Config.TargetPlatform));

            /* The stack transitional behavior, in sequential order, is:
             * value1 is pushed onto the stack.
             * value2 is pushed onto the stack.
             * value2 and value1 are popped from the stack; if value1 is less than value2, the branch operation is performed.
             */

            switch (Config.TargetPlatform)
            {
            case Architecture.x86:
            {
                if (itemA.IsFloat || itemB.IsFloat || size > 4)
                {
                    throw new Exception(string.Format("UnImplemented '{0}'", msIL));
                }

                if (!itemA.SystemStack || !itemB.SystemStack)
                {
                    throw new Exception(string.Format("UnImplemented-RegisterType '{0}'", msIL));
                }

                new Pop {
                    DestinationReg = Register.EAX
                };
                new Pop {
                    DestinationReg = Register.EDX
                };
                new Cmp {
                    DestinationReg = Register.EDX, SourceReg = Register.EAX
                };
                new Jmp {
                    Condition = ConditionalJump.JB, DestinationRef = xTrueLabel
                };

                Optimizer.SaveStack(offset);
                Optimizer.SaveStack(xOp.NextPosition);
            }
            break;

            default:
                throw new Exception(string.Format("Unsupported target platform '{0}' for MSIL '{1}'", Config.TargetPlatform, msIL));
            }
        }
Пример #24
0
 public OptimizerStore(Optimizer optimizer, IStore store)
 {
     _optimizer = optimizer;
     _store     = store;
 }
Пример #25
0
        /*
         * URL : https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.Ldarg(v=vs.110).aspx
         * Description : Loads an argument (referenced by a specified index value) onto the stack.
         */
        internal override void Execute(Options Config, OpCodeType xOp, MethodBase method, Optimizer Optimizer)
        {
            var index = ((OpVar)xOp).Value;

            int EBPoffset = GetArgumentOffset(Config, method, index);

            Type ArgType = null;

            if (!method.IsStatic)
            {
                if (index == 0)
                {
                    ArgType = method.DeclaringType;
                    if (method.DeclaringType.IsValueType)
                    {
                        ArgType = ArgType.MakeByRefType();
                    }
                }
                else
                {
                    ArgType = method.GetParameters()[index - 1].ParameterType;
                }
            }
            else
            {
                ArgType = method.GetParameters()[index].ParameterType;
            }

            int ArgSize = Helper.GetTypeSize(ArgType, Config.TargetPlatform);

            /* The stack transitional behavior, in sequential order, is:
             * The argument value at index is pushed onto the stack.
             */

            switch (Config.TargetPlatform)
            {
            case Architecture.x86:
            {
                if (ArgSize > 4)
                {
                    throw new Exception("Unsupported ArgSize");
                }

                new Push
                {
                    DestinationReg          = Register.EBP,
                    DestinationDisplacement = EBPoffset,
                    DestinationIndirect     = true
                };
            }
            break;

            default:
                throw new Exception(string.Format("Unsupported target platform '{0}' for MSIL '{1}'", Config.TargetPlatform, msIL));
            }

            Optimizer.vStack.Push(new StackItem(ArgType));
            Optimizer.SaveStack(xOp.NextPosition);
        }
Пример #26
0
 public void ProcessMessages(IReadOnlyCollection <Replicator> messageReplicators, Optimizer optimizer)
 {
     foreach (var r in messageReplicators)
     {
         r.Process(false);
     }
 }
Пример #27
0
        private static void train(int epoch, TorchTensor train_data, TransformerModel model, Loss criterion, int bptt, int ntokens, Optimizer optimizer)
        {
            model.Train();

            var total_loss = 0.0f;

            var src_mask = model.GenerateSquareSubsequentMask(bptt);

            var batch        = 0;
            var log_interval = 200;

            var tdlen = train_data.shape[0];

            for (int i = 0; i < tdlen - 1; batch++, i += bptt)
            {
                var(data, targets) = GetBatch(train_data, i, bptt);
                optimizer.zero_grad();

                if (data.shape[0] != bptt)
                {
                    src_mask.Dispose();
                    src_mask = model.GenerateSquareSubsequentMask(data.shape[0]);
                }

                var output = model.forward(data, src_mask);
                var loss   = criterion(output.view(-1, ntokens), targets);
                {
                    loss.backward();
                    model.parameters().clip_grad_norm(0.5);
                    optimizer.step();

                    total_loss += loss.to(Device.CPU).DataItem <float>();
                }

                GC.Collect();

                if (batch % log_interval == 0 && batch > 0)
                {
                    var cur_loss = total_loss / log_interval;
                    Console.WriteLine($"epoch: {epoch} | batch: {batch} / {tdlen/bptt} | loss: {cur_loss:0.00}");
                    total_loss = 0;
                }
            }
        }
Пример #28
0
 public void ProcessFacts(IReadOnlyCollection <Replicator> factReplicators, IReadOnlyCollection <Replicator> aggregateReplicators, IReadOnlyCollection <Replicator> messageReplicators, Optimizer optimizer)
 {
     foreach (var r in factReplicators)
     {
         r.Process(false);
     }
 }
Пример #29
0
 public void SetOptimizer(Optimizer se)
 {
     this.m_optimizer = se;
 }
Пример #30
0
 public void ProcessFacts(IReadOnlyCollection <Replicator> factReplicators, IReadOnlyCollection <Replicator> aggregateReplicators, IReadOnlyCollection <Replicator> messageReplicators, Optimizer optimizer)
 {
     foreach (var r in factReplicators)
     {
         r.Process(r.SkipCheckFunction.Invoke(optimizer.IsKnownEmpty));
     }
 }
Пример #31
0
        protected virtual void TrainCore(IChannel ch, RoleMappedData data)
        {
            Host.AssertValue(ch);
            ch.AssertValue(data);

            // Compute the number of threads to use. The ctor should have verified that this will
            // produce a positive value.
            int numThreads = !UseThreads ? 1 : (NumThreads ?? Environment.ProcessorCount);

            if (Host.ConcurrencyFactor > 0 && numThreads > Host.ConcurrencyFactor)
            {
                numThreads = Host.ConcurrencyFactor;
                ch.Warning("The number of threads specified in trainer arguments is larger than the concurrency factor "
                           + "setting of the environment. Using {0} training threads instead.", numThreads);
            }

            ch.Assert(numThreads > 0);

            NumGoodRows = 0;
            WeightSum   = 0;

            _features = null;
            _labels   = null;
            _weights  = null;
            if (numThreads > 1)
            {
                ch.Info("LBFGS multi-threading will attempt to load dataset into memory. In case of out-of-memory " +
                        "issues, add 'numThreads=1' to the trainer arguments and 'cache=-' to the command line " +
                        "arguments to turn off multi-threading.");
                _features = new VBuffer <float> [1000];
                _labels   = new float[1000];
                if (data.Schema.Weight != null)
                {
                    _weights = new float[1000];
                }
            }

            var cursorFactory = new FloatLabelCursor.Factory(data, CursOpt.Features | CursOpt.Label | CursOpt.Weight);

            long numBad;

            // REVIEW: This pass seems overly expensive for the benefit when multi-threading is off....
            using (var cursor = cursorFactory.Create())
                using (var pch = Host.StartProgressChannel("LBFGS data prep"))
                {
                    // REVIEW: maybe it makes sense for the factory to capture the good row count after
                    // the first successful cursoring?
                    Double totalCount = data.Data.GetRowCount(true) ?? Double.NaN;

                    long exCount = 0;
                    pch.SetHeader(new ProgressHeader(null, new[] { "examples" }),
                                  e => e.SetProgress(0, exCount, totalCount));
                    while (cursor.MoveNext())
                    {
                        WeightSum += cursor.Weight;
                        if (ShowTrainingStats)
                        {
                            ProcessPriorDistribution(cursor.Label, cursor.Weight);
                        }

                        PreTrainingProcessInstance(cursor.Label, ref cursor.Features, cursor.Weight);
                        exCount++;
                        if (_features != null)
                        {
                            ch.Assert(cursor.KeptRowCount <= int.MaxValue);
                            int index = (int)cursor.KeptRowCount - 1;
                            Utils.EnsureSize(ref _features, index + 1);
                            Utils.EnsureSize(ref _labels, index + 1);
                            if (_weights != null)
                            {
                                Utils.EnsureSize(ref _weights, index + 1);
                                _weights[index] = cursor.Weight;
                            }
                            Utils.Swap(ref _features[index], ref cursor.Features);
                            _labels[index] = cursor.Label;

                            if (cursor.KeptRowCount >= int.MaxValue)
                            {
                                ch.Warning("Limiting data size for multi-threading");
                                break;
                            }
                        }
                    }
                    NumGoodRows = cursor.KeptRowCount;
                    numBad      = cursor.SkippedRowCount;
                }
            ch.Check(NumGoodRows > 0, NoTrainingInstancesMessage);
            if (numBad > 0)
            {
                ch.Warning("Skipped {0} instances with missing features/label/weight during training", numBad);
            }

            if (_features != null)
            {
                ch.Assert(numThreads > 1);

                // If there are so many threads that each only gets a small number (less than 10) of instances, trim
                // the number of threads so each gets a more reasonable number (100 or so). These numbers are pretty arbitrary,
                // but avoid the possibility of having no instances on some threads.
                if (numThreads > 1 && NumGoodRows / numThreads < 10)
                {
                    int numNew = Math.Max(1, (int)NumGoodRows / 100);
                    ch.Warning("Too few instances to use {0} threads, decreasing to {1} thread(s)", numThreads, numNew);
                    numThreads = numNew;
                }
                ch.Assert(numThreads > 0);

                // Divide up the instances among the threads.
                _numChunks = numThreads;
                _ranges    = new int[_numChunks + 1];
                int cinstTot = (int)NumGoodRows;
                for (int ichk = 0, iinstMin = 0; ichk < numThreads; ichk++)
                {
                    int cchkLeft = numThreads - ichk;                                // Number of chunks left to fill.
                    ch.Assert(0 < cchkLeft && cchkLeft <= numThreads);
                    int cinstThis = (cinstTot - iinstMin + cchkLeft - 1) / cchkLeft; // Size of this chunk.
                    ch.Assert(0 < cinstThis && cinstThis <= cinstTot - iinstMin);
                    iinstMin         += cinstThis;
                    _ranges[ichk + 1] = iinstMin;
                }

                _localLosses    = new float[numThreads];
                _localGradients = new VBuffer <float> [numThreads - 1];
                int size = BiasCount + WeightCount;
                for (int i = 0; i < _localGradients.Length; i++)
                {
                    _localGradients[i] = VBufferUtils.CreateEmpty <float>(size);
                }

                ch.Assert(_numChunks > 0 && _data == null);
            }
            else
            {
                // Streaming, single-threaded case.
                _data          = data;
                _cursorFactory = cursorFactory;
                ch.Assert(_numChunks == 0 && _data != null);
            }

            VBuffer <float>       initWeights;
            ITerminationCriterion terminationCriterion;
            Optimizer             opt = InitializeOptimizer(ch, cursorFactory, out initWeights, out terminationCriterion);

            opt.Quiet = Quiet;

            float loss;

            try
            {
                opt.Minimize(DifferentiableFunction, ref initWeights, terminationCriterion, ref CurrentWeights, out loss);
            }
            catch (Optimizer.PrematureConvergenceException e)
            {
                if (!Quiet)
                {
                    ch.Warning("Premature convergence occurred. The OptimizationTolerance may be set too small. {0}", e.Message);
                }
                CurrentWeights = e.State.X;
                loss           = e.State.Value;
            }

            ch.Assert(CurrentWeights.Length == BiasCount + WeightCount);

            int numParams = BiasCount;

            if ((L1Weight > 0 && !Quiet) || ShowTrainingStats)
            {
                VBufferUtils.ForEachDefined(ref CurrentWeights, (index, value) => { if (index >= BiasCount && value != 0)
                                                                                    {
                                                                                        numParams++;
                                                                                    }
                                            });
                if (L1Weight > 0 && !Quiet)
                {
                    ch.Info("L1 regularization selected {0} of {1} weights.", numParams, BiasCount + WeightCount);
                }
            }

            if (ShowTrainingStats)
            {
                ComputeTrainingStatistics(ch, cursorFactory, loss, numParams);
            }
        }
Пример #32
0
            public void ProcessMessages(IReadOnlyCollection <Replicator> messageReplicators, Optimizer optimizer)
            {
                var skipMessageReplicators = messageReplicators.Where(x => x.SkipCheckFunction.Invoke(optimizer.IsKnownEmpty)).ToList();

                foreach (var r in messageReplicators.Except(skipMessageReplicators))
                {
                    r.Process(false);
                }
            }
Пример #33
0
 public void ShouldThrowExceptionWhenStreamIsNull()
 {
     Assert.Throws <ArgumentNullException>("stream", () => Optimizer.Compress((Stream)null));
 }
Пример #34
0
            public void ProcessFacts(IReadOnlyCollection <Replicator> factReplicators, IReadOnlyCollection <Replicator> aggregateReplicators, IReadOnlyCollection <Replicator> messageReplicators, Optimizer optimizer)
            {
                var skipFactReplicators = factReplicators.Where(x => x.SkipCheckFunction.Invoke(optimizer.IsKnownEmpty)).ToList();

                foreach (var r in skipFactReplicators)
                {
                    r.Process(true);
                }

                var skipAggregateReplicators = aggregateReplicators.Where(x => x.SkipCheckFunction.Invoke(optimizer.IsKnownEmpty)).ToList();

                foreach (var r in skipAggregateReplicators)
                {
                    r.Process(true);
                }

                var skipMessageReplicators = messageReplicators.Where(x => x.SkipCheckFunction.Invoke(optimizer.IsKnownEmpty)).ToList();

                var actualAggregateTypes         = new HashSet <Type>(messageReplicators.Except(skipMessageReplicators).SelectMany(x => x.Dependencies));
                var inactualAggregateReplicators = aggregateReplicators.Where(x => !actualAggregateTypes.Contains(x.DataObjectType)).ToList();

                var actualFactTypes         = new HashSet <Type>(aggregateReplicators.Except(skipAggregateReplicators).Except(inactualAggregateReplicators).SelectMany(x => x.Dependencies));
                var inactualFactReplicators = factReplicators.Where(x => !actualFactTypes.Contains(x.DataObjectType)).ToList();

                foreach (var r in factReplicators.Except(skipFactReplicators).Except(inactualFactReplicators))
                {
                    r.Process(false);
                }
            }
Пример #35
0
        /*
         * URL : https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.Newarr(v=vs.110).aspx
         * Description : Pushes an object reference to a new zero-based, one-dimensional array whose elements are of a specific type onto the evaluation stack.
         */
        internal override void Execute(Options Config, OpCodeType xOp, MethodBase method, Optimizer Optimizer)
        {
            if (Optimizer.vStack.Count < 1)
            {
                throw new Exception("Internal Compiler Error: vStack.Count < 1");
            }

            var type = ((OpType)xOp).Value;
            var size = Helper.GetTypeSize(type, Config.TargetPlatform);

            /* The stack transitional behavior, in sequential order, is:
             * The number of elements in the array is pushed onto the stack.
             * The number of elements is popped from the stack and the array is created.
             * An object reference to the new array is pushed onto the stack.
             */

            var item = Optimizer.vStack.Pop();

            switch (Config.TargetPlatform)
            {
            case Architecture.x86:
            {
                if (!item.SystemStack)
                {
                    throw new Exception(string.Format("UnImplemented-RegisterType '{0}'", msIL));
                }

                new Mov {
                    DestinationReg = Register.ESI, SourceReg = Register.ESP, SourceIndirect = true
                };
                new Mov {
                    DestinationReg = Register.EAX, SourceRef = "0x" + size.ToString()
                };
                new Mul {
                    DestinationReg = Register.ESI
                };
                new Add {
                    DestinationReg = Register.EAX, SourceRef = "0x10"
                };

                new Push {
                    DestinationReg = Register.EAX
                };
                new Call {
                    DestinationRef = Helper.Heap_Label, IsLabel = true
                };
                new Test {
                    DestinationReg = Register.ECX, SourceRef = "0xFFFFFFFF"
                };
                new Jmp {
                    Condition = ConditionalJump.JNZ, DestinationRef = xOp.HandlerRef
                };

                new Pop {
                    DestinationReg = Register.ESI
                };
                new Mov {
                    DestinationReg = Register.EAX, DestinationIndirect = true, SourceRef = "0x" + type.GetHashCode().ToString("X")
                };
                new Mov {
                    DestinationReg = Register.EAX, DestinationIndirect = true, DestinationDisplacement = 4, SourceRef = "0x2"
                };
                new Mov {
                    DestinationReg = Register.EAX, DestinationIndirect = true, DestinationDisplacement = 8, SourceReg = Register.ESI
                };
                new Mov {
                    DestinationReg = Register.EAX, DestinationIndirect = true, DestinationDisplacement = 12, SourceRef = "0x" + size.ToString("X")
                };

                new Push {
                    DestinationReg = Register.EAX
                };

                Optimizer.vStack.Push(new StackItem(typeof(Array)));
                Optimizer.SaveStack(xOp.NextPosition);
            }
            break;

            default:
                throw new Exception(string.Format("Unsupported target platform '{0}' for MSIL '{1}'", Config.TargetPlatform, msIL));
            }
        }
Пример #36
0
 /// <summary>
 /// Construct the object, un-weighted problems.
 /// </summary>
 /// <param name="optimizer">Optimize to be used.</param>
 /// <param name="problems">Array of problems to be optimized.</param>
 /// <param name="numRuns">Number of optimization runs per problem.</param>
 /// <param name="maxIterations">Max number of optimization iterations.</param>
 public MetaFitness(Optimizer optimizer, Problem[] problems, int numRuns, int maxIterations)
     : base(optimizer, problems, numRuns, maxIterations)
 {
 }
Пример #37
0
 public void SetOptimizer(Optimizer se)
 {
     this._optimizer = se;
 }
Пример #38
0
        private void StartOptimize()
        {
            uiWeeklyData_superGridControlTable.PrimaryGrid.DataSource = null;
            var optimizer = new Optimizer(_strategy, OptimizationParameters.Instance.Parameters, uiStrategy_dateTimeAdvOISStart.Value, uiStrategy_dateTimeAdvOISEnd.Value);
            optimizer.ProgressEvent += OptimizeProgressIncrement;
            optimizer.StartOptimize(uiSummary_dataGridViewBT, uiBTSummaryChart, uiBTSummaryGroupPanel);

            Invoke((Action)delegate
            {
                Report("In Sample/Out Of Sample procedure completed.", InformerMessageType.Success);
                uiStrategy_buttonXStart.Enabled = true;
                uiStrategy_buttonXStop.Enabled = false;
            });
        }
Пример #39
0
        public IReadOnlyCollection <Version.ValidationResult> Execute(long orderId, ICheckModeDescriptor checkModeDescriptor)
        {
            // todo: можно использовать checkModeDescriptor для дальнейшей оптимизации
            var optimization           = new Optimizer();
            Func <IStore, IStore> wrap = store => new OptimizerStore(optimization, store);

            using (Probe.Create("Execute"))
                using (var erm = new HashSetStoreFactory())
                    using (var store = new PersistentTableStoreFactory(_lockManager, _schemaManager))
                        using (var messages = new HashSetStoreFactory())
                        {
                            IReadOnlyCollection <Replicator> factReplicators;
                            IReadOnlyCollection <Replicator> aggregateReplicators;
                            IReadOnlyCollection <Replicator> messageReplicators;

                            using (Probe.Create("Initialization"))
                            {
                                factReplicators      = CreateReplicators(_factAccessorTypes, erm.CreateQuery(), wrap(store.CreateStore()));
                                aggregateReplicators = CreateReplicators(_aggregateAccessorTypes, store.CreateQuery(), wrap(store.CreateStore()));
                                messageReplicators   = CreateReplicators(_messageAccessorTypes, store.CreateQuery(), wrap(messages.CreateStore()))
                                                       .Where(x => x.DataObjectType == typeof(Version.ValidationResult) && checkModeDescriptor.Rules.ContainsKey(x.Rule)).ToArray();

                                var predicates = factReplicators.Concat(aggregateReplicators).Concat(messageReplicators).SelectMany(x => x.DependencyPredicates);
                                optimization.PrepareToUse(predicates.Distinct());
                            }

                            ErmDataLoader.ResolvedOrderSummary orderSummary;
                            using (Probe.Create("Erm -> Erm slice"))
                            {
                                ReadErmSlice(orderId, wrap(erm.CreateStore()), out orderSummary);
                            }

                            using (Probe.Create("Rulesets -> Facts"))
                            {
                                ReadRulesetsSlice(orderSummary, wrap(store.CreateStore()));
                            }

                            using (Probe.Create("Erm slice -> Facts"))
                            {
                                _strategy.ProcessFacts(factReplicators, aggregateReplicators, messageReplicators, optimization);
                            }

                            using (Probe.Create("Facts -> Aggregates"))
                            {
                                _strategy.ProcessAggregates(aggregateReplicators, messageReplicators, optimization);
                            }

                            using (Probe.Create("Aggregates -> Messages"))
                            {
                                _strategy.ProcessMessages(messageReplicators, optimization);
                            }

                            var validationPeriodStart = GetValidationPeriodStart(erm.CreateQuery(), orderId, checkModeDescriptor);
                            using (Probe.Create("Query result"))
                            {
                                return(messages.CreateQuery()
                                       .For <Version.ValidationResult>()
                                       .Where(x => x.OrderId == orderId && checkModeDescriptor.Rules.Keys.Contains((MessageTypeCode)x.MessageType) && x.PeriodEnd >= validationPeriodStart)
                                       .ToArray());
                            }
                        }
        }
Пример #40
0
 /// <summary>
 /// Create an OptimizerWrapper-object.
 /// </summary>
 /// <remarks>
 /// This is very similar to ProblemWrapper but C# does not allow
 /// for multiple inheritance and we need this class to inherit from
 /// Optimizer and therefore cannot make it inherit from ProblemWrapper
 /// as well.
 /// </remarks>
 /// <param name="optimizer">Optimizer-object being wrapped.</param>
 public OptimizerWrapper(Optimizer optimizer)
     : base()
 {
     Optimizer = optimizer;
 }
Пример #41
0
 /// <summary>
 /// Construct the object, weighted problems.
 /// </summary>
 /// <param name="optimizer">Optimize to be used.</param>
 /// <param name="weightedProblems">Array of weighted problems to be optimized.</param>
 /// <param name="numRuns">Number of optimization runs per problem.</param>
 /// <param name="maxIterations">Max number of optimization iterations.</param>
 public MetaFitness(Optimizer optimizer, WeightedProblem[] weightedProblems, int numRuns, int maxIterations)
     : base(optimizer, weightedProblems, numRuns, maxIterations)
 {
 }
Пример #42
0
        /*
         * URL : https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.Shr_Un(v=vs.110).aspx
         * Description : Shifts an unsigned integer value (in zeroes) to the right by a specified number of bits, pushing the result onto the evaluation stack.
         */
        internal override void Execute(Options Config, OpCodeType xOp, MethodBase method, Optimizer Optimizer)
        {
            if (Optimizer.vStack.Count < 2)
            {
                throw new Exception("Internal Compiler Error: vStack.Count < 2");
            }

            var itemA = Optimizer.vStack.Pop();
            var itemB = Optimizer.vStack.Pop();

            var size = Math.Max(Helper.GetTypeSize(itemA.OperandType, Config.TargetPlatform),
                                Helper.GetTypeSize(itemB.OperandType, Config.TargetPlatform));

            /* The stack transitional behavior, in sequential order, is:
             * A value is pushed onto the stack.
             * The amount of bits to be shifted is pushed onto the stack.
             * The number of bits to be shifted and the value are popped from the stack; the value is shifted right by the specified number of bits.
             * The result is pushed onto the stack.
             */

            switch (Config.TargetPlatform)
            {
            case Architecture.x86:
            {
                if (itemA.IsFloat || itemB.IsFloat || size > 4)
                {
                    throw new Exception(string.Format("UnImplemented '{0}'", msIL));
                }

                if (!itemA.SystemStack || !itemB.SystemStack)
                {
                    throw new Exception(string.Format("UnImplemented-RegisterType '{0}'", msIL));
                }

                new Pop {
                    DestinationReg = Register.ECX
                };
                new Shr {
                    DestinationReg = Register.ESP, SourceReg = Register.CL, DestinationIndirect = true
                };

                Optimizer.vStack.Push(new StackItem(itemB.OperandType));
                Optimizer.SaveStack(xOp.NextPosition);
            }
            break;

            default:
                throw new Exception(string.Format("Unsupported target platform '{0}' for MSIL '{1}'", Config.TargetPlatform, msIL));
            }
        }