Ejemplo n.º 1
0
 private void IncreaseMainBranchWeights(BranchSet branchSet, IEnumerable <EBranch> branches, int increment)
 {
     foreach (var branch in branches)
     {
         _mainBranchWeights[branchSet][branch] += increment;
     }
 }
Ejemplo n.º 2
0
        private void AddIfMissing(BranchSet branchSet, IEnumerable <EBranch> branches)
        {
            if (!_mainBranchOccurrences.ContainsKey(branchSet))
            {
                var internalDictionary = new Dictionary <EBranch, int>();

                foreach (var branch in branches)
                {
                    internalDictionary.Add(branch, EInteger.Number.Zero);
                }

                _mainBranchOccurrences.Add(branchSet, internalDictionary);
            }

            if (!_mainBranchWeights.ContainsKey(branchSet))
            {
                var internalDictionary = new Dictionary <EBranch, int>();

                foreach (var branch in branches)
                {
                    internalDictionary.Add(branch, EInteger.Number.One);
                }

                _mainBranchWeights.Add(branchSet, internalDictionary);
            }
        }
Ejemplo n.º 3
0
        private void ResetMainBranchWeights(BranchSet branchSet, IEnumerable <EBranch> branches)
        {
            var mainBranchWeights = _mainBranchWeights[branchSet];

            foreach (var branch in branches)
            {
                mainBranchWeights[branch] = EInteger.Number.One;
            }
        }
Ejemplo n.º 4
0
        public void Constructor_IgnoresInvalidBranches()
        {
            // arrange
            var branches      = typeof(EBranch).GetEnumerationItems <EBranch>();
            var validBranches = branches.Where(branch => branch.IsValid());

            // act
            var branchSet = new BranchSet(branches);

            // assert
            branchSet.Branches.Should().BeEquivalentTo(validBranches);
        }
		internal override void EmitBranch(CodeGen g, BranchSet branchSet, Label label)
		{
			IStandardOperation stdOp = af.Method as IStandardOperation;
			if (op.branchOp == 0 || stdOp == null)
			{
				base.EmitBranch(g, branchSet, label);
				return;
			}

			af.EmitArgs(g, operands);
			g.IL.Emit(branchSet.Get(op.branchOp, stdOp.IsUnsigned), label);
		}
Ejemplo n.º 6
0
        public override void EmitBranch(CodeGen g, BranchSet branchSet, Label label)
        {
            IStandardOperation stdOp = af.Method as IStandardOperation;

            if (op.branchOp == 0 || stdOp == null)
            {
                base.EmitBranch(g, branchSet, label);
                return;
            }

            af.EmitArgs(g, operands);
            g.IL.Emit(branchSet.Get(op.branchOp, stdOp.IsUnsigned), label);
        }
Ejemplo n.º 7
0
        public void Constructor_1_Branch()
        {
            // arrange
            var branches = new List <EBranch> {
                EBranch.Army
            };

            // act
            var branchSet = new BranchSet(branches);

            // assert
            branchSet.Branches.Should().BeEquivalentTo(branches);
        }
Ejemplo n.º 8
0
        internal override void EmitBranch(CodeGen g, BranchSet branchSet, Label label)
        {
            PrepareAf(g.TypeMapper);
            IStandardOperation stdOp = _af.Method as IStandardOperation;

            if (_op.BranchOp == 0 || stdOp == null)
            {
                base.EmitBranch(g, branchSet, label);
                return;
            }

            _af.EmitArgs(g, _operands);
            g.IL.Emit(branchSet.Get(_op.BranchOp, stdOp.IsUnsigned), label);
        }
Ejemplo n.º 9
0
        public void GetHashCode_EmptySets_ShouldBeEqual()
        {
            // arrange
            var branchesA  = new List <EBranch>();
            var branchesB  = new List <EBranch>();
            var branchSetA = new BranchSet(branchesA);
            var branchSetB = new BranchSet(branchesB);

            // act
            var setsAreEqual = branchSetA.GetHashCode() == branchSetB.GetHashCode();

            // assert
            setsAreEqual.Should().BeTrue();
        }
