Beispiel #1
0
        protected void DrawBars(SKCanvas canvas, SKPoint[] points, SKSize itemSize, float origin, float headerHeight)
        {
            const float MinBarHeight = 4;

            if (points.Length > 0)
            {
                for (int i = 0; i < Inputs.Count(); i++)
                {
                    var input = Inputs.ElementAt(i);
                    var point = points[i];

                    using (var paint = new SKPaint
                    {
                        Style = SKPaintStyle.Fill,
                        Color = input.Color,
                    })
                    {
                        var x      = point.X - (itemSize.Width / 2);
                        var y      = Math.Min(origin, point.Y);
                        var height = Math.Max(MinBarHeight, Math.Abs(origin - point.Y));
                        if (height < MinBarHeight)
                        {
                            height = MinBarHeight;
                            if (y + height > Margin + itemSize.Height)
                            {
                                y = headerHeight + itemSize.Height - height;
                            }
                        }

                        var rect = SKRect.Create(x, y, itemSize.Width, height);
                        canvas.DrawRect(rect, paint);
                    }
                }
            }
        }
Beispiel #2
0
 public override void Calculate()
 {
     if (Outputs.Count == 1 && Inputs.All(i => i.State.HasValue))
     {
         Outputs.First().State = Inputs.Count < 2 ? false : Inputs.Count(i => i.State == true) % 2 == 0;
     }
 }
Beispiel #3
0
        public IResolvedToken Resolve(FluTeCoreInstance template)
        {
            var inputObject = new MultiInput(template, Inputs);
            var value       = Inputs.Count() == 1 ? Processing.Invoke(inputObject[0]) : Processing.Invoke(inputObject);

            return(new ResolvedToken(this, value.ToString()));
        }
        public bool IsSame(InputsRequest other)
        {
            if (BlindedOutput != other.BlindedOutput)
            {
                return(false);
            }
            if (ChangeOutput != other.ChangeOutput)
            {
                return(false);
            }
            int inputsCount = Inputs.Count();

            if (inputsCount != other.Inputs.Count())
            {
                return(false);
            }
            for (int i = 0; i < inputsCount; i++)
            {
                if (Inputs[i].Input != other.Inputs[i].Input)
                {
                    return(false);
                }
                if (Inputs[i].Proof != other.Inputs[i].Proof)
                {
                    return(false);
                }
            }

            return(true);
        }
        private void DrawCaption(SKCanvas canvas, int width, int height)
        {
            var sumValue    = Inputs.Sum(x => Math.Abs(x.Value));
            var rightValues = new List <ChartInput>();
            var leftValues  = new List <ChartInput>();

            int i       = 0;
            var current = 0.0f;

            while (i < Inputs.Count() && (current < sumValue / 2))
            {
                var input = Inputs.ElementAt(i);
                rightValues.Add(input);
                current += Math.Abs(input.Value);
                i++;
            }

            while (i < Inputs.Count())
            {
                var input = Inputs.ElementAt(i);
                leftValues.Add(input);
                i++;
            }

            leftValues.Reverse();

            DrawCaptionElements(canvas, width, height, rightValues, false);
            DrawCaptionElements(canvas, width, height, leftValues, true);
        }
        public override void DrawContent(SKCanvas canvas, int width, int height)
        {
            if (Inputs != null)
            {
                DrawCaption(canvas, width, height);
                using (new SKAutoCanvasRestore(canvas))
                {
                    canvas.Translate(width / 2, height / 2);
                    var sumValue = Inputs.Sum(x => Math.Abs(x.Value));
                    var radius   = (Math.Min(width, height) - (2 * Margin)) / 2;

                    var start = 0.0f;
                    for (int i = 0; i < Inputs.Count(); i++)
                    {
                        var input = Inputs.ElementAt(i);
                        var end   = start + ((Math.Abs(input.Value) / sumValue) * AnimationProgress);

                        // Sector
                        var path = RadialHelpers.CreateSectorPath(start, end, radius, radius * HoleRadius);
                        using (var paint = new SKPaint
                        {
                            Style = SKPaintStyle.Fill,
                            Color = input.Color,
                            IsAntialias = true,
                        })
                        {
                            canvas.DrawPath(path, paint);
                        }

                        start = end;
                    }
                }
            }
        }
