Self-TrainingAlgorithm我想开发一个针对特定问题的自训练算法。为简单起见,我将其归结为简单的示例。更新:我在下面添加了一个工作解决方案作为这个问题的答案。假设我有一个来自数据库的大量实体列表。每个实体都是同一类型,有4个字节类型的属性。公共类实体{公共字节Prop1{得到;放;}publicbyteProp2{get;放;}publicbyteProp3{get;放;}publicbyteProp4{get;动态测试每个实体的一个或多个属性。这基本上意味着我想针对这种情况测试所有属性的所有可能组合。为了完成这项工作,我为属性创建了一个位掩码。[Flags]publicenumEEntityValues{Undefined=0,Prop1=1,Prop2=2,Prop3=4,Prop4=8,}并添加了获取位掩码最大值的方法。对于此示例,返回15(1+2+4+8)。publicstaticintGetMaxValue()whereT:struct{returnEnum.GetValues(typeof(T)).Cast().Sum();在此阶段,我能够通过一个简单的循环遍历所有属性组合。在第一次迭代的示例中,属性Prop1被测试,第二次迭代Prop2被测试,第三次迭代Prop1和Prop2被测试,等等。for(inti=1;i<=GetMaxValue();i++){EEntityValuesflags=(EEntityValues)i;if(flags.HasSet(EEntityValues.Prop1)){....}}现在让我们将实体放入游戏中。列出实体=GetEntitiesFromDb();for(inti=1;i<=GetMaxValue();i++){EEntityValuesflags=(EEntityValues)i;byteminProp1Value=10,minProp2Value=20,minProp3Value=30,minProp4Value=40;foreach(实体中的实体实体){if(flags.HasSet(EEntityValues.Prop1)&&entity.Prop1>=minProp1Value){....}else{continue;}if(flags.HasSet(EEntityValues.Prop2)&&entityy.Prop2>=minProp2Value){....}else{继续;好吧,如果我的min是静态的,这很好用。现在让我们把它变得更复杂。我们记得在第一次迭代中我们只测试属性Prop1,因为位掩码是1。Prot1的取值范围是0..255。我还为此属性定义了一个最小值,其有效范围为1..255。这个最小值只是跳过foreach循环中实体的过滤器。现在我想测试属性Prop1,我正在提高最低级别。这些测试不是问题的一部分,所以我没有将它们包括在我的样本中。字节minProp1Value=1;while(minProp1Value=minProp1Value){....//测试匹配实体并存储结果}else{continue;}}minProp1Value++;这对于单个属性来说很简单。在第三次迭代中,我必须处理2个属性Prop1和Prop2,因为位掩码是3。字节minProp1Value=1,minProp2Value=1;while(minProp1Value<=255){while(minProp2Value<=255){foreach(实体中的实体实体){....}minProp2Value++;}minProp1Value++;minProp2Value=1;如您所见,在这个阶段,我正在测试每个实体的Prop1和Prop2,以获得不断上升的底部级别。因为我正在处理动态生成的多个属性集,所以我不能将while循环硬编码到我的代码中。这就是为什么我正在寻找一种更智能的解决方案来测试给定属性集(位掩码)的所有可能的最小值组合。休息后,我想出了一个似乎符合我要求的解决方案。限制是所有测试的属性应该是具有相同值范围的相同类型,这对我来说很好,因为所有属性都是抽象的百分比值。顺便说一句,我不确定“自我训练算法”的主题在这里是否有点误导。有几种方法可以实现这样的解决方案,但如果您不知道数据的行为方式以及值的影响,最简单的解决方案是强制所有可能的组合来确定最佳拟合结果。这就是我在这里展示的。不管怎样,出于测试目的,我向我的实体类添加了一个随机数生成器。公共类实体{公共字节Prop1{得到;放;}publicbyteProp2{get;放;}publicbyteProp3{get;放;}publicbyteProp4{get;放;}publicEntity(){Randomrandom=newRandom(Guid.NewGuid().GetHashCode());byte[]bytes=newbyte[4];random.NextBytes(字节);this.Prop1=bytes[0];this.Prop2=bytes[1];这。Prop3=bytes[2];this.Prop4=bytes[3];我的位掩码保持不变。[Flags]publicenumEProperty{Undefined=0,Prop1=1,Prop2=1比我添加一些新的扩展方法来处理我的位掩码。publicstaticclassBitMask{publicstaticintGetMaxValue()whereT:struct{returnEnum.GetValues(typeof(T)).Cast().Sum();}publicstaticintGetTotalCount()whereT:struct{returnEnum.GetValues(typeof(T)).Cast().Count(e=>e>0);}publicstaticintGetFlagCount(thisTmask)whereT:struct{intresult=0,value=(int)(object)mask;while(value!=0){value=value&(value-1);结果++;}返回结果;}publicstaticIEnumerableSplit(thisTmask){intmaskValue=(int)(object)mask;foreach(TflaginEnum.GetValues(typeof(T))){intflagValue=(int)(object)flag;if(0!=(flagValue&maskValue)){yieldreturnflag;}}}}比我写了一个查询生成器publicstaticclassQueryBuilder{publicstaticIEnumerableWhere(thisIEnumerableentities,EProperty[]properties,int[]values){IEnumerableresult=entities.Select(e=>e);for(intindex=0;indexMath.Abs??(e.Prop1)>=value);休息;案例EProperty.Prop2:result=result.Where(e=>Math.Abs??(e.Prop2)>=值);休息;caseEProperty.Prop3:result=result.Where(e=>Math.Abs??(e.Prop3)>=value);休息;案例EProperty.Prop4:result=result.Where(e=>Math.Abs??(e.Prop4)>=value);休息;}}返回结果;}}终于可以开始训练了以上就是C#学习教程:自训练算法分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注---privateconstintmaxThreads=10;私有常量intminValue=0;私有常量intmaxValue=100;私有静态IEnumerable实体;publicstaticvoidMain(string[]args){Console.WriteLine(DateTime.Now.ToLongTimeString());entities=Enumerable.Repeat(newEntity(),10).ToList();动作testCase=RunTestCase;RunSelfTraining(测试用例);Console.WriteLine(DateTime.Now.ToLongTimeString());Console.WriteLine("完成");控制台.Read();}privatestaticvoidRunTestCase(EProperty[]properties,int[]values){foreach(Entityentityinentities.Where(properties,values)){}}privatestaticvoidRunSelfTraining(ActiontestCase)whereT:struct{ParallelOptionsparallelOptions=newParallelOptionsOmaxDegrees{MaxDegree};for(intmaskValue=1;maskValue();maskValue++){Tmask=(T)(object)maskValue;T[]properties=mask.Split().ToArray();变量iations=(int)Math.Pow(maxValue-minValue+1,properties.Length);Parallel.For(1,variations,parallelOptions,variation=>{int[]values=GetVariation(variation,minValue,maxValue,properties.Length).ToArray();testCase.Invoke(properties,values);});}}publicstaticIEnumerableGetVariation(intindex,intminValue,intmaxValue,intcount){index=index-1;intrange=maxValue-minValue+1;for(intj=0;j本文收集于网络,不代表立场,如涉及侵权,请点右联系管理员删除,如需转载请注明出处:
