private NonTypedParameterElement GetParameterValueDefinition(ParameterElementKey key, string type, string value) { double i_fake = 0.0; int year = 0; string trimmedValue = value.Trim(); bool canParseAsNumber = Double.TryParse(trimmedValue, NumberStyles.Float, CultureInfo.InvariantCulture, out i_fake); if (type != null) { if (type.ToLowerInvariant() == "s") { return(new ParameterElementConstant <string>(key, value)); } else { throw new NotImplementedException(); } } else if (canParseAsNumber) { return(new ParameterElementConstant <double>(key, i_fake)); } else if (trimmedValue.StartsWith(c_normalDistributionName + "(") && trimmedValue.EndsWith(")")) { string parameterPartOfValue = trimmedValue.Substring(c_normalDistributionName.Length + 1, trimmedValue.Length - (c_normalDistributionName.Length + 2)); string[] arguments = parameterPartOfValue.Split(';'); double bestGuess = Convert.ToDouble(arguments[0], CultureInfo.InvariantCulture); double standardDeviation = Convert.ToDouble(arguments[1], CultureInfo.InvariantCulture); double?lowerBound = null; double?upperBound = null; if (arguments.Length > 2) { if (arguments[2].Trim().Length > 0) { lowerBound = Convert.ToDouble(arguments[2], CultureInfo.InvariantCulture); } if (arguments.Length > 3) { if (arguments[3].Trim().Length > 0) { upperBound = Convert.ToDouble(arguments[3], CultureInfo.InvariantCulture); } } } return(new ParameterElementNormalDistribution(key, bestGuess, standardDeviation, lowerBound, upperBound)); } else if (trimmedValue.StartsWith(c_triangularDistributionName + "(") && trimmedValue.EndsWith(")")) { string parameterPartOfValue = trimmedValue.Substring(c_triangularDistributionName.Length + 1, trimmedValue.Length - (c_triangularDistributionName.Length + 2)); string[] arguments = parameterPartOfValue.Split(';'); double min = Convert.ToDouble(arguments[0], CultureInfo.InvariantCulture); double bestGuess = Convert.ToDouble(arguments[1], CultureInfo.InvariantCulture); double max = Convert.ToDouble(arguments[2], CultureInfo.InvariantCulture); return(new ParameterElementTriangularDistribution(key, bestGuess, min, max)); } else if (trimmedValue.StartsWith(c_exponentialDistributionName + "(") && trimmedValue.EndsWith(")")) { string parameterPartOfValue = trimmedValue.Substring(c_exponentialDistributionName.Length + 1, trimmedValue.Length - (c_exponentialDistributionName.Length + 2)); string[] arguments = parameterPartOfValue.Split(';'); double lambda = Convert.ToDouble(arguments[0], CultureInfo.InvariantCulture); double?lowerBound = null; double?upperBound = null; if (arguments.Length > 1) { if (arguments[1].Trim().Length > 0) { lowerBound = Convert.ToDouble(arguments[1], CultureInfo.InvariantCulture); } if (arguments.Length > 2) { if (arguments[2].Trim().Length > 0) { upperBound = Convert.ToDouble(arguments[2], CultureInfo.InvariantCulture); } } } return(new ParameterElementExponentialDistribution(key, lambda, lowerBound, upperBound)); } else if (trimmedValue.StartsWith(c_gammaDistributionName + "(") && trimmedValue.EndsWith(")")) { string parameterPartOfValue = trimmedValue.Substring(c_gammaDistributionName.Length + 1, trimmedValue.Length - (c_gammaDistributionName.Length + 2)); string[] arguments = parameterPartOfValue.Split(';'); double alpha = Convert.ToDouble(arguments[0], CultureInfo.InvariantCulture); double beta = Convert.ToDouble(arguments[1], CultureInfo.InvariantCulture); double?lowerBound = null; double?upperBound = null; if (arguments.Length > 2) { if (arguments[2].Trim().Length > 0) { lowerBound = Convert.ToDouble(arguments[2], CultureInfo.InvariantCulture); } if (arguments.Length > 3) { if (arguments[3].Trim().Length > 0) { upperBound = Convert.ToDouble(arguments[3], CultureInfo.InvariantCulture); } } } return(new ParameterElementGammaDistribution(key, alpha, beta, lowerBound, upperBound)); } else if (trimmedValue.EndsWith("y") && Int32.TryParse(trimmedValue.Substring(0, trimmedValue.Length - 1), out year)) { return(new ParameterElementConstant <Timestep>(key, Timestep.FromYear(year))); } else { throw new ApplicationException(string.Format("Syntax error: '[0}' is not a valid value for {1}.", trimmedValue /*, ParameterKey.Name*/)); } }
private void ValueSetForIndex(Timestep index1, D2 index2) { _valuesHasBeenSet[index1.Value, index2.Index] = true; _currentYear = index1; }
private void ValueSetForIndex(Timestep index) { _valuesHasBeenSet[index.Value] = true; _currentYear = index; }
// This implementation uses the new ~ syntax for distributions private NonTypedParameterElement GetParameterValueDefinition2(ParameterElementKey key, string value) { string trimmedValue = value.Trim().ToLowerInvariant(); double valueasnumber; bool canParseAsNumber = Double.TryParse(trimmedValue, NumberStyles.Float, CultureInfo.InvariantCulture, out valueasnumber); int year; if (trimmedValue.StartsWith("~")) { int pos_openbracket = trimmedValue.IndexOf('('); int pos_closebracket = trimmedValue.IndexOf(')'); string distributionname = trimmedValue.Substring(1, pos_openbracket - 1); string argumentstring = trimmedValue.Substring(pos_openbracket + 1, pos_closebracket - pos_openbracket - 1); string[] arguments = argumentstring.Split(';').Select(s => s.Trim()).ToArray(); string[] positionalargs = arguments.Where(s => !s.Contains('=')).ToArray(); var namedargs = (from a in arguments where a.Contains('=') let splitindex = a.IndexOf('=') let argname = a.Substring(0, splitindex).ToLowerInvariant() let argvalue = a.Substring(splitindex + 1) select new { argname, argvalue }).ToDictionary(i => i.argname, i => i.argvalue); if (distributionname == "n") { double bestGuess = Double.Parse(positionalargs[0], CultureInfo.InvariantCulture); double standardDeviation = Double.Parse(positionalargs[1], CultureInfo.InvariantCulture); double?lowerBound = namedargs.ContainsKey("min") ? (double?)Double.Parse(namedargs["min"], CultureInfo.InvariantCulture) : null; double?upperBound = namedargs.ContainsKey("max") ? (double?)Double.Parse(namedargs["max"], CultureInfo.InvariantCulture) : null; return(new ParameterElementNormalDistribution(key, bestGuess, standardDeviation, lowerBound, upperBound)); } else if (distributionname == "gamma") { double alpha = Double.Parse(positionalargs[0], CultureInfo.InvariantCulture); double beta = Double.Parse(positionalargs[1], CultureInfo.InvariantCulture); double?lowerBound = namedargs.ContainsKey("min") ? (double?)Double.Parse(namedargs["min"], CultureInfo.InvariantCulture) : null; double?upperBound = namedargs.ContainsKey("max") ? (double?)Double.Parse(namedargs["max"], CultureInfo.InvariantCulture) : null; return(new ParameterElementGammaDistribution(key, alpha, beta, lowerBound, upperBound)); } else if (distributionname == "exp") { double lambda = Double.Parse(positionalargs[0], CultureInfo.InvariantCulture); double?lowerBound = namedargs.ContainsKey("min") ? (double?)Double.Parse(namedargs["min"], CultureInfo.InvariantCulture) : null; double?upperBound = namedargs.ContainsKey("max") ? (double?)Double.Parse(namedargs["max"], CultureInfo.InvariantCulture) : null; return(new ParameterElementExponentialDistribution(key, lambda, lowerBound, upperBound)); } else if (distributionname == "triangular") { double min = Double.Parse(positionalargs[0], CultureInfo.InvariantCulture); double max = Double.Parse(positionalargs[1], CultureInfo.InvariantCulture); double bestGuess = Double.Parse(positionalargs[2], CultureInfo.InvariantCulture); return(new ParameterElementTriangularDistribution(key, bestGuess, min, max)); } else { throw new InvalidOperationException("Invalid distribution name"); } } else if (canParseAsNumber) { return(new ParameterElementConstant <double>(key, valueasnumber)); } else if (trimmedValue.EndsWith("y") && Int32.TryParse(trimmedValue.Substring(0, trimmedValue.Length - 1), out year)) { return(new ParameterElementConstant <Timestep>(key, Timestep.FromYear(year))); } else { return(new ParameterElementConstant <string>(key, value)); } }