#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 HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.BinaryVectorEncoding;
using HeuristicLab.Encodings.IntegerVectorEncoding;
using HeuristicLab.Encodings.PermutationEncoding;
using HeuristicLab.Encodings.RealVectorEncoding;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.ParameterVectorEncoding {
[Item("ParameterVector", "Encodes a range of parameters of different types.")]
[StorableClass]
public class ParameterVector : Item {
private bool isFeasible;
[Storable]
public bool IsFeasible {
get { return isFeasible; }
set {
if (isFeasible == value) return;
isFeasible = value;
OnIsFeasibleChanged();
}
}
[Storable]
public BinaryVector BooleanParameters { get; set; }
[Storable]
public IntegerVector IntegerParameters { get; set; }
[Storable]
public RealVector RealParameters { get; set; }
[Storable]
public Permutation PermutationParameter { get; set; }
[Storable]
protected BidirectionalDictionary BoolMap;
[Storable]
protected BidirectionalDictionary IntMap;
[Storable]
protected BidirectionalDictionary RealMap;
[Storable]
public IntMatrix IntegerBounds { get; set; }
[Storable]
public DoubleMatrix RealBounds { get; set; }
[StorableConstructor]
protected ParameterVector(bool deserializing) : base(deserializing) { }
protected ParameterVector(ParameterVector original, Cloner cloner)
: base(original, cloner) {
IsFeasible = original.IsFeasible;
if (original.BooleanParameters != null) {
BooleanParameters = cloner.Clone(original.BooleanParameters);
BoolMap = new BidirectionalDictionary(original.BoolMap);
}
if (original.IntegerParameters != null) {
IntegerParameters = cloner.Clone(original.IntegerParameters);
IntMap = new BidirectionalDictionary(original.IntMap);
IntegerBounds = cloner.Clone(original.IntegerBounds);
}
if (original.RealParameters != null) {
RealParameters = cloner.Clone(original.RealParameters);
RealMap = new BidirectionalDictionary(original.RealMap);
RealBounds = cloner.Clone(original.RealBounds);
}
PermutationParameter = cloner.Clone(original.PermutationParameter);
}
public ParameterVector() {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new ParameterVector(this, cloner);
}
public bool Boolean(string name) {
return BooleanParameters[BoolMap.GetByFirst(name)];
}
public bool Boolean(int index) {
return BooleanParameters[index];
}
public IEnumerable BooleanParameterNames {
get {
for (int i = 0; i < BooleanParameters.Length; i++)
yield return BoolMap.GetBySecond(i);
}
}
public int Integer(string name) {
return IntegerParameters[IntMap.GetByFirst(name)];
}
public int Integer(int index) {
return IntegerParameters[index];
}
public IEnumerable IntegerParameterNames {
get {
for (int i = 0; i < IntegerParameters.Length; i++)
yield return IntMap.GetBySecond(i);
}
}
public double Real(string name) {
return RealParameters[RealMap.GetByFirst(name)];
}
public double Real(int index) {
return RealParameters[index];
}
public IEnumerable RealParameterNames {
get {
for (int i = 0; i < RealParameters.Length; i++)
yield return RealMap.GetBySecond(i);
}
}
public Permutation Permutation() {
return PermutationParameter;
}
public event EventHandler IsFeasibleChanged;
protected virtual void OnIsFeasibleChanged() {
var handler = IsFeasibleChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
#region Internal Methods for the Builder
internal void SetBooleanParameters(HashSet bools = null) {
if (bools == null) {
BooleanParameters = null;
BoolMap = null;
} else {
BooleanParameters = new BinaryVector(bools.Count);
BoolMap = new BidirectionalDictionary();
foreach (var p in bools) {
BoolMap.Add(p, BoolMap.Count);
}
}
}
internal void SetIntegerParameters(Dictionary> ints = null) {
if (ints == null) {
IntegerParameters = null;
IntMap = null;
IntegerBounds = null;
} else {
IntegerParameters = new IntegerVector(ints.Count);
IntMap = new BidirectionalDictionary();
IntegerBounds = new IntMatrix(ints.Count, 3);
var i = 0;
foreach (var p in ints) {
IntegerBounds[i, 0] = p.Value.Item1;
IntegerBounds[i, 1] = p.Value.Item2;
IntegerBounds[i, 2] = p.Value.Item3 ?? 1;
IntegerParameters[i] = p.Value.Item1; // default to min
IntMap.Add(p.Key, i++);
}
}
}
internal void SetRealParameters(Dictionary> reals = null) {
if (reals == null) {
RealParameters = null;
RealMap = null;
RealBounds = null;
} else {
RealParameters = new RealVector(reals.Count);
RealMap = new BidirectionalDictionary();
RealBounds = new DoubleMatrix(reals.Count, 2);
var i = 0;
foreach (var p in reals) {
RealBounds[i, 0] = p.Value.Item1;
RealBounds[i, 1] = p.Value.Item2;
RealParameters[i] = p.Value.Item1; // default to min
RealMap.Add(p.Key, i++);
}
}
}
internal void SetPermutationParameters(Tuple perms = null) {
if (perms == null) PermutationParameter = null;
else PermutationParameter = new Permutation(perms.Item1, perms.Item2);
}
#endregion
}
}