Beispiel #7
0
        public void Validate(ValidationResult result)
        {
            if (Timers == null)
            {
                Timers = new List <Timer>();
            }


            if (Inputs.Select(param => param.Key).Distinct().Count() != Inputs.Count())
            {
                result.AddUserError("Duplicate Keys found on Inputs.");
            }
            if (Attributes.Select(param => param.Key).Distinct().Count() != Attributes.Count())
            {
                result.AddUserError("Duplicate Keys found on Attributes.");
            }
            if (InputCommands.Select(param => param.Key).Distinct().Count() != InputCommands.Count())
            {
                result.AddUserError("Duplicate Keys found on Input Commands.");
            }
            if (StateMachines.Select(param => param.Key).Distinct().Count() != StateMachines.Count())
            {
                result.AddUserError("Duplicate Keys found on State Machines.");
            }
            if (OutputCommands.Select(param => param.Key).Distinct().Count() != OutputCommands.Count())
            {
                result.AddUserError("Duplicate Keys found on Output Commands.");
            }
            if (Timers.Select(param => param.Key).Distinct().Count() != Timers.Count())
            {
                result.AddUserError("Duplicate Keys found on Timers.");
            }

            foreach (var input in Inputs)
            {
                input.Validate(this, result);
            }
            foreach (var attribute in Attributes)
            {
                attribute.Validate(this, result);
            }
            foreach (var inputCommand in InputCommands)
            {
                inputCommand.Validate(this, result);
            }
            foreach (var stateMachine in StateMachines)
            {
                stateMachine.Validate(this, result);
            }
            foreach (var outputCommand in OutputCommands)
            {
                outputCommand.Validate(this, result);
            }
            foreach (var timer in Timers)
            {
                timer.Validate(this, result);
            }
        }
Beispiel #8
0
        public int Guess()
        {
            double sum = 0;

            for (int i = 0; i < Inputs.Count(); i++)
            {
                sum += Inputs[i] * Weights[i][i];
            }
            return(Math.Sign(sum));
        }
Beispiel #9
0
        private void DrawCaption(SKCanvas canvas, int width, int height)
        {
            var rightValues = Inputs.Take(Inputs.Count() / 2).ToList <ChartInput>();
            var leftValues  = Inputs.Skip(rightValues.Count()).ToList <ChartInput>();

            leftValues.Reverse();

            DrawCaptionElements(canvas, width, height, rightValues, false);
            DrawCaptionElements(canvas, width, height, leftValues, true);
        }
Beispiel #10
0
        public bool successResult()
        {
            int count = 0;

            foreach (var input in Inputs)
            {
                if (M[input.CorrespondingOutput.GetValueOrDefault(), 0] == input.Value)
                {
                    count++;
                }
            }
            return(count == Inputs.Count());
        }
Beispiel #11
0
        // method to export true vs estimated (NN) output for a particular output index
        public void TestNetworkOutputValidationTimeDomain(int outputIndex)
        {
            double[,] trueOutput   = new double[Inputs.Count() - 1, 2];
            double[,] nnOutput     = new double[Inputs.Count() - 1, 2];
            double[,] bestNNOutput = new double[Inputs.Count() - 1, 2];
            double[] tempInput1 = new double[NumInputs];
            double[] tempInput2 = new double[NumInputs];
            double[] tempOutput1;
            double[] tempOutput2;

            for (int i = 0; i < Inputs.Count() - 1; i++)
            {
                if (i == 0)
                {
                    for (int j = 0; j < NumInputs; j++)
                    {
                        tempInput1[j] = Inputs[0][j];
                        tempInput2[j] = Inputs[0][j];
                    }
                }
                trueOutput[i, 0] = Convert.ToDouble(i);
                trueOutput[i, 1] = Outputs[i][outputIndex];
                nnOutput[i, 0]   = Convert.ToDouble(i);
                tempOutput1      = NeuralNet.ForwardPass(tempInput1);
                nnOutput[i, 1]   = tempOutput1[outputIndex];
                for (int j = 0; j < NumOutputs; j++)
                {
                    tempInput1[j] = tempOutput1[j];
                }
                for (int j = NumOutputs; j < NumInputs; j++)
                {
                    tempInput1[j] = Inputs[i][j];
                }
                bestNNOutput[i, 0] = Convert.ToDouble(i);
                tempOutput2        = BestNetwork.ForwardPass(tempInput2);
                bestNNOutput[i, 1] = tempOutput2[outputIndex];
                for (int j = 0; j < NumOutputs; j++)
                {
                    tempInput2[j] = tempOutput2[j];
                }
                for (int j = NumOutputs; j < NumInputs; j++)
                {
                    tempInput2[j] = Inputs[i][j];
                }
            }
            DE.Export2DArray(trueOutput, "true_data_validation");
            DE.Export2DArray(nnOutput, "nn_output_validation");
            DE.Export2DArray(bestNNOutput, "best_nn_output_validation");
        }
