#region License Information /* HeuristicLab * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Scripting; namespace HeuristicLab.Problems.Programmable { [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")] [StorableClass] public abstract class ProblemDefinitionScript : Script, IProblemDefinition { protected bool SuppressEvents { get; set; } [Storable] private VariableStore variableStore; public VariableStore VariableStore { get { return variableStore; } } [StorableConstructor] protected ProblemDefinitionScript(bool deserializing) : base(deserializing) { } protected ProblemDefinitionScript(ProblemDefinitionScript original, Cloner cloner) : base(original, cloner) { variableStore = cloner.Clone(original.variableStore); } protected ProblemDefinitionScript() :base(){ variableStore = new VariableStore(); } IEncoding IProblemDefinition.Encoding { get { return CompiledProblemDefinition != null ? CompiledProblemDefinition.Encoding : null; } } IEnumerable IProblemDefinition.GetNeighbors(Individual individual, IRandom random) { return CompiledProblemDefinition.GetNeighbors(individual, random); } private readonly object locker = new object(); private volatile IProblemDefinition compiledProblemDefinition; protected IProblemDefinition CompiledProblemDefinition { get { lock (locker) { if (compiledProblemDefinition == null) { Compile(); } } return compiledProblemDefinition; } private set { compiledProblemDefinition = value; OnProblemDefinitionChanged(); } } public override Assembly Compile() { var assembly = base.Compile(); var types = assembly.GetTypes(); try { var inst = (CompiledProblemDefinition)Activator.CreateInstance(types.First(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x))); inst.vars = new Variables(VariableStore); inst.Initialize(); CompiledProblemDefinition = inst; } catch { compiledProblemDefinition = null; } return assembly; } protected override void OnCodeChanged() { base.OnCodeChanged(); compiledProblemDefinition = null; } public event EventHandler ProblemDefinitionChanged; protected virtual void OnProblemDefinitionChanged() { var handler = ProblemDefinitionChanged; if (handler != null) handler(this, EventArgs.Empty); } } }