public async override Task <OptimizerResult> Minimize(double[] parameters)
        {
            if (CancellationToken.IsCancellationRequested)
            {
                throw new TaskCanceledException();
            }

            if (_minimize == null)
            {
                var assembly = await _cSharpCompiler.CreateAssembly(Code);

                var worker = await _workerFactory.CreateAsync();

                _backgroundService = await worker.CreateBackgroundServiceAsync <MinimizeFacade>();

                _minimize = _cSharpCompiler.GetDelegate(assembly);
            }

            var cost = await _backgroundService.RunAsync(r => r.Minimize(_minimize, parameters)).Result;

            await Task.Run(() =>
            {
                ActivityLogger.Add(Guid.NewGuid().ToString(), Keys, parameters, cost);
                //ActivityLogger.Add("Parameters:", parameters);
                //ActivityLogger.Add("Cost:", cost);
                //ActivityLogger.StateHasChanged();
            });

            return(new OptimizerResult(parameters, cost));
        }
예제 #2
0
        public static async Task <byte[]> Decompress(this IWorkerBackgroundService <CompressorService> Service, byte[] Data)
        {
            var lData = Convert.ToBase64String(Data);
            var cData = await Service.RunAsync((x) => x.Decompress(lData));

            if (cData == null)
            {
                return(null);
            }

            return(Convert.FromBase64String(cData));
        }
        public async Task <Dictionary <string, PlotlyData> > Split(string[] chunk)
        {
            if (_backgroundService == null)
            {
                var worker = await _workerFactory.CreateAsync();

                _backgroundService = await worker.CreateBackgroundServiceAsync <PlotlyLineSplitter>(new WorkerInitOptions
                {
                    DependentAssemblyFilenames = new[] { "Jtc.Optimization.Objects.dll", "Jtc.Optimization.Transformation.dll",
                                                         "System.Text.Json.dll" }
                });
            }

            return(await _backgroundService.RunAsync(s => s.Split(chunk)));
        }
예제 #4
0
        /// <summary>
        /// Creates a new background service using the specified <paramref name="factoryExpression"/>
        /// </summary>
        /// <typeparam name="TFactory"></typeparam>
        /// <typeparam name="TService"></typeparam>
        /// <param name="webWorkerService"></param>
        /// <param name="factoryExpression"></param>
        /// <returns></returns>
        public static async Task <IWorkerBackgroundService <TService> > CreateBackgroundServiceAsync <TFactory, TService>(
            this IWorkerBackgroundService <TFactory> webWorkerService,
            Expression <Func <TFactory, TService> > factoryExpression)
            where TFactory : class
            where TService : class
        {
            if (webWorkerService is null)
            {
                throw new ArgumentNullException(nameof(webWorkerService));
            }

            if (factoryExpression is null)
            {
                throw new ArgumentNullException(nameof(factoryExpression));
            }

            if (!(webWorkerService is WorkerBackgroundServiceProxy <TFactory> webWorkerProxy))
            {
                throw new ArgumentException($"{nameof(webWorkerService)} must be of type {nameof(WorkerBackgroundServiceProxy<TFactory>)}", nameof(webWorkerProxy));
            }

            return(await webWorkerProxy.InitFromFactoryAsync(factoryExpression));
        }
예제 #5
0
        public static async Task <byte[]> Compress(this IWorkerBackgroundService <CompressorService> Service, byte[] Data)
        {
            var lData = Convert.ToBase64String(Data);

            return(Convert.FromBase64String(await Service.RunAsync((x) => x.Compress(lData))));
        }