示例#1
0
        public static void AssemblyInit()
        {
            Context = new FuzzyContext(new DbContextOptionsBuilder <FuzzyContext>()
                                       .UseSqlite("DataSource=:memory:").Options);

            Context.ChangeTracker.AutoDetectChangesEnabled = false;

            Context.Database.OpenConnection();
            Context.Database.EnsureCreated();

            var role1 = Context.Roles.Add(new Role()
            {
                Name = "Admin", Status = (int)AccountType.Admin
            });
            var role2 = Context.Roles.Add(new Role()
            {
                Name = "User", Status = (int)AccountType.Engineer
            });

            Context.SaveChanges();

            //role1.State = EntityState.Deleted;
            //role2.State = EntityState.Deleted;

            Accounts = new AccountService(Context);
        }
示例#2
0
        public DateTimeExtensionsTest()
        {
            spec = Substitute.ForPartsOf <FuzzyRange <DateTime> >(fuzzy, DateTime.MinValue, DateTime.MaxValue);

            FuzzyContext.Set(value, spec);
            ConfiguredCall arrange = fuzzy.Build(spec).Returns(newValue);
        }
示例#3
0
        public StringExtensionsTest()
        {
            spec = new FuzzyString(fuzzy);

            FuzzyContext.Set(value, spec);
            ConfiguredCall arrange = fuzzy.Build(spec).Returns(newValue);
        }
示例#4
0
        public Int16ExtensionsTest()
        {
            spec = Substitute.ForPartsOf <FuzzyRange <short> >(fuzzy, short.MinValue, short.MaxValue);

            FuzzyContext.Set(value, spec);
            ConfiguredCall arrange = fuzzy.Build(spec).Returns(newValue);
        }
示例#5
0
        public static T Maximum <T>(this T value, T maximum) where T : struct, IComparable <T>
        {
            FuzzyRange <T> spec = FuzzyContext.Get <T, FuzzyRange <T> >(value);

            spec.Maximum = maximum;
            return(spec);
        }
示例#6
0
        public static string LettersOrDigits(this string value)
        {
            FuzzyString spec = FuzzyContext.Get <string, FuzzyString>(value);

            spec.Characters = spec.Characters.Where(c => char.IsLetterOrDigit(c)).ToArray();
            return(spec);
        }
示例#7
0
 void ArrangeBuildOfFuzzyRange <T>(Func <T> generate) where T : struct, IComparable <T>
 {
     ConfiguredCall arrange = fuzzy.Build(Arg.Any <FuzzyRange <T> >())
                              .Returns(call => {
         T initial = generate();
         FuzzyContext.Set(initial, (FuzzyRange <T>)call[0]);
         return(initial);
     });
 }
示例#8
0
        public IComparableExtensionsTest() {
            value = new TestStruct(random.Next());
            spec = Substitute.ForPartsOf<FuzzyRange<TestStruct>>(fuzzy, new TestStruct(int.MinValue), new TestStruct(int.MaxValue));
            minimum = new TestStruct(int.MinValue + random.Next() % short.MaxValue);
            maximum = new TestStruct(int.MaxValue - random.Next() % short.MaxValue);
            newValue = new TestStruct(random.Next());

            FuzzyContext.Set(value, spec);
            ConfiguredCall arrange = fuzzy.Build(spec).Returns(newValue);
        }
示例#9
0
文件: Fuzz.cs 项目: olegsych/fuzzy
        T IFuzz.Build <T>(Fuzzy <T> spec)
        {
            if (spec is null)
            {
                throw new System.ArgumentNullException(nameof(spec));
            }
            T value = spec.Build();

            FuzzyContext.Set(value, spec);
            return(value);
        }
示例#10
0
            public LinguisticValue Infer(FuzzyCalculator calc, FuzzyContext context)
            {
                List <FuzzyValue> inputValues = new List <FuzzyValue>(inputVariables.Count);

                foreach (FuzzySet set in inputVariables)
                {
                    inputValues.Add(context.fuzzyInputValues[set]);
                }
                FuzzyValue membership = calc.and(inputValues.ToArray());

                LinguisticValue result = new LinguisticValue();

                foreach (FuzzySet set in outputVariables)
                {
                    result.AddSet(set, membership);
                }
                return(result);
            }
