Exemplo n.º 1
0
 public Task <REvaluationResult> EvaluateAsync(string expression, REvaluationKind kind, CancellationToken ct = default(CancellationToken))
 {
     if (kind.HasFlag(REvaluationKind.Mutating))
     {
         Mutated?.Invoke(this, EventArgs.Empty);
     }
     return(Task.FromResult(new REvaluationResult()));
 }
Exemplo n.º 2
0
 Task IRCallbacks.Connected(string rVersion)
 {
     Prompt         = GetDefaultPrompt();
     _isHostRunning = true;
     _hostStartedTcs.TrySetResult(null);
     Connected?.Invoke(this, new RConnectedEventArgs(rVersion));
     Mutated?.Invoke(this, EventArgs.Empty);
     return(Task.CompletedTask);
 }
Exemplo n.º 3
0
 Task IRCallbacks.Connected(string rVersion)
 {
     Prompt         = DefaultPrompt;
     _isHostRunning = true;
     _initializationTcs.SetResult(null);
     Connected?.Invoke(this, new RConnectedEventArgs(rVersion));
     Mutated?.Invoke(this, EventArgs.Empty);
     return(Task.CompletedTask);
 }
Exemplo n.º 4
0
 public Task <IRSessionEvaluation> BeginEvaluationAsync(CancellationToken cancellationToken = default(CancellationToken))
 {
     _eval = new RSessionEvaluationMock();
     BeforeRequest?.Invoke(this, new RRequestEventArgs(_eval.Contexts, Prompt, 4096, true));
     if (_eval.IsMutating)
     {
         Mutated?.Invoke(this, EventArgs.Empty);
     }
     return(Task.FromResult(_eval));
 }
Exemplo n.º 5
0
 public Task <IRSessionEvaluation> BeginEvaluationAsync(CancellationToken cancellationToken = default(CancellationToken))
 {
     Evaluation = new RSessionEvaluationMock();
     BeforeRequest?.Invoke(this, new RBeforeRequestEventArgs(Evaluation.Contexts, Prompt, 4096, addToHistoty: true));
     if (Evaluation.IsMutating)
     {
         Mutated?.Invoke(this, EventArgs.Empty);
     }
     return(Task.FromResult((IRSessionEvaluation)Evaluation));
 }
Exemplo n.º 6
0
 private void OnMutated()
 {
     if (_disableMutatingOnReadConsole.Count == 0)
     {
         Mutated?.Invoke(this, EventArgs.Empty);
     }
     else
     {
         _delayedMutatedOnReadConsole = true;
     }
 }
Exemplo n.º 7
0
        public RSession(int id, Action onDispose)
        {
            Id            = id;
            _onDispose    = onDispose;
            _disposeToken = DisposeToken.Create <RSession>();
            _disableMutatingOnReadConsole = new CountdownDisposable(() => {
                if (!_delayedMutatedOnReadConsole)
                {
                    return;
                }

                _delayedMutatedOnReadConsole = false;
                Task.Run(() => Mutated?.Invoke(this, EventArgs.Empty));
            });
        }
Exemplo n.º 8
0
        public RSession(int id, IRHostBrokerConnector brokerConnector, Action onDispose)
        {
            Id = id;
            BrokerConnector = brokerConnector;
            _onDispose      = onDispose;
            _disposeToken   = DisposeToken.Create <RSession>();
            _disableMutatingOnReadConsole = new CountdownDisposable(() => {
                if (!_delayedMutatedOnReadConsole)
                {
                    return;
                }

                _delayedMutatedOnReadConsole = false;
                Task.Run(() => Mutated?.Invoke(this, EventArgs.Empty));
            });

            _afterHostStartedTask = TaskUtilities.CreateCanceled(new RHostDisconnectedException());
        }
Exemplo n.º 9
0
        /// <summary>
        /// Called when R wants to display generic Windows MessageBox.
        /// Graph app may call Win32 API directly rather than going via R API callbacks.
        /// </summary>
        /// <returns>Pressed button code</returns>
        async Task <MessageButtons> IRCallbacks.ShowDialog(IReadOnlyList <IRContext> contexts, string s, bool isEvaluationAllowed, MessageButtons buttons, CancellationToken ct)
        {
            await TaskUtilities.SwitchToBackgroundThread();

            if (isEvaluationAllowed)
            {
                await EvaluateAll(contexts, true, ct);
            }
            else
            {
                Mutated?.Invoke(this, EventArgs.Empty);
            }

            if (_hostClientApp != null)
            {
                return(await _hostClientApp.ShowMessage(s, buttons));
            }

            return(MessageButtons.OK);
        }