Beispiel #12
0
        // method to export true vs estimated (NN) output for a particular output index
        public void TestNetworkOutputTraining(int inputIndex, int outputIndex)
        {
            double[,] trueOutput   = new double[Inputs.Count(), 2];
            double[,] nnOutput     = new double[Inputs.Count(), 2];
            double[,] bestNNOutput = new double[Inputs.Count(), 2];

            for (int i = 0; i < Inputs.Count(); i++)
            {
                trueOutput[i, 0]   = Inputs[i][inputIndex];
                trueOutput[i, 1]   = Outputs[i][outputIndex];
                nnOutput[i, 0]     = Inputs[i][inputIndex];
                nnOutput[i, 1]     = NeuralNet.ForwardPass(Inputs[i])[outputIndex];
                bestNNOutput[i, 0] = Inputs[i][inputIndex];
                bestNNOutput[i, 1] = BestNetwork.ForwardPass(Inputs[i])[outputIndex];
            }
            DE.Export2DArray(trueOutput, "true_data_training");
            DE.Export2DArray(nnOutput, "nn_output_training");
            DE.Export2DArray(bestNNOutput, "best_nn_output_training");
        }
Beispiel #13
0
        // method to find MSE from training set
        public double MSETraining()
        {
            int    numPoints  = Inputs.Count();
            double numPointsD = Convert.ToDouble(numPoints);
            double error      = 0.0;
            double singleError;

            double[] networkOutput;
            for (int i = 0; i < numPoints; i++)
            {
                singleError   = 0.0;
                networkOutput = NeuralNet.ForwardPass(Inputs[i]);
                for (int j = 0; j < networkOutput.GetLength(0); j++)
                {
                    singleError += (networkOutput[j] - Outputs[i][j]) * (networkOutput[j] - Outputs[i][j]);
                }
                error += singleError;
            }
            error /= numPointsD;
            return(Math.Sqrt(error));
        }
Beispiel #14
0
        public override void DrawContent(SKCanvas canvas, int width, int height)
        {
            if (Inputs != null)
            {
                DrawCaption(canvas, width, height);

                var sumValue    = Inputs.Sum(x => Math.Abs(x.Value));
                var radius      = (Math.Min(width, height) - (2 * Margin)) / 2;
                var cx          = width / 2;
                var cy          = height / 2;
                var lineWidth   = (LineSize < 0) ? (radius / ((Inputs.Count() + 1) * 2)) : LineSize;
                var radiusSpace = lineWidth * 2;

                for (int i = 0; i < Inputs.Count(); i++)
                {
                    var input       = Inputs.ElementAt(i);
                    var inputRadius = (i + 1) * radiusSpace;
                    DrawGaugeArea(canvas, input, inputRadius, cx, cy, lineWidth);
                    DrawGauge(canvas, input, inputRadius, cx, cy, lineWidth);
                }
            }
        }
