Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/28/16 18:32:54 (9 years ago)
Author:
gkronber
Message:

#1966: changed PackingShapes to ParameterizedItems

Location:
branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Shapes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Shapes/CuboidPackingShape.cs

    r13497 r13574  
    2626using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2727using HeuristicLab.Common;
     28using HeuristicLab.Data;
     29using HeuristicLab.Parameters;
    2830using HeuristicLab.Problems.BinPacking.Dimensions;
    2931
     
    3335  public abstract class CuboidPackingShape : PackingShape<ThreeDimensionalPacking>, IRegularPackingShape, IComparable<CuboidPackingShape> {
    3436    #region Properties
    35     /// <summary>
    36     /// Describes the size on the X-axis
    37     /// </summary>   
    38     [Storable]
    39     public int Width { get; set; }
    40     /// <summary>
    41     /// Describes the size on the Y-axis
    42     /// </summary>   
    43     [Storable]
    44     public int Height { get; set; }
    45     /// <summary>
    46     /// Describes the size on the Z-axis
    47     /// </summary> 
    48     [Storable]
    49     public int Depth { get; set; }
     37    public int Height {
     38      get { return ((IFixedValueParameter<IntValue>)Parameters["Height"]).Value.Value; }
     39      set { ((IFixedValueParameter<IntValue>)Parameters["Height"]).Value.Value = value; }
     40    }
     41
     42    public int Width {
     43      get { return ((IFixedValueParameter<IntValue>)Parameters["Width"]).Value.Value; }
     44      set { ((IFixedValueParameter<IntValue>)Parameters["Width"]).Value.Value = value; }
     45    }
     46
     47    public int Depth {
     48      get { return ((IFixedValueParameter<IntValue>)Parameters["Depth"]).Value.Value; }
     49      set { ((IFixedValueParameter<IntValue>)Parameters["Depth"]).Value.Value = value; }
     50    }
     51
    5052    #endregion
    5153
     
    101103    #endregion
    102104
    103     protected CuboidPackingShape(int width, int height, int depth)
    104       : base() {
    105       this.Width = width;
    106       this.Height = height;
    107       this.Depth = depth;
    108     }
     105
    109106
    110107    public override void InitializeFromMeasures(int[] measures) {
     
    119116    }
    120117
     118
     119    protected CuboidPackingShape()
     120      : base() {
     121      Parameters.Add(new FixedValueParameter<IntValue>("Width"));
     122      Parameters.Add(new FixedValueParameter<IntValue>("Height"));
     123      Parameters.Add(new FixedValueParameter<IntValue>("Depth"));
     124    }
     125
     126    protected CuboidPackingShape(int width, int height, int depth)
     127      : this() {
     128      this.Width = width;
     129      this.Height = height;
     130      this.Depth = depth;
     131    }
     132
    121133    [StorableConstructor]
    122134    protected CuboidPackingShape(bool deserializing) : base(deserializing) { }
    123135    protected CuboidPackingShape(CuboidPackingShape original, Cloner cloner)
    124136      : base(original, cloner) {
    125       this.Width = original.Width;
    126       this.Height = original.Height;
    127       this.Depth = original.Depth;
    128137    }
    129 
    130     protected CuboidPackingShape() : base() { }
    131138
    132139    public override string ToString() {
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Shapes/PackingShape.cs

    r13497 r13574  
    2222
    2323using System;
     24using System.Collections.Generic;
     25using System.Drawing;
     26using System.Linq;
    2427using HeuristicLab.Problems.BinPacking.Interfaces;
    2528using HeuristicLab.Core;
     
    3033  [Item("PackingShape", "Represents an abstract shape to describe the measures and the positioning of objects related to bin-packing problems.")]
    3134  [StorableClass]
    32   public abstract class PackingShape<T> : Item, IPackingShape
     35  public abstract class PackingShape<T> : Item, IPackingShape, IParameterizedItem
    3336    where T : class, IPackingDimensions {
    3437    public static Type PositionType {
     
    4447    public abstract T Origin { get; }
    4548
    46     protected PackingShape(int[] measures) {
    47       InitializeFromMeasures(measures);
     49    protected PackingShape()
     50      : base() {
     51      Parameters = new ParameterCollection();
    4852    }
    49 
    50     protected PackingShape() { }
    5153
    5254
    5355    [StorableConstructor]
    54     protected PackingShape(bool deserializing) : base(deserializing) { }
    55     protected PackingShape(PackingShape<T> original, Cloner cloner)
    56       : base(original, cloner) {
     56    protected PackingShape(bool deserializing) { }
     57    protected PackingShape(PackingShape<T> original, Cloner cloner) {
     58      this.Parameters = new ParameterCollection(original.Parameters.Select(p => cloner.Clone(p)));
    5759    }
     60
     61    public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
     62      foreach (IValueParameter param in Parameters.OfType<IValueParameter>()) {
     63        var children = GetCollectedValues(param);
     64        foreach (var c in children) {
     65          if (String.IsNullOrEmpty(c.Key))
     66            values.Add(param.Name, c.Value);
     67          else values.Add(param.Name + "." + c.Key, c.Value);
     68        }
     69      }
     70    }
     71
     72    protected virtual IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
     73      if (param.Value == null) yield break;
     74      if (param.GetsCollected) yield return new KeyValuePair<string, IItem>(String.Empty, param.Value);
     75      var parameterizedItem = param.Value as IParameterizedItem;
     76      if (parameterizedItem != null) {
     77        var children = new Dictionary<string, IItem>();
     78        parameterizedItem.CollectParameterValues(children);
     79        foreach (var child in children) yield return child;
     80      }
     81    }
     82
     83    public IKeyedItemCollection<string, IParameter> Parameters { get; private set; }
    5884  }
    5985}
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Shapes/RectangularPackingShape.cs

    r13497 r13574  
    2626using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2727using HeuristicLab.Common;
     28using HeuristicLab.Data;
     29using HeuristicLab.Parameters;
    2830using HeuristicLab.Problems.BinPacking.Dimensions;
    2931
     
    3335  public abstract class RectangularPackingShape : PackingShape<TwoDimensionalPacking>, IRegularPackingShape, IComparable<RectangularPackingShape> {
    3436    #region Properties
    35     [Storable]
    36     public int Height { get; set; }
     37    public int Height {
     38      get { return ((IFixedValueParameter<IntValue>)Parameters["Height"]).Value.Value; }
     39      set { ((IFixedValueParameter<IntValue>)Parameters["Height"]).Value.Value = value; }
     40    }
    3741
    38     [Storable]
    39     public int Width { get; set; }
     42    public int Width {
     43      get { return ((IFixedValueParameter<IntValue>)Parameters["Width"]).Value.Value; }
     44      set { ((IFixedValueParameter<IntValue>)Parameters["Width"]).Value.Value = value; }
     45    }
     46
    4047    #endregion
     48
     49    protected RectangularPackingShape()
     50      : base() {
     51      Parameters.Add(new FixedValueParameter<IntValue>("Width"));
     52      Parameters.Add(new FixedValueParameter<IntValue>("Height"));
     53    }
     54    protected RectangularPackingShape(int width, int height)
     55      : this() {
     56      this.Height = height;
     57      this.Width = width;
     58    }
     59
     60    [StorableConstructor]
     61    protected RectangularPackingShape(bool deserializing) : base(deserializing) { }
     62    protected RectangularPackingShape(RectangularPackingShape original, Cloner cloner)
     63      : base(original, cloner) {
     64    }
     65
     66    public override void InitializeFromMeasures(int[] measures) {
     67      if (measures.Length != 2)
     68        throw new InvalidOperationException("Nr of measures does not fit shape-dimension.");
     69      this.Width = measures[0];
     70      this.Height = measures[1];
     71    }
     72
     73
     74
     75    public override string ToString() {
     76      return String.Format("RectangularPackingShape ({0}, {1})", this.Width, this.Height);
     77    }
     78
     79    #region IComparable<RectangularPackingShape> Members
     80
     81    public int CompareTo(RectangularPackingShape other) {
     82      int result = this.Width.CompareTo(other.Width);
     83      if (result == 0)
     84        result = this.Height.CompareTo(other.Height);
     85      return result;
     86    }
     87
     88    public int CompareTo(object obj) {
     89      var other = obj as RectangularPackingShape;
     90      if (other != null) return CompareTo(other);
     91      else throw new ArgumentException(string.Format("Cannot compare to object {0}", obj), "obj");
     92    }
     93
     94    #endregion
     95
     96    public override int[] ToArray() {
     97      return new int[] { Width, Height };
     98    }
     99
     100
    41101
    42102    #region Helpers
     
    85145    #endregion
    86146
    87     protected RectangularPackingShape() : base() { }
    88     protected RectangularPackingShape(int width, int height)
    89       : base() {
    90       this.Height = height;
    91       this.Width = width;
    92     }
    93 
    94     public override void InitializeFromMeasures(int[] measures) {
    95       if (measures.Length != 2)
    96         throw new InvalidOperationException("Nr of measures does not fit shape-dimension.");
    97       this.Width = measures[0];
    98       this.Height = measures[1];
    99     }
    100 
    101 
    102     [StorableConstructor]
    103     protected RectangularPackingShape(bool deserializing) : base(deserializing) { }
    104     protected RectangularPackingShape(RectangularPackingShape original, Cloner cloner)
    105       : base(original, cloner) {
    106       this.Width = original.Width;
    107       this.Height = original.Height;
    108     }
    109 
    110     public override string ToString() {
    111       return String.Format("RectangularPackingShape ({0}, {1})", this.Width, this.Height);
    112     }
    113 
    114     #region IComparable<RectangularPackingShape> Members
    115 
    116     public int CompareTo(RectangularPackingShape other) {
    117       int result = 0;// this.MultipliedMeasures.CompareTo(other.MultipliedMeasures);
    118       if (result == 0) {
    119         result = this.Width.CompareTo(other.Width);
    120         if (result == 0)
    121           result = this.Height.CompareTo(other.Height);
    122       }
    123       return result;
    124     }
    125 
    126     public int CompareTo(object obj) {
    127       var other = obj as RectangularPackingShape;
    128       if (other != null) return CompareTo(other);
    129       else throw new ArgumentException(string.Format("Cannot compare to object {0}", obj), "obj");
    130     }
    131 
    132     #endregion
    133 
    134     public override int[] ToArray() {
    135       return new int[] { Width, Height };
    136     }
    137147
    138148    private struct RectangleDiagonal {
Note: See TracChangeset for help on using the changeset viewer.