Changeset 7415
- Timestamp:
- 01/26/12 14:50:56 (13 years ago)
- Location:
- branches/GeneralizedQAP
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common/3.3/ExtensionMethods.cs
r7407 r7415 27 27 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common { 28 28 public static class ExtensionMethods { 29 30 /// <summary>31 /// Simply enumerates the sequence by moving over all elements.32 /// </summary>33 /// <typeparam name="T">The type of the items that are to be enumerated.</typeparam>34 /// <param name="source">The enumerable that contains the items.</param>35 public static void Enumerate<T>(this IEnumerable<T> source) {36 var enumerator = source.GetEnumerator();37 while (enumerator.MoveNext()) ;38 }39 29 40 30 /// <summary> … … 109 99 110 100 /// <summary> 101 /// Selects the first element in the sequence where the selected key is the maximum. 102 /// </summary> 103 /// <remarks> 104 /// Runtime complexity of the operation is O(N). 105 /// </remarks> 106 /// <typeparam name="T">The type of the elements.</typeparam> 107 /// <typeparam name="TComparable">The selected key (needs to be an IComparable).</typeparam> 108 /// <param name="source">The enumeration in which the maximum item should be found.</param> 109 /// <param name="keySelector">The function that selects the key.</param> 110 /// <returns>The element in the enumeration where the selected key is the maximum.</returns> 111 public static T SelectMax<T, TComparable>(this IEnumerable<T> source, Func<T, TComparable> keySelector) where TComparable : IComparable { 112 IEnumerator<T> enumerator = source.GetEnumerator(); 113 if (!enumerator.MoveNext()) throw new InvalidOperationException("Sequence is empty!"); 114 T result = enumerator.Current; 115 TComparable max = keySelector(result); 116 while (enumerator.MoveNext()) { 117 T item = enumerator.Current; 118 TComparable comparison = keySelector(item); 119 if (comparison.CompareTo(max) > 0) { 120 result = item; 121 max = comparison; 122 } 123 } 124 return result; 125 } 126 127 /// <summary> 128 /// Selects the first element in the sequence where the selected key is the minimum. 129 /// </summary> 130 /// <remarks> 131 /// Runtime complexity of the operation is O(N). 132 /// </remarks> 133 /// <typeparam name="T">The type of the elements.</typeparam> 134 /// <typeparam name="TComparable">The selected key (needs to be an IComparable).</typeparam> 135 /// <param name="source">The enumeration in which the minimum item should be found.</param> 136 /// <param name="keySelector">The function that selects the key.</param> 137 /// <returns>The element in the enumeration where the selected key is the minimum.</returns> 138 public static T SelectMin<T, TComparable>(this IEnumerable<T> source, Func<T, TComparable> keySelector) where TComparable : IComparable { 139 IEnumerator<T> enumerator = source.GetEnumerator(); 140 if (!enumerator.MoveNext()) throw new InvalidOperationException("Sequence is empty!"); 141 T result = enumerator.Current; 142 TComparable min = keySelector(result); 143 while (enumerator.MoveNext()) { 144 T item = enumerator.Current; 145 TComparable comparison = keySelector(item); 146 if (comparison.CompareTo(min) < 0) { 147 result = item; 148 min = comparison; 149 } 150 } 151 return result; 152 } 153 154 /// <summary> 111 155 /// Walks an operator graph in that it jumps from one operator to all its operator parameters and yields each operator it touches. 112 156 /// Cycles are detected and not walked twice. -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views/3.3/GQAPAssignmentView.Designer.cs
r7412 r7415 47 47 this.assignmentTreeView = new System.Windows.Forms.TreeView(); 48 48 this.label1 = new System.Windows.Forms.Label(); 49 this.qualityTextBox = new System.Windows.Forms.TextBox(); 50 this.label2 = new System.Windows.Forms.Label(); 49 this.qualityLabel = new System.Windows.Forms.Label(); 50 this.assignmentGroupBox = new System.Windows.Forms.GroupBox(); 51 this.characteristicsGroupBox = new System.Windows.Forms.GroupBox(); 52 this.flowDistanceQualityLabel = new System.Windows.Forms.Label(); 53 this.label3 = new System.Windows.Forms.Label(); 54 this.installationQualityLabel = new System.Windows.Forms.Label(); 55 this.label5 = new System.Windows.Forms.Label(); 56 this.overbookedCapacityLabel = new System.Windows.Forms.Label(); 57 this.label7 = new System.Windows.Forms.Label(); 58 this.assignmentGroupBox.SuspendLayout(); 59 this.characteristicsGroupBox.SuspendLayout(); 51 60 this.SuspendLayout(); 52 61 // … … 56 65 | System.Windows.Forms.AnchorStyles.Left) 57 66 | System.Windows.Forms.AnchorStyles.Right))); 58 this.assignmentTreeView.Location = new System.Drawing.Point( 3, 50);67 this.assignmentTreeView.Location = new System.Drawing.Point(6, 19); 59 68 this.assignmentTreeView.Margin = new System.Windows.Forms.Padding(3, 6, 3, 3); 60 69 this.assignmentTreeView.Name = "assignmentTreeView"; 61 this.assignmentTreeView.Size = new System.Drawing.Size( 349, 331);70 this.assignmentTreeView.Size = new System.Drawing.Size(228, 331); 62 71 this.assignmentTreeView.TabIndex = 3; 63 72 // … … 65 74 // 66 75 this.label1.AutoSize = true; 67 this.label1.Location = new System.Drawing.Point(3, 7); 76 this.label1.Location = new System.Drawing.Point(6, 19); 77 this.label1.Margin = new System.Windows.Forms.Padding(3); 68 78 this.label1.Name = "label1"; 69 79 this.label1.Size = new System.Drawing.Size(42, 13); … … 71 81 this.label1.Text = "Quality:"; 72 82 // 73 // qualityTextBox 74 // 75 this.qualityTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 83 // qualityLabel 84 // 85 this.qualityLabel.AutoSize = true; 86 this.qualityLabel.Location = new System.Drawing.Point(91, 19); 87 this.qualityLabel.Margin = new System.Windows.Forms.Padding(3); 88 this.qualityLabel.Name = "qualityLabel"; 89 this.qualityLabel.Size = new System.Drawing.Size(10, 13); 90 this.qualityLabel.TabIndex = 4; 91 this.qualityLabel.Text = "-"; 92 // 93 // assignmentGroupBox 94 // 95 this.assignmentGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 96 | System.Windows.Forms.AnchorStyles.Left) 76 97 | System.Windows.Forms.AnchorStyles.Right))); 77 this.qualityTextBox.Location = new System.Drawing.Point(51, 4); 78 this.qualityTextBox.Name = "qualityTextBox"; 79 this.qualityTextBox.Size = new System.Drawing.Size(301, 20); 80 this.qualityTextBox.TabIndex = 1; 81 // 82 // label2 83 // 84 this.label2.AutoSize = true; 85 this.label2.Location = new System.Drawing.Point(3, 31); 86 this.label2.Name = "label2"; 87 this.label2.Size = new System.Drawing.Size(64, 13); 88 this.label2.TabIndex = 2; 89 this.label2.Text = "Assignment:"; 98 this.assignmentGroupBox.Controls.Add(this.assignmentTreeView); 99 this.assignmentGroupBox.Location = new System.Drawing.Point(217, 3); 100 this.assignmentGroupBox.Name = "assignmentGroupBox"; 101 this.assignmentGroupBox.Size = new System.Drawing.Size(240, 356); 102 this.assignmentGroupBox.TabIndex = 5; 103 this.assignmentGroupBox.TabStop = false; 104 this.assignmentGroupBox.Text = "Assignment"; 105 // 106 // characteristicsGroupBox 107 // 108 this.characteristicsGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 109 | System.Windows.Forms.AnchorStyles.Left))); 110 this.characteristicsGroupBox.Controls.Add(this.label7); 111 this.characteristicsGroupBox.Controls.Add(this.overbookedCapacityLabel); 112 this.characteristicsGroupBox.Controls.Add(this.label5); 113 this.characteristicsGroupBox.Controls.Add(this.installationQualityLabel); 114 this.characteristicsGroupBox.Controls.Add(this.label3); 115 this.characteristicsGroupBox.Controls.Add(this.flowDistanceQualityLabel); 116 this.characteristicsGroupBox.Controls.Add(this.label1); 117 this.characteristicsGroupBox.Controls.Add(this.qualityLabel); 118 this.characteristicsGroupBox.Location = new System.Drawing.Point(3, 3); 119 this.characteristicsGroupBox.Name = "characteristicsGroupBox"; 120 this.characteristicsGroupBox.Size = new System.Drawing.Size(208, 356); 121 this.characteristicsGroupBox.TabIndex = 6; 122 this.characteristicsGroupBox.TabStop = false; 123 this.characteristicsGroupBox.Text = "Characteristics"; 124 // 125 // flowDistanceQualityLabel 126 // 127 this.flowDistanceQualityLabel.AutoSize = true; 128 this.flowDistanceQualityLabel.Location = new System.Drawing.Point(91, 38); 129 this.flowDistanceQualityLabel.Margin = new System.Windows.Forms.Padding(3); 130 this.flowDistanceQualityLabel.Name = "flowDistanceQualityLabel"; 131 this.flowDistanceQualityLabel.Size = new System.Drawing.Size(10, 13); 132 this.flowDistanceQualityLabel.TabIndex = 4; 133 this.flowDistanceQualityLabel.Text = "-"; 134 // 135 // label3 136 // 137 this.label3.AutoSize = true; 138 this.label3.Location = new System.Drawing.Point(6, 38); 139 this.label3.Margin = new System.Windows.Forms.Padding(3); 140 this.label3.Name = "label3"; 141 this.label3.Size = new System.Drawing.Size(79, 13); 142 this.label3.TabIndex = 0; 143 this.label3.Text = "Flow/Distance:"; 144 // 145 // installationQualityLabel 146 // 147 this.installationQualityLabel.AutoSize = true; 148 this.installationQualityLabel.Location = new System.Drawing.Point(91, 57); 149 this.installationQualityLabel.Margin = new System.Windows.Forms.Padding(3); 150 this.installationQualityLabel.Name = "installationQualityLabel"; 151 this.installationQualityLabel.Size = new System.Drawing.Size(10, 13); 152 this.installationQualityLabel.TabIndex = 4; 153 this.installationQualityLabel.Text = "-"; 154 // 155 // label5 156 // 157 this.label5.AutoSize = true; 158 this.label5.Location = new System.Drawing.Point(6, 57); 159 this.label5.Margin = new System.Windows.Forms.Padding(3); 160 this.label5.Name = "label5"; 161 this.label5.Size = new System.Drawing.Size(60, 13); 162 this.label5.TabIndex = 0; 163 this.label5.Text = "Installation:"; 164 // 165 // overbookedCapacityLabel 166 // 167 this.overbookedCapacityLabel.AutoSize = true; 168 this.overbookedCapacityLabel.Location = new System.Drawing.Point(91, 76); 169 this.overbookedCapacityLabel.Margin = new System.Windows.Forms.Padding(3); 170 this.overbookedCapacityLabel.Name = "overbookedCapacityLabel"; 171 this.overbookedCapacityLabel.Size = new System.Drawing.Size(10, 13); 172 this.overbookedCapacityLabel.TabIndex = 4; 173 this.overbookedCapacityLabel.Text = "-"; 174 // 175 // label7 176 // 177 this.label7.AutoSize = true; 178 this.label7.Location = new System.Drawing.Point(6, 76); 179 this.label7.Margin = new System.Windows.Forms.Padding(3); 180 this.label7.Name = "label7"; 181 this.label7.Size = new System.Drawing.Size(71, 13); 182 this.label7.TabIndex = 0; 183 this.label7.Text = "Overbooking:"; 90 184 // 91 185 // GQAPAssignmentView … … 93 187 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 94 188 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 95 this.Controls.Add(this.qualityTextBox); 96 this.Controls.Add(this.label2); 97 this.Controls.Add(this.label1); 98 this.Controls.Add(this.assignmentTreeView); 189 this.Controls.Add(this.characteristicsGroupBox); 190 this.Controls.Add(this.assignmentGroupBox); 99 191 this.Name = "GQAPAssignmentView"; 100 this.Size = new System.Drawing.Size(355, 384); 192 this.Size = new System.Drawing.Size(460, 362); 193 this.assignmentGroupBox.ResumeLayout(false); 194 this.characteristicsGroupBox.ResumeLayout(false); 195 this.characteristicsGroupBox.PerformLayout(); 101 196 this.ResumeLayout(false); 102 this.PerformLayout();103 197 104 198 } … … 108 202 private System.Windows.Forms.TreeView assignmentTreeView; 109 203 private System.Windows.Forms.Label label1; 110 private System.Windows.Forms.TextBox qualityTextBox; 111 private System.Windows.Forms.Label label2; 204 private System.Windows.Forms.Label qualityLabel; 205 private System.Windows.Forms.GroupBox assignmentGroupBox; 206 private System.Windows.Forms.GroupBox characteristicsGroupBox; 207 private System.Windows.Forms.Label label3; 208 private System.Windows.Forms.Label flowDistanceQualityLabel; 209 private System.Windows.Forms.Label label7; 210 private System.Windows.Forms.Label overbookedCapacityLabel; 211 private System.Windows.Forms.Label label5; 212 private System.Windows.Forms.Label installationQualityLabel; 112 213 } 113 214 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views/3.3/GQAPAssignmentView.cs
r7412 r7415 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.ComponentModel; 24 25 using System.Linq; 25 26 using System.Windows.Forms; … … 44 45 #region Register Content Events 45 46 protected override void DeregisterContentEvents() { 46 // TODO: Deregister your event handlers on the Content here47 Content.PropertyChanged -= new PropertyChangedEventHandler(Content_PropertyChanged); 47 48 base.DeregisterContentEvents(); 48 49 } 49 50 protected override void RegisterContentEvents() { 50 51 base.RegisterContentEvents(); 51 // TODO: Register your event handlers on the Content here52 Content.PropertyChanged += new PropertyChangedEventHandler(Content_PropertyChanged); 52 53 } 53 54 #endregion … … 55 56 protected override void OnContentChanged() { 56 57 base.OnContentChanged(); 57 assignmentTreeView.Nodes.Clear(); 58 if (Content == null) { 59 qualityTextBox.Text = String.Empty; 60 } else { 61 qualityTextBox.Text = Content.Quality.Value.ToString(); 62 BuildAssignmentTree(); 63 } 58 UpdateQuality(); 59 UpdateAssignment(); 64 60 } 65 61 66 62 protected override void SetEnabledStateOfControls() { 67 63 base.SetEnabledStateOfControls(); 68 qualityTextBox.Enabled = false;69 assignmentTreeView.Enabled = true;70 64 } 71 65 72 66 #region Event Handlers 73 // TODO: Put event handlers here 67 private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) { 68 switch (e.PropertyName) { 69 case "Quality": UpdateQuality(); break; 70 case "FlowDistanceQuality": UpdateFlowDistanceQuality(); break; 71 case "InstallationQuality": UpdateInstallationQuality(); break; 72 case "OverbookedCapacity": UpdateOverbookedCapacity(); break; 73 case "Assignment": UpdateAssignment(); break; 74 case "EquipmentNames": UpdateAssignment(); break; 75 case "LocationNames": UpdateAssignment(); break; 76 default: break; 77 } 78 } 74 79 #endregion 75 80 76 private void BuildAssignmentTree() { 77 IntegerVector assignment = Content.Assignment; 78 Dictionary<int, TreeNode> locationNodes = new Dictionary<int, TreeNode>(); 79 for (int i = 0; i < assignment.Length; i++) { 80 if (!locationNodes.ContainsKey(assignment[i])) { 81 string locationName = assignment[i].ToString(); 82 if (Content.LocationNames != null && Content.LocationNames.Length > 0) 83 locationName = Content.LocationNames[assignment[i] % Content.LocationNames.Length]; 84 locationNodes.Add(assignment[i], new TreeNode(locationName)); 81 private void UpdateQuality() { 82 if (InvokeRequired) Invoke((Action)UpdateQuality); 83 else { 84 if (Content == null) { 85 qualityLabel.Text = "-"; 86 } else { 87 qualityLabel.Text = Content.Quality.ToString(); 85 88 } 86 string equipmentName = i.ToString();87 if (Content.EquipmentNames != null && Content.EquipmentNames.Length > 0)88 equipmentName = Content.EquipmentNames[i % Content.EquipmentNames.Length];89 locationNodes[assignment[i]].Nodes.Add(equipmentName);90 89 } 90 } 91 91 92 assignmentTreeView.BeginUpdate(); 93 try { 94 foreach (var node in locationNodes.OrderBy(x => x.Key).Select(x => x.Value)) { 95 assignmentTreeView.Nodes.Add(node); 96 node.Expand(); 92 private void UpdateFlowDistanceQuality() { 93 if (InvokeRequired) Invoke((Action)UpdateFlowDistanceQuality); 94 else { 95 if (Content == null || Content.FlowDistanceQuality == null) { 96 flowDistanceQualityLabel.Text = "-"; 97 } else { 98 flowDistanceQualityLabel.Text = Content.FlowDistanceQuality.ToString(); 97 99 } 98 } finally { assignmentTreeView.EndUpdate(); } 100 } 101 } 102 103 private void UpdateInstallationQuality() { 104 if (InvokeRequired) Invoke((Action)UpdateInstallationQuality); 105 else { 106 if (Content == null || Content.InstallationQuality == null) { 107 installationQualityLabel.Text = "-"; 108 } else { 109 installationQualityLabel.Text = Content.InstallationQuality.ToString(); 110 } 111 } 112 } 113 114 private void UpdateOverbookedCapacity() { 115 if (InvokeRequired) Invoke((Action)UpdateOverbookedCapacity); 116 else { 117 if (Content == null || Content.OverbookedCapacity == null) { 118 overbookedCapacityLabel.Text = "-"; 119 } else { 120 overbookedCapacityLabel.Text = Content.OverbookedCapacity.ToString(); 121 } 122 } 123 } 124 125 private void UpdateAssignment() { 126 if (InvokeRequired) Invoke((Action)UpdateAssignment); 127 else { 128 assignmentTreeView.Nodes.Clear(); 129 if (Content != null) { 130 IntegerVector assignment = Content.Assignment; 131 Dictionary<int, TreeNode> locationNodes = new Dictionary<int, TreeNode>(); 132 for (int i = 0; i < assignment.Length; i++) { 133 if (!locationNodes.ContainsKey(assignment[i])) { 134 string locationName = assignment[i].ToString(); 135 if (Content.LocationNames != null && Content.LocationNames.Length > assignment[i]) 136 locationName = Content.LocationNames[assignment[i]]; 137 locationNodes.Add(assignment[i], new TreeNode(locationName)); 138 } 139 string equipmentName = i.ToString(); 140 if (Content.EquipmentNames != null && Content.EquipmentNames.Length > i) 141 equipmentName = Content.EquipmentNames[i]; 142 locationNodes[assignment[i]].Nodes.Add(equipmentName); 143 } 144 145 assignmentTreeView.BeginUpdate(); 146 try { 147 foreach (var node in locationNodes.OrderBy(x => x.Key).Select(x => x.Value)) { 148 assignmentTreeView.Nodes.Add(node); 149 node.Expand(); 150 } 151 } finally { assignmentTreeView.EndUpdate(); } 152 } 153 } 99 154 } 100 155 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Analyzers/BestGQAPSolutionAnalyzer.cs
r7412 r7415 29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Problems.GeneralizedQuadraticAssignment.Common; 31 32 32 33 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { … … 51 52 get { return (LookupParameter<DoubleMatrix>)Parameters["Weights"]; } 52 53 } 54 public LookupParameter<DoubleMatrix> InstallationCostsParameter { 55 get { return (LookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; } 56 } 57 public LookupParameter<DoubleArray> DemandsParameter { 58 get { return (LookupParameter<DoubleArray>)Parameters["Demands"]; } 59 } 60 public LookupParameter<DoubleArray> CapacitiesParameter { 61 get { return (LookupParameter<DoubleArray>)Parameters["Capacities"]; } 62 } 63 public LookupParameter<DoubleValue> TransportationCostsParameter { 64 get { return (LookupParameter<DoubleValue>)Parameters["TransportationCosts"]; } 65 } 66 public LookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter { 67 get { return (LookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; } 68 } 53 69 public ScopeTreeLookupParameter<IntegerVector> AssignmentParameter { 54 70 get { return (ScopeTreeLookupParameter<IntegerVector>)Parameters["Assignment"]; } … … 56 72 public ScopeTreeLookupParameter<DoubleValue> QualityParameter { 57 73 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; } 74 } 75 public ScopeTreeLookupParameter<DoubleValue> FlowDistanceQualityParameter { 76 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; } 77 } 78 public ScopeTreeLookupParameter<DoubleValue> InstallationQualityParameter { 79 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["InstallationQuality"]; } 80 } 81 public ScopeTreeLookupParameter<DoubleValue> OverbookedCapacityParameter { 82 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; } 58 83 } 59 84 public LookupParameter<GQAPAssignment> BestSolutionParameter { … … 86 111 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem.")); 87 112 Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances between the locations.")); 88 Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights between the facilities.")); 113 Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights between the equipments.")); 114 Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", "The cost of installing equipment x at location y.")); 115 Parameters.Add(new LookupParameter<DoubleArray>("Demands", "The demands of the equipments.")); 116 Parameters.Add(new LookupParameter<DoubleArray>("Capacities", "The capacities at the locations.")); 117 Parameters.Add(new LookupParameter<DoubleValue>("TransportationCosts", "The transportation cost represents the flow-unit per distance-unit cost factor. This value can also be set to 1 if these costs are factored into the weights matrix already.")); 118 Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacityPenalty", "The multiplier for the constraint violation when added to the quality.")); 89 119 Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Assignment", "The GQAP solutions from which the best solution should be analyzed.")); 90 120 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the GQAP solutions which should be analyzed.")); 121 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("FlowDistanceQuality", "The flow-distance qualities of the GQAP solutions which should be analyzed.")); 122 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("InstallationQuality", "The installation qualities of the GQAP solutions which should be analyzed.")); 123 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("OverbookedCapacity", "The overbooked capacities of the GQAP solutions which should be analyzed.")); 91 124 Parameters.Add(new LookupParameter<GQAPAssignment>("BestSolution", "The best GQAP solution.")); 92 125 Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best GQAP solution should be stored.")); … … 109 142 110 143 public override IOperation Apply() { 111 DoubleMatrix distances = DistancesParameter.ActualValue; 112 DoubleMatrix weights = WeightsParameter.ActualValue; 113 ItemArray<IntegerVector> assignments = AssignmentParameter.ActualValue; 114 ItemArray<DoubleValue> qualities = QualityParameter.ActualValue; 115 ResultCollection results = ResultsParameter.ActualValue; 116 bool max = MaximizationParameter.ActualValue.Value; 117 DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue; 118 119 var sorted = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).ToArray(); 120 if (max) sorted = sorted.Reverse().ToArray(); 121 int i = sorted.First().index; 122 123 if (bestKnownQuality == null 124 || max && qualities[i].Value > bestKnownQuality.Value 125 || !max && qualities[i].Value < bestKnownQuality.Value) { 126 // if there isn't a best-known quality or we improved the best-known quality we'll add the current solution as best-known 127 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value); 128 BestKnownSolutionParameter.ActualValue = (IntegerVector)assignments[i].Clone(); 144 var assignments = AssignmentParameter.ActualValue; 145 var qualities = QualityParameter.ActualValue; 146 var equipmentNames = EquipmentNamesParameter.ActualValue; 147 var locationNames = LocationNamesParameter.ActualValue; 148 var flowDistanceQualities = FlowDistanceQualityParameter.ActualValue; 149 var installationQualities = InstallationQualityParameter.ActualValue; 150 var overbookedCapacities = OverbookedCapacityParameter.ActualValue; 151 var distances = DistancesParameter.ActualValue; 152 var weights = WeightsParameter.ActualValue; 153 var installationCosts = InstallationCostsParameter.ActualValue; 154 var demands = DemandsParameter.ActualValue; 155 var capacities = CapacitiesParameter.ActualValue; 156 var transportationCosts = TransportationCostsParameter.ActualValue; 157 var overbookedCapacityPenalty = OverbookedCapacityPenaltyParameter.ActualValue; 158 var results = ResultsParameter.ActualValue; 159 var maximization = MaximizationParameter.ActualValue.Value; 160 var bestKnownQuality = BestKnownQualityParameter.ActualValue; 161 162 int bestIndex; 163 var tmp = qualities.Select((x, index) => new { Index = index, Value = x.Value }); 164 if (maximization) bestIndex = tmp.SelectMax(x => x.Value).Index; 165 else bestIndex = tmp.SelectMin(x => x.Value).Index; 166 167 if (bestKnownQuality == null || HasSolutionImproved(bestKnownQuality.Value, qualities[bestIndex].Value, maximization)) { 168 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value); 169 BestKnownSolutionParameter.ActualValue = (IntegerVector)assignments[bestIndex].Clone(); 129 170 } 130 171 131 172 GQAPAssignment assignment = BestSolutionParameter.ActualValue; 132 173 if (assignment == null) { 133 StringArray equipmentNames = EquipmentNamesParameter.ActualValue;134 StringArray locationNames = LocationNamesParameter.ActualValue;135 assignment = new GQAPAssignment(weights, (IntegerVector)assignments[i].Clone(), new DoubleValue(qualities[i].Value), equipmentNames, locationNames);174 assignment = new GQAPAssignment((IntegerVector)assignments[bestIndex].Clone(), (DoubleValue)qualities[bestIndex].Clone(), 175 equipmentNames, locationNames, distances, weights, installationCosts, demands, capacities, transportationCosts, 176 overbookedCapacityPenalty, flowDistanceQualities[bestIndex], installationQualities[bestIndex], overbookedCapacities[bestIndex]); 136 177 assignment.Distances = distances; 137 178 BestSolutionParameter.ActualValue = assignment; 138 179 results.Add(new Result("Best GQAP Solution", assignment)); 139 180 } else { 140 if (max && assignment.Quality.Value < qualities[i].Value || 141 !max && assignment.Quality.Value > qualities[i].Value) { 181 if (HasSolutionImproved(assignment.Quality.Value, qualities[bestIndex].Value, maximization)) { 182 assignment.Assignment = (IntegerVector)assignments[bestIndex].Clone(); 183 assignment.Quality = (DoubleValue)qualities[bestIndex].Clone(); 184 assignment.EquipmentNames = equipmentNames; 185 assignment.LocationNames = locationNames; 142 186 assignment.Distances = distances; 143 187 assignment.Weights = weights; 144 assignment.Assignment = (IntegerVector)assignments[i].Clone(); 145 assignment.Quality.Value = qualities[i].Value; 188 assignment.InstallationCosts = installationCosts; 189 assignment.Demands = demands; 190 assignment.Capacities = capacities; 191 assignment.TransportationCosts = transportationCosts; 192 assignment.OverbookedCapacityPenalty = overbookedCapacityPenalty; 193 assignment.FlowDistanceQuality = (DoubleValue)flowDistanceQualities[bestIndex].Clone(); 194 assignment.InstallationQuality = (DoubleValue)installationQualities[bestIndex].Clone(); 195 assignment.OverbookedCapacity = (DoubleValue)overbookedCapacities[bestIndex].Clone(); 146 196 } 147 197 } … … 149 199 return base.Apply(); 150 200 } 201 202 private static bool HasSolutionImproved(double oldQuality, double quality, bool maximization) { 203 return maximization && oldQuality < quality || !maximization && oldQuality > quality; 204 } 151 205 } 152 206 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/GQAPAssignment.cs
r7319 r7415 33 33 34 34 [Storable] 35 private IntegerVector assignment; 36 public IntegerVector Assignment { 37 get { return assignment; } 38 set { 39 bool changed = (assignment != value); 40 assignment = value; 41 if (changed) OnPropertyChanged("Assignment"); 42 } 43 } 44 45 [Storable] 46 private DoubleValue quality; 47 public DoubleValue Quality { 48 get { return quality; } 49 set { 50 bool changed = (quality != value); 51 quality = value; 52 if (changed) OnPropertyChanged("Quality"); 53 } 54 } 55 56 [Storable] 57 private StringArray equipmentNames; 58 public StringArray EquipmentNames { 59 get { return equipmentNames; } 60 set { 61 bool changed = (equipmentNames != value); 62 equipmentNames = value; 63 if (changed) OnPropertyChanged("EquipmentNames"); 64 } 65 } 66 67 [Storable] 68 private StringArray locationNames; 69 public StringArray LocationNames { 70 get { return locationNames; } 71 set { 72 bool changed = (locationNames != value); 73 locationNames = value; 74 if (changed) OnPropertyChanged("LocationNames"); 75 } 76 } 77 78 [Storable] 35 79 private DoubleMatrix distances; 36 80 public DoubleMatrix Distances { … … 55 99 56 100 [Storable] 57 private IntegerVector assignment; 58 public IntegerVector Assignment { 59 get { return assignment; } 60 set { 61 bool changed = (assignment != value); 62 assignment = value; 63 if (changed) OnPropertyChanged("Assignment"); 64 } 65 } 66 67 [Storable] 68 private DoubleValue quality; 69 public DoubleValue Quality { 70 get { return quality; } 71 set { 72 bool changed = (quality != value); 73 quality = value; 74 if (changed) OnPropertyChanged("Quality"); 75 } 76 } 77 78 [Storable] 79 private StringArray equipmentNames; 80 public StringArray EquipmentNames { 81 get { return equipmentNames; } 82 set { 83 bool changed = (equipmentNames != value); 84 equipmentNames = value; 85 if (changed) OnPropertyChanged("EquipmentNames"); 86 } 87 } 88 89 [Storable] 90 private StringArray locationNames; 91 public StringArray LocationNames { 92 get { return locationNames; } 93 set { 94 bool changed = (locationNames != value); 95 locationNames = value; 96 if (changed) OnPropertyChanged("LocationNames"); 101 private DoubleMatrix installationCosts; 102 public DoubleMatrix InstallationCosts { 103 get { return installationCosts; } 104 set { 105 bool changed = (installationCosts != value); 106 installationCosts = value; 107 if (changed) OnPropertyChanged("InstallationCosts"); 108 } 109 } 110 111 [Storable] 112 private DoubleArray demands; 113 public DoubleArray Demands { 114 get { return demands; } 115 set { 116 bool changed = (demands != value); 117 demands = value; 118 if (changed) OnPropertyChanged("Demands"); 119 } 120 } 121 122 [Storable] 123 private DoubleArray capacities; 124 public DoubleArray Capacities { 125 get { return capacities; } 126 set { 127 bool changed = (capacities != value); 128 capacities = value; 129 if (changed) OnPropertyChanged("Capacities"); 130 } 131 } 132 133 [Storable] 134 private DoubleValue transportationCosts; 135 public DoubleValue TransportationCosts { 136 get { return transportationCosts; } 137 set { 138 bool changed = (transportationCosts != value); 139 transportationCosts = value; 140 if (changed) OnPropertyChanged("TransportationCosts"); 141 } 142 } 143 144 [Storable] 145 private DoubleValue overbookedCapacityPenalty; 146 public DoubleValue OverbookedCapacityPenalty { 147 get { return overbookedCapacityPenalty; } 148 set { 149 bool changed = (overbookedCapacityPenalty != value); 150 overbookedCapacityPenalty = value; 151 if (changed) OnPropertyChanged("OverbookedCapacityPenalty"); 152 } 153 } 154 155 [Storable] 156 private DoubleValue flowDistanceQuality; 157 public DoubleValue FlowDistanceQuality { 158 get { return flowDistanceQuality; } 159 set { 160 bool changed = (flowDistanceQuality != value); 161 flowDistanceQuality = value; 162 if (changed) OnPropertyChanged("FlowDistanceQuality"); 163 } 164 } 165 166 [Storable] 167 private DoubleValue installationQuality; 168 public DoubleValue InstallationQuality { 169 get { return installationQuality; } 170 set { 171 bool changed = (installationQuality != value); 172 installationQuality = value; 173 if (changed) OnPropertyChanged("InstallationQuality"); 174 } 175 } 176 177 [Storable] 178 private DoubleValue overbookedCapacity; 179 public DoubleValue OverbookedCapacity { 180 get { return overbookedCapacity; } 181 set { 182 bool changed = (overbookedCapacity != value); 183 overbookedCapacity = value; 184 if (changed) OnPropertyChanged("OverbookedCapacity"); 97 185 } 98 186 } … … 102 190 private GQAPAssignment(GQAPAssignment original, Cloner cloner) 103 191 : base(original, cloner) { 104 distances = cloner.Clone(original.distances);105 weights = cloner.Clone(original.weights);106 192 assignment = cloner.Clone(original.assignment); 107 193 quality = cloner.Clone(original.quality); 108 194 equipmentNames = cloner.Clone(original.equipmentNames); 109 195 locationNames = cloner.Clone(original.locationNames); 110 } 111 public GQAPAssignment(DoubleMatrix weights, IntegerVector assignment) { 112 this.weights = weights; 196 distances = cloner.Clone(original.distances); 197 weights = cloner.Clone(original.weights); 198 installationCosts = cloner.Clone(original.installationCosts); 199 demands = cloner.Clone(original.demands); 200 capacities = cloner.Clone(original.capacities); 201 transportationCosts = cloner.Clone(original.transportationCosts); 202 overbookedCapacityPenalty = cloner.Clone(original.overbookedCapacityPenalty); 203 flowDistanceQuality = cloner.Clone(original.flowDistanceQuality); 204 installationQuality = cloner.Clone(original.installationQuality); 205 overbookedCapacity = cloner.Clone(original.overbookedCapacity); 206 } 207 public GQAPAssignment(IntegerVector assignment, DoubleValue quality) 208 : base() { 113 209 this.assignment = assignment; 114 }115 public GQAPAssignment(DoubleMatrix weights, IntegerVector assignment, DoubleValue quality)116 : this(weights, assignment) {117 210 this.quality = quality; 118 211 } 119 public GQAPAssignment( DoubleMatrix weights,IntegerVector assignment, DoubleValue quality, StringArray equipmentNames, StringArray locationNames)120 : this( weights,assignment, quality) {212 public GQAPAssignment(IntegerVector assignment, DoubleValue quality, StringArray equipmentNames, StringArray locationNames) 213 : this(assignment, quality) { 121 214 this.equipmentNames = equipmentNames; 122 215 this.locationNames = locationNames; 216 } 217 public GQAPAssignment(IntegerVector assignment, DoubleValue quality, StringArray equipmentNames, StringArray locationNames, 218 DoubleMatrix distances, DoubleMatrix weights, DoubleMatrix installationCosts, DoubleArray demands, DoubleArray capacities, 219 DoubleValue transportationCosts, DoubleValue overbookedCapacityPenalty, DoubleValue flowDistanceQuality, 220 DoubleValue installationQuality, DoubleValue overbookedCapacity) 221 : this(assignment, quality, equipmentNames, locationNames) { 222 this.distances = distances; 223 this.weights = weights; 224 this.installationCosts = installationCosts; 225 this.demands = demands; 226 this.capacities = capacities; 227 this.transportationCosts = transportationCosts; 228 this.overbookedCapacityPenalty = overbookedCapacityPenalty; 229 this.flowDistanceQuality = flowDistanceQuality; 230 this.installationQuality = installationQuality; 231 this.overbookedCapacity = overbookedCapacity; 123 232 } 124 233 -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/GeneralizedQuadraticAssignmentProblem.cs
r7413 r7415 68 68 get { return (OptionalValueParameter<IItem>)Parameters["BestKnownSolution"]; } 69 69 } 70 public OptionalValueParameter<StringArray> EquipmentNamesParameter { 71 get { return (OptionalValueParameter<StringArray>)Parameters["EquipmentNames"]; } 72 } 73 public OptionalValueParameter<StringArray> LocationNamesParameter { 74 get { return (OptionalValueParameter<StringArray>)Parameters["LocationNames"]; } 75 } 70 76 #endregion 71 77 … … 99 105 set { TransportationCostsParameter.Value.Value = value; } 100 106 } 107 public StringArray EquipmentNames { 108 get { return EquipmentNamesParameter.Value; } 109 set { EquipmentNamesParameter.Value = value; } 110 } 111 public StringArray LocationNames { 112 get { return LocationNamesParameter.Value; } 113 set { LocationNamesParameter.Value = value; } 114 } 101 115 #endregion 102 116 103 [Storable]104 private BestGQAPSolutionAnalyzer bestSolutionAnalyzer;105 117 public BestGQAPSolutionAnalyzer BestSolutionAnalyzer { 106 get { return bestSolutionAnalyzer; } 107 set { bestSolutionAnalyzer = value; } 118 get { return Operators.OfType<BestGQAPSolutionAnalyzer>().First(); } 108 119 } 109 120 … … 112 123 private GeneralizedQuadraticAssignmentProblem(GeneralizedQuadraticAssignmentProblem original, Cloner cloner) 113 124 : base(original, cloner) { 114 bestSolutionAnalyzer = cloner.Clone(original.bestSolutionAnalyzer);115 125 AttachEventHandlers(); 116 126 } … … 201 211 202 212 private void InitializeOperators() { 213 Operators.Add(new BestGQAPSolutionAnalyzer()); 214 Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPOperator>()); 203 215 Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>()); 204 Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPOperator>());205 216 Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator); 206 217 Operators.AddRange(ApplicationManager.Manager.GetInstances<IGQAPMoveEvaluator>()); 207 Operators.Add(new BestGQAPSolutionAnalyzer());208 218 Parameterize(); 209 219 } … … 276 286 277 287 if (BestSolutionAnalyzer != null) { 278 BestSolutionAnalyzer. QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;288 BestSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name; 279 289 BestSolutionAnalyzer.DistancesParameter.ActualName = DistancesParameter.Name; 280 290 BestSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name; 291 BestSolutionAnalyzer.InstallationCostsParameter.ActualName = InstallationCostsParameter.Name; 292 BestSolutionAnalyzer.DemandsParameter.ActualName = DemandsParameter.Name; 293 BestSolutionAnalyzer.CapacitiesParameter.ActualName = CapacitiesParameter.Name; 294 BestSolutionAnalyzer.TransportationCostsParameter.ActualName = TransportationCostsParameter.Name; 295 BestSolutionAnalyzer.OverbookedCapacityPenaltyParameter.ActualName = OverbookedCapacityPenaltyParameter.Name; 281 296 BestSolutionAnalyzer.AssignmentParameter.ActualName = SolutionCreator.AssignmentParameter.ActualName; 297 BestSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 298 BestSolutionAnalyzer.FlowDistanceQualityParameter.ActualName = Evaluator.FlowDistanceQualityParameter.ActualName; 299 BestSolutionAnalyzer.InstallationQualityParameter.ActualName = Evaluator.InstallationQualityParameter.ActualName; 300 BestSolutionAnalyzer.OverbookedCapacityParameter.ActualName = Evaluator.OverbookedCapacityParameter.ActualName; 282 301 BestSolutionAnalyzer.ResultsParameter.ActualName = "Results"; 283 302 BestSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name; 284 BestSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name; 303 BestSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name; 304 BestSolutionAnalyzer.EquipmentNamesParameter.ActualName = EquipmentNamesParameter.Name; 305 BestSolutionAnalyzer.LocationNamesParameter.ActualName = LocationNamesParameter.Name; 285 306 } 286 307 } -
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/GreedyRandomizedSolutionCreator.cs
r7412 r7415 60 60 while (tries < maxTries) { 61 61 assignment.Clear(); 62 capacities.Select((v, i) => slack[i] = v).Enumerate();62 slack = capacities.Select((v, i) => new { Index = i, Value = v }).ToDictionary(x => x.Index, y => y.Value); 63 63 HashSet<int> CF = new HashSet<int>(), // set of chosen facilities / equipments 64 64 T = new HashSet<int>(), // set of facilities / equpiments that can be assigned to the set of chosen locations (CL)
Note: See TracChangeset
for help on using the changeset viewer.