namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
using HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator;
using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
///
/// Pushes a random already defined name or a new one onto the NAME stack.
///
[PushExpression(
StackTypes.Name,
"NAME.RAND",
"Pushes a random already defined name or a new one based on the Name-ERC-Options onto the NAME stack.")]
[StorableClass]
public class NameRandExpression : StatelessExpression {
public NameRandExpression() { }
[StorableConstructor]
protected NameRandExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return !interpreter.Configuration.ErcOptions.NameErcOptions.IsEnabled;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var name = interpreter.Configuration.ErcOptions.NameErcOptions.GetErcValue(interpreter.Random);
interpreter.NameStack.Push(name);
}
}
///
/// Pushes a random integer.
///
[PushExpression(
StackTypes.Integer,
"INTEGER.RAND",
"Pushes a random integer based on the Integer-ERC-Options onto the INTEGER stack.")]
[StorableClass]
public class IntegerRandExpression : StatelessExpression {
public IntegerRandExpression() { }
[StorableConstructor]
protected IntegerRandExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return !interpreter.Configuration.ErcOptions.IntegerErcOptions.IsEnabled;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var value = interpreter.Configuration.ErcOptions.IntegerErcOptions.GetErcValue(interpreter.Random);
interpreter.IntegerStack.Push(value);
}
}
///
/// Pushes a random float.
///
[PushExpression(
StackTypes.Float,
"FLOAT.RAND",
"Pushes a random float based on the Float-ERC-Options onto the FLOAT stack.")]
[StorableClass]
public class FloatRandExpression : StatelessExpression {
public FloatRandExpression() { }
[StorableConstructor]
protected FloatRandExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return !interpreter.Configuration.ErcOptions.FloatErcOptions.IsEnabled;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var value = interpreter.Configuration.ErcOptions.FloatErcOptions.GetErcValue(interpreter.Random);
interpreter.FloatStack.Push(value);
}
}
///
/// Pushes a random boolean.
///
[PushExpression(
StackTypes.Boolean,
"BOOLEAN.RAND",
"Pushes a random boolean based on the Boolean-ERC-Options onto the BOOLEAN stack.")]
[StorableClass]
public class BooleanRandExpression : StatelessExpression {
public BooleanRandExpression() { }
[StorableConstructor]
protected BooleanRandExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return !interpreter.Configuration.ErcOptions.BooleanErcOptions.IsEnabled;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var value = interpreter.Configuration.ErcOptions.BooleanErcOptions.GetErcValue(interpreter.Random);
interpreter.BooleanStack.Push(value);
}
}
///
/// Pushes random expressions onto the code stack.
///
[PushExpression(
StackTypes.Code,
"CODE.RAND",
"Pushes a random code onto the CODE stack.")]
[StorableClass]
public class CodeRandExpression : StatelessExpression {
public CodeRandExpression() { }
[StorableConstructor]
protected CodeRandExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.IntegerStack.Count == 0 || interpreter.IntegerStack.Top < 1;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var size = interpreter.IntegerStack.Pop().AsInt(interpreter.Configuration.MaxPointsInRandomExpression);
var program = LinearCodeGenerator.RandomProgram(
interpreter.PoolContainer.PushProgramPool,
interpreter.PoolContainer.ExpressionListPool,
size,
interpreter.Random,
interpreter.Configuration,
interpreter.CustomExpressions);
interpreter.CodeStack.Push(program);
}
}
///
/// Pushes a random char.
///
[PushExpression(
StackTypes.Char,
"CHAR.RAND",
"Pushes a random char based on the Char-ERC-Options onto the CHAR stack.")]
[StorableClass]
public class CharRandExpression : StatelessExpression {
public CharRandExpression() { }
[StorableConstructor]
protected CharRandExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return !interpreter.Configuration.ErcOptions.CharErcOptions.IsEnabled;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var value = interpreter.Configuration.ErcOptions.CharErcOptions.GetErcValue(interpreter.Random);
interpreter.CharStack.Push(value);
}
///
/// Pushes a random string.
///
[PushExpression(
StackTypes.String,
"STRING.RAND",
"Pushes a random string based on the String-ERC-Options onto the STRING stack.")]
[StorableClass]
public class StringRandExpression : StatelessExpression {
public StringRandExpression() { }
[StorableConstructor]
protected StringRandExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return !interpreter.Configuration.ErcOptions.StringErcOptions.IsEnabled;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var value = interpreter.Configuration.ErcOptions.StringErcOptions.GetErcValue(interpreter.Random);
interpreter.StringStack.Push(value);
}
///
/// Pushes a random interger vector.
///
[PushExpression(
StackTypes.IntegerVector,
"INTEGER[].RAND",
"Pushes a random integer vector based on the Integer-Vector-ERC-Options onto the INTEGER[] stack.")]
[StorableClass]
public class IntegerVectorRandExpression : StatelessExpression {
public IntegerVectorRandExpression() { }
[StorableConstructor]
protected IntegerVectorRandExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return !interpreter.Configuration.ErcOptions.IntegerVectorErcOptions.IsEnabled;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var value = interpreter.Configuration.ErcOptions.IntegerVectorErcOptions.GetErcValue(interpreter.Random);
interpreter.IntegerVectorStack.Push(new List(value.Select(i => (long)i)));
}
}
///
/// Pushes a random float vector.
///
[PushExpression(
StackTypes.FloatVector,
"FLOAT[].RAND",
"Pushes a random float vector based on the Float-Vector-ERC-Options onto the FLOAT[] stack.")]
[StorableClass]
public class FloatVectorRandExpression : StatelessExpression {
public FloatVectorRandExpression() { }
[StorableConstructor]
protected FloatVectorRandExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return !interpreter.Configuration.ErcOptions.FloatVectorErcOptions.IsEnabled;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var value = interpreter.Configuration.ErcOptions.FloatVectorErcOptions.GetErcValue(interpreter.Random);
interpreter.FloatVectorStack.Push(value);
}
}
///
/// Pushes a random boolean vector.
///
[PushExpression(
StackTypes.StringVector,
"STRING[].RAND",
"Pushes a random string vector based on the String-Vector-ERC-Options onto the STRING[] stack.")]
[StorableClass]
public class StringVectorRandExpression : StatelessExpression {
public StringVectorRandExpression() { }
[StorableConstructor]
protected StringVectorRandExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return !interpreter.Configuration.ErcOptions.StringVectorErcOptions.IsEnabled;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var value = interpreter.Configuration.ErcOptions.StringVectorErcOptions.GetErcValue(interpreter.Random);
interpreter.StringVectorStack.Push(value);
}
}
}
}
}