Changeset 14480
- Timestamp:
- 12/12/16 15:55:22 (8 years ago)
- Location:
- branches/HeuristicLab.ExpressionGenerator
- Files:
-
- 2 added
- 1 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/Expression.cs
r14448 r14480 29 29 30 30 namespace HeuristicLab.ExpressionGenerator { 31 public class Expression : IExpression { 32 public string Name { get; set; } 31 public class Expression : IExpression { 32 // unique name for each expression 33 public string Name { get; set; } 34 // optional label (non-unique) - useful for string (infix) representations 35 public string Label { get; set; } 33 36 34 public ExpressionType Type { get; }37 public ExpressionType Type { get; private set; } 35 38 36 private readonly List<Expression> arguments; 37 public IEnumerable<Expression> Arguments { 38 get { return arguments ?? Enumerable.Empty<Expression>(); } 39 private readonly List<Expression> arguments; 40 public IEnumerable<Expression> Arguments { 41 get { return arguments ?? Enumerable.Empty<Expression>(); } 42 } 43 44 public IRandom Distribution { get; private set; } 45 46 public Func<IEnumerable<double>, double> Transform { get; private set; } 47 48 public double Value { get; private set; } 49 50 public Expression(string name, double value) { 51 Type = ExpressionType.Constant; 52 Value = value; 53 Name = name; 54 } 55 56 public Expression(string name, IRandom distribution) { 57 Type = ExpressionType.RandomVariable; 58 Distribution = distribution; 59 Name = name; 60 } 61 62 public Expression(string name, Func<IEnumerable<double>, double> transform, IEnumerable<Expression> arguments) { 63 Type = ExpressionType.Function; 64 Transform = transform; 65 this.arguments = this.arguments ?? new List<Expression>(); 66 this.arguments.AddRange(arguments); 67 Name = name; 68 } 69 70 public static Expression Constant(string name, double value) { 71 return new Expression(name, value); 72 } 73 74 public static Expression RandomVariable(string name, IRandom distribution) { 75 return new Expression(name, distribution); 76 } 77 78 public static Expression Function(string name, Func<IEnumerable<double>, double> transform, IEnumerable<Expression> arguments) { 79 return new Expression(name, transform, arguments); 80 } 81 82 public override string ToString() { 83 var sb = new StringBuilder(); 84 85 switch (Type) { 86 case ExpressionType.Constant: 87 sb.Append(Value.ToString("0.000", CultureInfo.CurrentCulture)); 88 break; 89 case ExpressionType.RandomVariable: 90 sb.Append(string.Format("{0} ~ {1}", Name, GetStringDescription(Distribution))); 91 break; 92 case ExpressionType.Function: 93 sb.Append(string.Format("{0} = f(", Name)); 94 if (Arguments.Any()) { 95 for (int i = 0; i < arguments.Count - 1; ++i) 96 sb.Append(string.Format("{0}, ", arguments[i].Name)); 97 sb.Append(string.Format("{0})", arguments.Last().Name)); 98 } 99 break; 100 } 101 102 return sb.ToString(); 103 } 104 105 public string PrintInfix() { 106 var sb = new StringBuilder(); 107 switch (Type) { 108 case ExpressionType.Constant: 109 sb.Append(Value < 0 110 ? string.Format("(- {0})", Math.Abs(Value).ToString("0.000", CultureInfo.CurrentCulture)) 111 : Value.ToString("0.000", CultureInfo.CurrentCulture)); 112 break; 113 case ExpressionType.RandomVariable: 114 sb.Append(Name); 115 break; 116 case ExpressionType.Function: 117 if (!Arguments.Any()) 118 break; 119 var args = Arguments.ToList(); 120 sb.Append("("); 121 // the Label should be known to the infix parser 122 if (Label == "+" || Label == "-" || Label == "*" || Label == "/") { 123 if (args.Count == 1) { 124 sb.Append(string.Format("{0}", args[0].PrintInfix())); 125 } else { 126 sb.Append("("); 127 var last = args.Last(); 128 for (int i = 0; i < args.Count - 1; ++i) { 129 var arg = args[i]; 130 sb.Append(string.Format("{0} {1}", arg.PrintInfix(), Label)); 131 } 132 sb.Append(string.Format(" {0})", last.PrintInfix())); 133 } 134 } else { 135 sb.Append(string.Format("{0}(", Label)); 136 var last = args.Last(); 137 for (int i = 0; i < args.Count - 1; ++i) { 138 var arg = args[i]; 139 sb.Append(string.Format("{0}, ", arg.PrintInfix(), Label)); 140 } 141 sb.Append(string.Format("{0})", last.PrintInfix())); 142 } 143 sb.Append(")"); 144 break; 145 } 146 return sb.ToString(); 147 } 148 149 public string PrintDot() { 150 var stack = new Stack<Expression>(); 151 stack.Push(this); 152 153 var sb = new StringBuilder(); 154 sb.AppendLine("digraph g {"); 155 156 while (stack.Count > 0) { 157 var top = stack.Pop(); 158 159 if (!top.Arguments.Any()) 160 continue; 161 162 foreach (var arg in top.Arguments) { 163 var from = top.Arguments.Any() ? top.Name : top.ToString(); 164 var to = arg.Arguments.Any() ? arg.Name : arg.ToString(); 165 sb.AppendLine(string.Format("\"{0}\" -> \"{1}\";", from, to)); 166 stack.Push(arg); 167 } 168 } 169 170 sb.AppendLine("}"); 171 return sb.ToString(); 172 } 173 174 private static string GetStringDescription(IRandom random) { 175 var normal = random as NormalDistributedRandom; 176 if (normal != null) 177 return string.Format("N({0}, {1})", normal.Mu, normal.Sigma); 178 179 var uniform = random as UniformDistributedRandom; 180 if (uniform != null) 181 return string.Format("U({0}, {1})", uniform.Min, uniform.Max); 182 183 var gamma = random as GammaDistributedRandom; 184 if (gamma != null) 185 return string.Format("G({0}, {1})", gamma.Shape, gamma.Rate); 186 187 return random.ToString(); 188 } 39 189 } 40 41 public IRandom Distribution { get; }42 43 public Func<IEnumerable<double>, double> Transform { get; }44 45 public double Value { get; }46 47 public Expression(string name, double value) {48 Type = ExpressionType.Constant;49 Value = value;50 Name = name;51 }52 53 public Expression(string name, IRandom distribution) {54 Type = ExpressionType.RandomVariable;55 Distribution = distribution;56 Name = name;57 }58 59 public Expression(string name, Func<IEnumerable<double>, double> transform, IEnumerable<Expression> arguments) {60 Type = ExpressionType.Function;61 Transform = transform;62 this.arguments = this.arguments ?? new List<Expression>();63 this.arguments.AddRange(arguments);64 Name = name;65 }66 67 public static Expression Constant(string label, double value) {68 return new Expression(label, value);69 }70 71 public static Expression RandomVariable(string label, IRandom distribution) {72 return new Expression(label, distribution);73 }74 75 public static Expression Function(string label, Func<IEnumerable<double>, double> transform, IEnumerable<Expression> arguments) {76 return new Expression(label, transform, arguments);77 }78 79 public override string ToString() {80 var sb = new StringBuilder();81 82 switch (Type) {83 case ExpressionType.Constant:84 sb.Append(Value.ToString("0.000", CultureInfo.CurrentCulture));85 break;86 case ExpressionType.RandomVariable:87 sb.Append(string.Format("{0} ~ {1}", Name, GetStringDescription(Distribution)));88 break;89 case ExpressionType.Function:90 sb.Append(string.Format("{0} = f(", Name));91 if (Arguments.Any()) {92 for (int i = 0; i < arguments.Count - 1; ++i)93 sb.Append(string.Format("{0}, ", arguments[i].Name));94 sb.Append(string.Format("{0})", arguments.Last().Name));95 }96 break;97 }98 99 return sb.ToString();100 }101 102 public string PrintInfix() {103 var sb = new StringBuilder();104 105 return sb.ToString();106 }107 108 public string PrintDot() {109 var stack = new Stack<Expression>();110 stack.Push(this);111 112 var sb = new StringBuilder();113 sb.AppendLine("digraph g {");114 115 while (stack.Count > 0) {116 var top = stack.Pop();117 118 if (!top.Arguments.Any())119 continue;120 121 foreach (var arg in top.Arguments) {122 var from = top.Arguments.Any() ? top.Name : top.ToString();123 var to = arg.Arguments.Any() ? arg.Name : arg.ToString();124 sb.AppendLine(string.Format("\"{0}\" -> \"{1}\";", from, to));125 stack.Push(arg);126 }127 }128 129 sb.AppendLine("}");130 return sb.ToString();131 }132 133 private static string GetStringDescription(IRandom random) {134 var normal = random as NormalDistributedRandom;135 if (normal != null)136 return string.Format("N({0}, {1})", normal.Mu, normal.Sigma);137 138 var uniform = random as UniformDistributedRandom;139 if (uniform != null)140 return string.Format("U({0}, {1})", uniform.Min, uniform.Max);141 142 var gamma = random as GammaDistributedRandom;143 if (gamma != null)144 return string.Format("G({0}, {1})", gamma.Shape, gamma.Rate);145 146 return random.ToString();147 }148 }149 190 } -
branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/ExpressionTemplate.cs
r14448 r14480 30 30 private List<Tuple<Expression, double>> arguments; 31 31 private readonly Func<IEnumerable<double>, double> transform; 32 public string Label { get; set; } 32 33 33 protected Expression Instantiate(string label, IRandom random, int n, bool sampleWithRepetition = false) {34 protected Expression Instantiate(string name, IRandom random, int n, bool sampleWithRepetition = false) { 34 35 var weights = arguments.Select(x => x.Item2); 35 36 var args = sampleWithRepetition 36 37 ? arguments.SampleProportional(random, n, weights).Select(x => x.Item1) 37 38 : arguments.SampleProportionalWithoutRepetition(random, n, weights).Select(x => x.Item1); 38 return Expression.Function(label, transform, args); 39 var func = Expression.Function(name, transform, args); 40 func.Label = Label; 41 return func; 39 42 } 40 43 -
branches/HeuristicLab.ExpressionGenerator/HeuristicLab.ExpressionGenerator/3.4/HeuristicLab.ExpressionGenerator.csproj
r14409 r14480 22 22 <ErrorReport>prompt</ErrorReport> 23 23 <WarningLevel>4</WarningLevel> 24 <LangVersion>4</LangVersion> 24 25 </PropertyGroup> 25 26 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> … … 76 77 <Compile Include="Interfaces\IExpression.cs" /> 77 78 <Compile Include="Plugin.cs" /> 78 <Compile Include="Properties\AssemblyInfo.cs" /> 79 </ItemGroup> 80 <ItemGroup> 81 <None Include="Properties\AssemblyInfo.cs.frame" /> 79 82 </ItemGroup> 80 83 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> … … 83 86 set ProjectDir=$(ProjectDir) 84 87 set SolutionDir=$(SolutionDir) 88 set Outdir=$(Outdir) 89 85 90 call PreBuildEvent.cmd</PreBuildEvent> 86 91 </PropertyGroup>
Note: See TracChangeset
for help on using the changeset viewer.