using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.RealVectorEncoding; using HeuristicLab.Optimization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Algorithms.SAPBA { [StorableClass] [Item("Surrogate problem (single-objective)", "Wrapper for a problem that allows surrogate models to mitigate some of the work")] public class SurrogateProblem : SingleObjectiveBasicProblem { [Storable] private ISurrogateStrategy Strategy; #region HLConstructors [StorableConstructor] protected SurrogateProblem(bool deserializing) : base(deserializing) { } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { } protected SurrogateProblem(SurrogateProblem original, Cloner cloner) : base(original, cloner) { Strategy = original?.Strategy; } public override IDeepCloneable Clone(Cloner cloner) { return new SurrogateProblem(this, cloner); } public SurrogateProblem() { } #endregion public override double Evaluate(Individual individual, IRandom random) { return Strategy.Evaluate(individual.RealVector(), random); } public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) { base.Analyze(individuals, qualities, results, random); Strategy.Analyze(individuals, qualities, results, random); } public override bool Maximization { get; } public void SetStrategy(ISurrogateStrategy strategy) { Strategy = strategy; } public void SetProblem(SingleObjectiveBasicProblem expensiveProblem) { if (expensiveProblem != null) Encoding = expensiveProblem.Encoding as RealVectorEncoding; } } }