public IRule CopyRule(int ruleIndex, Strategy target) { IRule rule = rules[ruleIndex]; IRule newRule = RuleFactory.GetInstance().CreateRuleInstance(rule.GetType()); //we do not initialize rule here coz when str is added to rep, its //initialize method is called which calles init of its rules //newRule.Initialize(this); rule.CopyState(newRule); target.AddRule(newRule); return(newRule); }
//deep dup - dup rules instances too public Strategy Duplicate() { Strategy result = new Strategy(indicatorRep); foreach (IRule rule in rules) { //newRule contains parameters of rule but no value IRule newRule = RuleFactory.GetInstance().CreateRuleInstance(rule.GetType()); //rule.init will be called upon strategy initialization //newRule.Initialize(this); rule.CopyState(newRule); result.AddRule(newRule); } CopyInfo(result); return(result as Strategy); }
private void addStrategy(int newCount) { int counter = 0; while (true) { if (counter >= newCount) { break; } Strategy s = new Strategy(commonContext.IndicatorRepository); //create a unique strategy int ruleCount = Rand.Random.Next(commonContext.EnvParams.MinRulePerStrategy, commonContext.EnvParams.MaxRulePerStrategy); for (int i = 0; i < ruleCount; i++) { IRule rule = RuleFactory.GetInstance().CreateRandomInstance(); s.AddRule(rule); } int tryCounter = 0; do { //randomize all rules' states s.Randomize(1); tryCounter++; } while (commonContext.StrategyRepository.AddStrategy(s) == false && tryCounter < commonContext.CommonConfig.MaxTryPerAddStrategy); if (tryCounter == commonContext.CommonConfig.MaxTryPerAddStrategy) { //all strategy choice space is filled break; } counter++; } }
public static BaseRule LoadFromDescription(string desc) { int typeNameIndex = desc.IndexOf("<PARAMS>"); string typeName = desc.Substring(0, typeNameIndex); //parameters are added upon instantiation BaseRule result = RuleFactory.GetInstance().CreateRuleInstance(Type.GetType("GeneticMarket.Logic.Rules." + typeName)) as BaseRule; //we only need to load parameter values int endParamIndex = desc.Length - 1; string paramsString = desc.Substring(typeNameIndex + 8, endParamIndex - typeNameIndex - 8); string[] paramsKeyValues = paramsString.Split('|'); for (int i = 0; i < paramsKeyValues.Length; i++) { if (paramsKeyValues[i].Length == 0) { continue; } string[] parts = paramsKeyValues[i].Split('='); string key = parts[0]; string value = parts[1]; IRuleParameter argObject = result.parameters[key]; bool isDouble = (argObject is DoubleRuleParameter); if (isDouble) { result[key] = double.Parse(value); } else { result[key] = int.Parse(value); } } return(result); }