Beispiel #15
0
        // method to train network
        public List <double[]> TrainNetwork()
        {
            InitializeNetwork();
            InitializeBestNetwork();
            double[] mseDataValidation = new double[Episodes];
            double[] mseDataTraining   = new double[Episodes];

            for (int ep = 0; ep < Episodes; ep++)
            {
                if (Shuffle == ToggleShuffle.yes)
                {
                    ShuffleData();
                }

                for (int dp = 0; dp < Inputs.Count(); dp++)
                {
                    Backprop(TrainingInputs[dp], TrainingOutputs[dp]);
                }

                mseDataValidation[ep]   = MSEValidation();
                mseDataTraining[ep]     = MSETraining();
                NeuralNet.MSETraining   = mseDataTraining[ep];
                NeuralNet.MSEValidation = mseDataValidation[ep];

                if (mseDataValidation[ep] < BestNetwork.MSEValidation)
                {
                    BestNetwork.WeightMat1    = NeuralNet.WeightMat1;
                    BestNetwork.WeightMat2    = NeuralNet.WeightMat2;
                    BestNetwork.MSETraining   = NeuralNet.MSETraining;
                    BestNetwork.MSEValidation = NeuralNet.MSEValidation;
                }
            }
            List <double[]> output = new List <double[]>();

            output.Add(mseDataTraining);
            output.Add(mseDataValidation);
            return(output);
        }
Beispiel #16
0
        public override void DrawContent(SKCanvas canvas, int width, int height)
        {
            var total = Inputs?.Count() ?? 0;

            if (total > 0)
            {
                var captionHeight = Inputs.Max(x =>
                {
                    var result = 0.0f;

                    var hasLabel      = !string.IsNullOrEmpty(x.Label);
                    var hasValueLabel = !string.IsNullOrEmpty(x.DisplayValue);
                    if (hasLabel || hasValueLabel)
                    {
                        var hasOffset     = hasLabel && hasValueLabel;
                        var captionMargin = LabelTextSize * 0.60f;
                        var space         = hasOffset ? captionMargin : 0;

                        if (hasLabel)
                        {
                            result += LabelTextSize;
                        }

                        if (hasValueLabel)
                        {
                            result += LabelTextSize;
                        }
                    }

                    return(result);
                });

                var center     = new SKPoint(width / 2, height / 2);
                var radius     = ((Math.Min(width, height) - (2 * Margin)) / 2) - captionHeight;
                var rangeAngle = (float)((Math.PI * 2) / total);
                var startAngle = (float)Math.PI;

                var nextInput = Inputs.First();
                var nextAngle = startAngle;
                var nextPoint = GetPoint(nextInput.Value * AnimationProgress, center, nextAngle, radius);

                DrawBorder(canvas, center, radius);

                using (var clip = new SKPath())
                {
                    clip.AddCircle(center.X, center.Y, radius);

                    for (int i = 0; i < total; i++)
                    {
                        var angle = nextAngle;
                        var input = nextInput;
                        var point = nextPoint;

                        var nextIndex = (i + 1) % total;
                        nextAngle = startAngle + (rangeAngle * nextIndex);
                        nextInput = Inputs.ElementAt(nextIndex);
                        nextPoint = GetPoint(nextInput.Value * AnimationProgress, center, nextAngle, radius);

                        canvas.Save();
                        canvas.ClipPath(clip);

                        // Border center bars
                        using (var paint = new SKPaint()
                        {
                            Style = SKPaintStyle.Stroke,
                            StrokeWidth = BorderLineSize,
                            Color = BorderLineColor,
                            IsAntialias = true,
                        })
                        {
                            var borderPoint = GetPoint(MaxValue, center, angle, radius);
                            canvas.DrawLine(point.X, point.Y, borderPoint.X, borderPoint.Y, paint);
                        }

                        // Values points and lines
                        using (var paint = new SKPaint()
                        {
                            Style = SKPaintStyle.Stroke,
                            StrokeWidth = BorderLineSize,
                            Color = input.Color.WithAlpha((byte)(input.Color.Alpha * 0.75f * AnimationProgress)),
                            PathEffect = SKPathEffect.CreateDash(new[] { BorderLineSize, BorderLineSize * 2 }, 0),
                            IsAntialias = true,
                        })
                        {
                            var amount = Math.Abs(input.Value - AbsoluteMinimum) / ValueRange;
                            canvas.DrawCircle(center.X, center.Y, radius * amount, paint);
                        }

                        canvas.DrawGradientLine(center, input.Color.WithAlpha(0), point, input.Color.WithAlpha((byte)(input.Color.Alpha * 0.75f)), LineSize);
                        canvas.DrawGradientLine(point, input.Color, nextPoint, nextInput.Color, LineSize);
                        canvas.DrawPoint(point, input.Color, PointSize, PointMode);

                        canvas.Restore();

                        // Labels
                        var labelPoint = new SKPoint(0, radius + LabelTextSize + (PointSize / 2));
                        var rotation   = SKMatrix.MakeRotation(angle);
                        labelPoint = center + rotation.MapPoint(labelPoint);
                        var alignment = SKTextAlign.Left;

                        if ((Math.Abs(angle - (startAngle + Math.PI)) < Epsilon) || (Math.Abs(angle - Math.PI) < Epsilon))
                        {
                            alignment = SKTextAlign.Center;
                        }
                        else if (angle > (float)(startAngle + Math.PI))
                        {
                            alignment = SKTextAlign.Right;
                        }

                        canvas.DrawCaptionLabels(input.Label, input.TextColor, input.DisplayValue, input.Color.WithAlpha((byte)(255 * AnimationProgress)), LabelTextSize, labelPoint, alignment, base.Typeface);
                    }
                }
            }
        }
