private TaskSpecBase BuildConcreteTaskSpecInstance <TStateSpace, TActionSpace>( RangeParseResult observations, RangeParseResult actions, Tuple <object, object> reward, double discountFactor, string additionalInformation) { return((TaskSpecBase)typeof(TaskSpec <,>) .MakeGenericType(observations.Type, actions.Type) .GetConstructor(new[] { typeof(IEnumerable <TStateSpace>), typeof(IEnumerable <TStateSpace>), typeof(IEnumerable <TActionSpace>), typeof(IEnumerable <TActionSpace>), typeof(double), typeof(double), typeof(double), typeof(string) }) .Invoke(new object[] { new List <TStateSpace>(observations.MinimumValues.Cast <TStateSpace>()), new List <TStateSpace>(observations.MaximumValues.Cast <TStateSpace>()), new List <TActionSpace>(actions.MinimumValues.Cast <TActionSpace>()), new List <TActionSpace>(actions.MaximumValues.Cast <TActionSpace>()), (double)reward.Item1, (double)reward.Item2, discountFactor, additionalInformation })); }
public TaskSpecBase Parse(string taskSpecString) { IEnumerator <Lexem> tokens = Scan(taskSpecString).GetEnumerator(); string version = ParseVersion(tokens); string problemType = ParseProblemType(tokens); double discountFactor = ParseDiscountFactor(tokens); ExpectNextToken(tokens, "OBSERVATIONS", context: "TaskSpec"); RangeParseResult observations = ParseRangeGroup(tokens, "observations"); ExpectNextToken(tokens, "ACTIONS", context: "TaskSpec"); RangeParseResult actions = ParseRangeGroup(tokens, "actions"); ExpectNextToken(tokens, "REWARDS", context: "TaskSpec"); Tuple <object, object> reward = null; try { reward = ParseRange(tokens, typeof(double), context: "rewards").Single(); } catch (InvalidOperationException e) { throw new InvalidDataException("Expected single reward range.", e); } string additionalInformation = string.Empty; if (OptionalNextToken(tokens, "EXTRA")) { StringBuilder extraString = new StringBuilder(); while (tokens.MoveNext()) { extraString.Append(tokens.Current.Value + tokens.Current.Tail); } additionalInformation = extraString.ToString(); } return(BuildConcreteTaskSpecInstance(observations, actions, reward, discountFactor, additionalInformation)); }
private TaskSpecBase BuildConcreteTaskSpecInstance( RangeParseResult observations, RangeParseResult actions, Tuple <object, object> reward, double discountFactor, string additionalInformation) { return((TaskSpecBase)this .GetType() .GetMethods(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) .Single(m => m.Name == "BuildConcreteTaskSpecInstance" && m.IsGenericMethod) .MakeGenericMethod(observations.Type, actions.Type) .Invoke(this, new object[] { observations, actions, reward, discountFactor, additionalInformation })); }
private RangeParseResult ParseRangeGroupContents(IEnumerator <Lexem> tokens, Type rangeType, string context) { List <object> minimumValues = new List <object>(); List <object> maximumValues = new List <object>(); var range = ParseRange(tokens, rangeType, context: context); minimumValues.AddRange(range.Select(r => r.Item1)); maximumValues.AddRange(range.Select(r => r.Item2)); if (tokens.Current.NextLexem != null) { RangeParseResult nextRange = null; if (tokens.Current.NextLexem.Value == "(") { nextRange = ParseRangeGroupContents(tokens, rangeType, context); } else if (RangeTypes.Contains(tokens.Current.NextLexem.Value)) { nextRange = ParseRangeGroup(tokens, context); if (!nextRange.Type.Equals(rangeType)) { throw new InvalidDataException("Non-uniform ranges are not supported. First encountered range: " + TypeToRangeDescription(rangeType) + ", next: " + TypeToRangeDescription(nextRange.Type)); } } if (nextRange != null) { minimumValues.AddRange(nextRange.MinimumValues); maximumValues.AddRange(nextRange.MaximumValues); } } return(new RangeParseResult() { Type = rangeType, MinimumValues = minimumValues, MaximumValues = maximumValues }); }