示例#11
0
 public AccountService(FuzzyContext debugContext)
 {
     _unitOfWork = new UnitOfWork <FuzzyContext>(debugContext);
 }
            private void Init()
            {
                calculator = new FuzzyCalculator();
                context    = new FuzzyContext();
                inference  = new FuzzyInference(calculator, context);

                LinguisticVariable agentHealth       = new LinguisticVariable();
                FuzzySet           lowAgentHealth    = new FuzzySet(FuzzyContext.Variable.AgentHealth, "lowAgentHealth", new Trapezoid(0.0f, 0.0f, 30.0f, 60.0f));
                FuzzySet           mediumAgentHealth = new FuzzySet(FuzzyContext.Variable.AgentHealth, "mediumAgentHealth", new Trapezoid(30.0f, 60.0f, 60.0f, 80.0f));
                FuzzySet           highAgentHealth   = new FuzzySet(FuzzyContext.Variable.AgentHealth, "highAgentHealth", new Trapezoid(60.0f, 80.0f, 100.0f, 100.0f));

                agentHealth.AddSets(lowAgentHealth, mediumAgentHealth, highAgentHealth);

                LinguisticVariable enemyHealth       = new LinguisticVariable();
                FuzzySet           lowEnemyHealth    = new FuzzySet(FuzzyContext.Variable.EnemyHealth, "lowEnemyHealth", new Trapezoid(0.0f, 0.0f, 30.0f, 60.0f));
                FuzzySet           mediumEnemyHealth = new FuzzySet(FuzzyContext.Variable.EnemyHealth, "mediumEnemyHealth", new Trapezoid(30.0f, 60.0f, 60.0f, 80.0f));
                FuzzySet           highEnemyHealth   = new FuzzySet(FuzzyContext.Variable.EnemyHealth, "highEnemyHealth", new Trapezoid(60.0f, 80.0f, 100.0f, 100.0f));

                enemyHealth.AddSets(lowEnemyHealth, mediumEnemyHealth, highEnemyHealth);

                LinguisticVariable enemyVisibility   = new LinguisticVariable();
                FuzzySet           enemyIsVisible    = new FuzzySet(FuzzyContext.Variable.EnemyVisibility, "enemyIsVisible", new Singleton(1.0f));
                FuzzySet           enemyIsNotVisible = new FuzzySet(FuzzyContext.Variable.EnemyVisibility, "enemyIsNotVisible", new Singleton(0.0f));

                enemyVisibility.AddSets(enemyIsVisible, enemyIsNotVisible);

                LinguisticVariable distanceToEnemy = new LinguisticVariable();
                FuzzySet           closeDistance   = new FuzzySet(FuzzyContext.Variable.DistanceToEnemy, "closeDistance", new Trapezoid(0.0f, 0.0f, 7.0f, 10.0f));
                FuzzySet           mediumDistance  = new FuzzySet(FuzzyContext.Variable.DistanceToEnemy, "mediumDistance", new Trapezoid(7.0f, 10.0f, 14.0f, 18.0f));
                FuzzySet           farDistance     = new FuzzySet(FuzzyContext.Variable.DistanceToEnemy, "farDistance", new Trapezoid(14.0f, 18.0f, 100.0f, 100.0f));

                distanceToEnemy.AddSets(closeDistance, mediumDistance, farDistance);

                LinguisticVariable distanceWeight       = new LinguisticVariable();
                FuzzySet           lowDistanceWeight    = new FuzzySet(FuzzyContext.Variable.DistanceWeight, "lowDistanceWeight", new Trapezoid(0.0f, 0.0f, 0.2f, 0.4f));
                FuzzySet           mediumDistanceWeight = new FuzzySet(FuzzyContext.Variable.DistanceWeight, "mediumDistanceWeight", new Trapezoid(0.2f, 0.4f, 0.6f, 0.8f));
                FuzzySet           highDistanceWeight   = new FuzzySet(FuzzyContext.Variable.DistanceWeight, "highDistanceWeight", new Trapezoid(0.6f, 0.8f, 1.0f, 1.0f));

                distanceWeight.AddSets(lowDistanceWeight, mediumDistanceWeight, highDistanceWeight);

                LinguisticVariable inCoverWeight       = new LinguisticVariable();
                FuzzySet           lowInCoverWeight    = new FuzzySet(FuzzyContext.Variable.InCoverWeight, "lowInCoverWeight", new Trapezoid(0.0f, 0.0f, 0.2f, 0.4f));
                FuzzySet           mediumInCoverWeight = new FuzzySet(FuzzyContext.Variable.InCoverWeight, "mediumInCoverWeight", new Trapezoid(0.2f, 0.4f, 0.6f, 0.8f));
                FuzzySet           highInCoverWeight   = new FuzzySet(FuzzyContext.Variable.InCoverWeight, "highInCoverWeight", new Trapezoid(0.6f, 0.8f, 1.0f, 1.0f));

                inCoverWeight.AddSets(lowInCoverWeight, mediumInCoverWeight, highInCoverWeight);

                LinguisticVariable behindCoverWeight       = new LinguisticVariable();
                FuzzySet           lowBehindCoverWeight    = new FuzzySet(FuzzyContext.Variable.BehindCoverWeight, "lowBehindCoverWeight", new Trapezoid(0.0f, 0.0f, 0.2f, 0.4f));
                FuzzySet           mediumBehindCoverWeight = new FuzzySet(FuzzyContext.Variable.BehindCoverWeight, "mediumBehindCoverWeight", new Trapezoid(0.2f, 0.4f, 0.6f, 0.8f));
                FuzzySet           highBehindCoverWeight   = new FuzzySet(FuzzyContext.Variable.BehindCoverWeight, "highBehindCoverWeight", new Trapezoid(0.6f, 0.8f, 1.0f, 1.0f));

                behindCoverWeight.AddSets(lowBehindCoverWeight, mediumBehindCoverWeight, highBehindCoverWeight);

                LinguisticVariable behindWallWeight       = new LinguisticVariable();
                FuzzySet           lowBehindWallWeight    = new FuzzySet(FuzzyContext.Variable.BehindWallWeight, "lowBehindWallWeight", new Trapezoid(0.0f, 0.0f, 0.2f, 0.4f));
                FuzzySet           mediumBehindWallWeight = new FuzzySet(FuzzyContext.Variable.BehindWallWeight, "mediumBehindWallWeight", new Trapezoid(0.2f, 0.4f, 0.6f, 0.8f));
                FuzzySet           highBehindWallWeight   = new FuzzySet(FuzzyContext.Variable.BehindWallWeight, "highBehindWallWeight", new Trapezoid(0.6f, 0.8f, 1.0f, 1.0f));

                behindWallWeight.AddSets(lowBehindWallWeight, mediumBehindWallWeight, highBehindWallWeight);

                LinguisticVariable healhPackWeight       = new LinguisticVariable();
                FuzzySet           lowHealhPackWeight    = new FuzzySet(FuzzyContext.Variable.HealthPackWeight, "lowHealhPackWeight", new Trapezoid(0.0f, 0.0f, 0.2f, 0.4f));
                FuzzySet           mediumHealhPackWeight = new FuzzySet(FuzzyContext.Variable.HealthPackWeight, "mediumHealhPackWeight", new Trapezoid(0.2f, 0.4f, 0.6f, 0.8f));
                FuzzySet           highHealhPackWeight   = new FuzzySet(FuzzyContext.Variable.HealthPackWeight, "highHealhPackWeight", new Trapezoid(0.6f, 0.8f, 1.0f, 1.0f));

                healhPackWeight.AddSets(lowHealhPackWeight, mediumHealhPackWeight, highHealhPackWeight);

                LinguisticVariable plainSightWeight       = new LinguisticVariable();
                FuzzySet           lowPlainSightWeight    = new FuzzySet(FuzzyContext.Variable.PlainSightWeight, "lowPlainSightWeight", new Trapezoid(0.0f, 0.0f, 0.2f, 0.4f));
                FuzzySet           mediumPlainSightWeight = new FuzzySet(FuzzyContext.Variable.PlainSightWeight, "mediumPlainSightWeight", new Trapezoid(0.2f, 0.4f, 0.6f, 0.8f));
                FuzzySet           highPlainSightWeight   = new FuzzySet(FuzzyContext.Variable.PlainSightWeight, "highPlainSightWeight", new Trapezoid(0.6f, 0.8f, 1.0f, 1.0f));

                plainSightWeight.AddSets(lowPlainSightWeight, mediumPlainSightWeight, highPlainSightWeight);

                LinguisticVariable searchRadius       = new LinguisticVariable();
                FuzzySet           lowSearchRadius    = new FuzzySet(FuzzyContext.Variable.SearchRadius, "lowSearchRadius", new Trapezoid(0.0f, 0.0f, 6.0f, 8.0f));
                FuzzySet           mediumSearchRadius = new FuzzySet(FuzzyContext.Variable.SearchRadius, "mediumSearchRadius", new Trapezoid(6.0f, 8.0f, 12.0f, 14.0f));
                FuzzySet           highSearchRadius   = new FuzzySet(FuzzyContext.Variable.SearchRadius, "highSearchRadius", new Trapezoid(12.0f, 14.0f, 100.0f, 100.0f));

                searchRadius.AddSets(lowSearchRadius, mediumSearchRadius, highSearchRadius);

                context.inputVariables.Add(FuzzyContext.Variable.AgentHealth, agentHealth);
                context.inputVariables.Add(FuzzyContext.Variable.EnemyHealth, enemyHealth);
                context.inputVariables.Add(FuzzyContext.Variable.EnemyVisibility, enemyVisibility);

                context.outputVariables.Add(FuzzyContext.Variable.DistanceToEnemy, distanceToEnemy);
                context.outputVariables.Add(FuzzyContext.Variable.DistanceWeight, distanceWeight);
                context.outputVariables.Add(FuzzyContext.Variable.InCoverWeight, inCoverWeight);
                context.outputVariables.Add(FuzzyContext.Variable.BehindCoverWeight, behindCoverWeight);
                context.outputVariables.Add(FuzzyContext.Variable.BehindWallWeight, behindWallWeight);
                context.outputVariables.Add(FuzzyContext.Variable.HealthPackWeight, healhPackWeight);
                context.outputVariables.Add(FuzzyContext.Variable.PlainSightWeight, plainSightWeight);
                context.outputVariables.Add(FuzzyContext.Variable.SearchRadius, searchRadius);

                inference.AddRule(
                    new FuzzyRule()
                    .In(highAgentHealth).In(highEnemyHealth).In(enemyIsVisible)
                    .Out(mediumDistance).Out(mediumDistanceWeight).Out(lowInCoverWeight).Out(lowBehindCoverWeight)
                    .Out(lowBehindWallWeight).Out(lowHealhPackWeight).Out(highPlainSightWeight).Out(lowSearchRadius)
                    );
                inference.AddRule(
                    new FuzzyRule()
                    .In(highAgentHealth).In(mediumEnemyHealth).In(enemyIsVisible)
                    .Out(closeDistance).Out(highDistanceWeight).Out(lowInCoverWeight).Out(lowBehindCoverWeight)
                    .Out(lowBehindWallWeight).Out(lowHealhPackWeight).Out(highPlainSightWeight).Out(lowSearchRadius)
                    );
                inference.AddRule(
                    new FuzzyRule()
                    .In(highAgentHealth).In(lowEnemyHealth).In(enemyIsVisible)
                    .Out(closeDistance).Out(highDistanceWeight).Out(lowInCoverWeight).Out(lowBehindCoverWeight)
                    .Out(lowBehindWallWeight).Out(lowHealhPackWeight).Out(highPlainSightWeight).Out(lowSearchRadius)
                    );
                inference.AddRule(
                    new FuzzyRule()
                    .In(mediumAgentHealth).In(highEnemyHealth).In(enemyIsVisible)
                    .Out(farDistance).Out(mediumDistanceWeight).Out(highInCoverWeight).Out(mediumBehindCoverWeight)
                    .Out(mediumBehindWallWeight).Out(mediumHealhPackWeight).Out(mediumPlainSightWeight).Out(lowSearchRadius)
                    );
                inference.AddRule(
                    new FuzzyRule()
                    .In(mediumAgentHealth).In(mediumEnemyHealth).In(enemyIsVisible)
                    .Out(mediumDistance).Out(mediumDistanceWeight).Out(mediumInCoverWeight).Out(mediumBehindCoverWeight)
                    .Out(lowBehindWallWeight).Out(mediumHealhPackWeight).Out(mediumPlainSightWeight).Out(lowSearchRadius)
                    );
                inference.AddRule(
                    new FuzzyRule()
                    .In(mediumAgentHealth).In(lowEnemyHealth).In(enemyIsVisible)
                    .Out(closeDistance).Out(highDistanceWeight).Out(mediumInCoverWeight).Out(mediumBehindCoverWeight)
                    .Out(lowBehindWallWeight).Out(mediumHealhPackWeight).Out(lowPlainSightWeight).Out(lowSearchRadius)
                    );
                inference.AddRule(
                    new FuzzyRule()
                    .In(lowAgentHealth).In(highEnemyHealth).In(enemyIsVisible)
                    .Out(farDistance).Out(highDistanceWeight).Out(highInCoverWeight).Out(highBehindCoverWeight)
                    .Out(highBehindWallWeight).Out(highHealhPackWeight).Out(lowPlainSightWeight).Out(highSearchRadius)
                    );
                inference.AddRule(
                    new FuzzyRule()
                    .In(lowAgentHealth).In(mediumEnemyHealth).In(enemyIsVisible)
                    .Out(farDistance).Out(mediumDistanceWeight).Out(mediumInCoverWeight).Out(mediumBehindCoverWeight)
                    .Out(highBehindWallWeight).Out(highHealhPackWeight).Out(lowPlainSightWeight).Out(highSearchRadius)
                    );
                inference.AddRule(
                    new FuzzyRule()
                    .In(lowAgentHealth).In(lowEnemyHealth).In(enemyIsVisible)
                    .Out(mediumDistance).Out(mediumDistanceWeight).Out(highInCoverWeight).Out(highBehindCoverWeight)
                    .Out(highBehindWallWeight).Out(highHealhPackWeight).Out(lowPlainSightWeight).Out(highSearchRadius)
                    );
                inference.AddRule(
                    new FuzzyRule()
                    .In(highAgentHealth).In(enemyIsNotVisible)
                    .Out(closeDistance).Out(lowDistanceWeight).Out(lowInCoverWeight).Out(lowBehindCoverWeight)
                    .Out(lowBehindWallWeight).Out(lowHealhPackWeight).Out(lowPlainSightWeight).Out(highSearchRadius)
                    );
                inference.AddRule(
                    new FuzzyRule()
                    .In(mediumAgentHealth).In(enemyIsNotVisible)
                    .Out(mediumDistance).Out(lowDistanceWeight).Out(lowInCoverWeight).Out(lowBehindCoverWeight)
                    .Out(lowBehindWallWeight).Out(mediumHealhPackWeight).Out(lowPlainSightWeight).Out(highSearchRadius)
                    );
                inference.AddRule(
                    new FuzzyRule()
                    .In(lowAgentHealth).In(enemyIsNotVisible)
                    .Out(farDistance).Out(lowDistanceWeight).Out(lowInCoverWeight).Out(lowBehindCoverWeight)
                    .Out(lowBehindWallWeight).Out(highHealhPackWeight).Out(lowPlainSightWeight).Out(highSearchRadius)
                    );
            }
示例#13
0
 public FuzzyInference(FuzzyCalculator calc, FuzzyContext context)
 {
     this.rules   = new List <FuzzyRule>();
     this.calc    = calc;
     this.context = context;
 }