Ejemplo n.º 10
0
        private EBranch GetRandomMainBranch(BranchSet branchSet, IEnumerable <EBranch> branches)
        {
            var scaleStringBuilder = new StringBuilder();

            foreach (var branch in branches)
            {
                scaleStringBuilder.Append(string.Empty.PadLeft(_mainBranchWeights[branchSet][branch], branch.CastTo <int>().ToString().First()));
            }

            var scaleString = scaleStringBuilder.ToString();

            return(int
                   .Parse(scaleString[_generator.Next(scaleString.Count())].ToString())
                   .CastTo <EBranch>()
                   );
        }
Ejemplo n.º 11
0
        public void GetHashCode_DifferentBranchOrder_ShouldBeEqual()
        {
            // arrange
            var branchesA = new List <EBranch> {
                EBranch.Army, EBranch.Aviation
            };
            var branchesB = new List <EBranch> {
                EBranch.Aviation, EBranch.Army
            };
            var branchSetA = new BranchSet(branchesA);
            var branchSetB = new BranchSet(branchesB);

            // act
            var setsAreEqual = branchSetA.GetHashCode() == branchSetB.GetHashCode();

            // assert
            setsAreEqual.Should().BeTrue();
        }
Ejemplo n.º 12
0
        private EBranch GetRandomMainBranch(BranchSet branchSet, IEnumerable <EBranch> branches)
        {
            var scaleStringBuilder = new StringBuilder();

            foreach (var branch in branches)
            {
                var weight       = _mainBranchWeights[branchSet][branch];
                var branchCode   = branch.GetSingleDigitCode();
                var paddedString = string.Empty.PadLeft(weight, branchCode.ToString().First());

                scaleStringBuilder.Append(paddedString);
            }

            var scaleString        = scaleStringBuilder.ToString();
            var selectedBranchCode = int.Parse(scaleString[_generator.Next(scaleString.Count())].ToString());

            return(EBranchExtensions.FromSingleDigitCode(selectedBranchCode));
        }
Ejemplo n.º 13
0
        protected override T GetRandomCore <T>(IEnumerable <T> items, ERandomisationStep randomisationStep)
        {
            if (typeof(T) == typeof(EBranch) && randomisationStep == ERandomisationStep.MainBranchWhenSelectingByCategories)
            {
                var branches  = items.OfType <EBranch>();
                var branchSet = new BranchSet(branches);

                AddIfMissing(branchSet, branches);

                var mainBranchOccurrences = _mainBranchOccurrences[branchSet];
                var selectedBranch        = EBranch.None;

                if (mainBranchOccurrences.Values.AllEqual())
                {
                    selectedBranch = base.GetRandomCore(items, randomisationStep).CastTo <EBranch>();
                }
                else
                {
                    var mostOccurences            = mainBranchOccurrences.Max(keyValuePair => keyValuePair.Value);
                    var leastOccurrences          = mainBranchOccurrences.Min(keyValuePair => keyValuePair.Value);
                    var leastOccurredMainBranches = mainBranchOccurrences.GetKeyWhereValue(occurrenceCount => occurrenceCount == leastOccurrences);

                    IncreaseMainBranchWeights(branchSet, leastOccurredMainBranches, mostOccurences - leastOccurrences);

                    selectedBranch = GetRandomMainBranch(branchSet, branches);

                    ResetMainBranchWeights(branchSet, branches);
                }

                mainBranchOccurrences[selectedBranch] += EInteger.Number.One;

                return(selectedBranch.CastTo <T>());
            }
            else
            {
                return(base.GetRandomCore(items, randomisationStep));
            }
        }
 internal override void EmitBranch(CodeGen g, BranchSet branchSet, Label label)
 {
     _operand.EmitBranch(g, branchSet, label);
 }