Beispiel #17
0
            public ExitCode Run()
            {
                try
                {
                    int opcode = 0;

                    while (opcode != 99)
                    {
                        opcode = Instructions[PC] % 100;
                        var p1mode = (Instructions[PC] / 100) % 10;
                        var p2mode = (Instructions[PC] / 1000) % 10;
                        var p3mode = (Instructions[PC] / 10000) % 10;

                        int GetValue(int m, int v) => m != 0 ? v : Instructions[v];

                        switch (opcode)
                        {
                        case 1:
                            Instructions[Instructions[PC + 3]] = GetValue(p1mode, Instructions[PC + 1]) + GetValue(p2mode, Instructions[PC + 2]);
                            PC += 4;
                            break;

                        case 2:
                            Instructions[Instructions[PC + 3]] = GetValue(p1mode, Instructions[PC + 1]) * GetValue(p2mode, Instructions[PC + 2]);
                            PC += 4;
                            break;

                        case 3:
                            if (Inputs.Count() > 0)
                            {
                                Instructions[Instructions[PC + 1]] = Inputs.Dequeue();
                                PC += 2;
                            }
                            else
                            {
                                return(ExitCode.NEED_INPUT);
                            }
                            break;

                        case 4:
                            Outputs.Enqueue(GetValue(p1mode, Instructions[PC + 1]));
                            //Console.WriteLine($"Enqueued {Outputs.Peek()}");
                            PC += 2;
                            break;

                        case 5:
                            PC = GetValue(p1mode, Instructions[PC + 1]) != 0 ? GetValue(p2mode, Instructions[PC + 2]) : (PC + 3);
                            break;

                        case 6:
                            PC = GetValue(p1mode, Instructions[PC + 1]) == 0 ? GetValue(p2mode, Instructions[PC + 2]) : (PC + 3);
                            break;

                        case 7:
                            Instructions[Instructions[PC + 3]] = GetValue(p1mode, Instructions[PC + 1]) < GetValue(p2mode, Instructions[PC + 2]) ? 1 : 0;
                            PC += 4;
                            break;

                        case 8:
                            Instructions[Instructions[PC + 3]] = GetValue(p1mode, Instructions[PC + 1]) == GetValue(p2mode, Instructions[PC + 2]) ? 1 : 0;
                            PC += 4;
                            break;

                        case 99:
                            break;
                        }
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("Invalid Program");
                    return(ExitCode.EXCEPTION);
                }

                return(ExitCode.HALT);
            }
Beispiel #18
0
 public override bool Output() =>
 Inputs.Count(input => input.Output()) % 2 == 1;
Beispiel #19
0
 protected override bool Check()
 {
     return(Inputs.Count(c => c.IsActive) == 1);
 }