Exemplo n.º 10
0
        public RSession(int id, IBrokerClient brokerClient, Action onDispose)
        {
            Id            = id;
            BrokerClient  = brokerClient;
            _onDispose    = onDispose;
            _disposeToken = DisposeToken.Create <RSession>();
            _disableMutatingOnReadConsole = new CountdownDisposable(() => {
                if (!_delayedMutatedOnReadConsole)
                {
                    return;
                }

                _delayedMutatedOnReadConsole = false;
                Task.Run(() => Mutated?.Invoke(this, EventArgs.Empty));
            });

            _initializationLock   = new BinaryAsyncLock();
            _initializationTcs    = new TaskCompletionSourceEx <object>();
            _afterHostStartedTask = TaskUtilities.CreateCanceled(new RHostDisconnectedException());
        }
Exemplo n.º 11
0
        public RSession(int id, string name, IBrokerClient brokerClient, IExclusiveReaderLock initializationLock, Action onDispose)
        {
            Id            = id;
            Name          = name;
            BrokerClient  = brokerClient;
            _onDispose    = onDispose;
            _disposeToken = DisposeToken.Create <RSession>();
            _disableMutatingOnReadConsole = new CountdownDisposable(() => {
                if (!_delayedMutatedOnReadConsole)
                {
                    return;
                }

                _delayedMutatedOnReadConsole = false;
                Task.Run(() => Mutated?.Invoke(this, EventArgs.Empty));
            });

            _initializationLock = initializationLock;
            _stopHostLock       = new BinaryAsyncLock(true);
            _hostStartedTcs     = new TaskCompletionSourceEx <object>();
            _startupInfo        = new RHostStartupInfo();
        }
Exemplo n.º 12
0
        public void Process(int iteration)
        {
            // Utworznie zmutowanej populacji
            for (int i = 0; i < Size; i++)
            {
                if (Individual.Binary[i] == '0')
                {
                    Mutated.Add(new Individual()
                    {
                        Id = i + 1, Binary = ReplaceAtIndex(i, '1', Individual.Binary)
                    });
                }
                else
                {
                    Mutated.Add(new Individual()
                    {
                        Id = i + 1, Binary = ReplaceAtIndex(i, '0', Individual.Binary)
                    });
                }
            }

            // Liczenie wartości funkcji
            Individual.Real           = _calc.BinToReal(Individual.Binary);
            Individual.FunctionResult = GetFunctionResult(Individual.Real);

            // Liczenie wartości funkcji zmutowanych
            foreach (var item in Mutated)
            {
                item.Real           = _calc.BinToReal(item.Binary);
                item.FunctionResult = GetFunctionResult(item.Real);
            }

            // sortowanie zmutowanych
            Mutated = Mutated.OrderByDescending(_ => _.FunctionResult).ToList();

            // tworzenie tabeli wag i prawdopodobieństw
            for (int i = 1; i <= Size; i++)
            {
                var range = Mutated.FindIndex(_ => _.Id == i) + 1;
                var prob  = 1 / Math.Pow(range + 0.2, Tau);

                MutationElements.Add(new MutationModel()
                {
                    Id          = i,
                    Range       = range,
                    Probability = prob,
                    Selected    = GetRandomNumber(0, 1, Randomizer) < prob
                });
            }

            // mutacja
            var           ids = MutationElements.Where(_ => _.Selected).Select(_ => _.Id).ToList();
            StringBuilder sb2 = new StringBuilder(Individual.Binary);

            foreach (var elem in ids)
            {
                if (sb2[elem - 1] == '1')
                {
                    sb2[elem - 1] = '0';
                }
                else
                {
                    sb2[elem - 1] = '1';
                }
            }
            var result = sb2.ToString();


            var resReal = _calc.BinToReal(result);
            var fx      = GetFunctionResult(resReal);

            if (Best == null)
            {
                results.Iteration = iteration;
                Best              = fx;
                results.FXBest    = Best.Value;
                results.XRealBest = resReal;
                results.XBinBest  = result;
            }
            else
            {
                if (fx > Best)
                {
                    results.Iteration = iteration;
                    Best              = fx;
                    results.FXBest    = Best.Value;
                    results.XRealBest = resReal;
                    results.XBinBest  = result;
                }
            }
            Individual = new Individual()
            {
                Binary = result
            };
            results.FXs.Add(fx);
            results.Bests.Add(Best.Value);
        }