Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5855


Ignore:
Timestamp:
03/29/11 01:42:41 (13 years ago)
Author:
abeham
Message:

#1330

  • Fixed calculation of normalized stress value
  • Fixed a few bugs
Location:
branches/QAP
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/QAP/HeuristicLab.Analysis/3.3/MultidimensionalScaling/MultidimensionalScaling.cs

    r5723 r5855  
    6161      double epsf = 0;
    6262      double epsx = 0;
    63       int maxits = 1000;
     63      int maxits = 100;
    6464      alglib.mincgstate state = null;
    6565      alglib.mincgreport rep;
     
    6969          double[] c = new double[] { coordinates[i, 0], coordinates[i, 1] };
    7070
    71           if (iterations == 0 && i == 0) {
    72             alglib.mincgcreate(c, out state);
    73             alglib.mincgsetcond(state, epsg, epsf, epsx, maxits);
    74           } else {
    75             alglib.mincgrestartfrom(state, c);
     71          try {
     72            if ((iterations == 0 && i == 0)) {
     73              alglib.mincgcreate(c, out state);
     74              alglib.mincgsetcond(state, epsg, epsf, epsx, maxits);
     75            } else {
     76              alglib.mincgrestartfrom(state, c);
     77            }
     78            alglib.mincgoptimize(state, StressGradient, null, new Info(coordinates, distances, i));
     79            alglib.mincgresults(state, out c, out rep);
     80          } catch (alglib.alglibexception e) { }
     81          if (!double.IsNaN(c[0]) && !double.IsNaN(c[1])) {
     82            coordinates[i, 0] = c[0];
     83            coordinates[i, 1] = c[1];
    7684          }
    77           alglib.mincgoptimize(state, StressGradient, null, new Info(coordinates, distances, i));
    78           alglib.mincgresults(state, out c, out rep);
    79 
    80           coordinates[i, 0] = c[0];
    81           coordinates[i, 1] = c[1];
    8285        }
    8386      }
     
    112115
    113116    public static double CalculateNormalizedStress(int dimension, DoubleMatrix distances, DoubleMatrix coordinates) {
    114       double stress = 0;
     117      double stress = 0, normalization = 0;
    115118      for (int i = 0; i < dimension - 1; i++) {
    116119        for (int j = i + 1; j < dimension; j++) {
    117120          if (distances[i, j] != 0) {
    118             stress += Stress(coordinates[i, 0], coordinates[i, 1], distances[i, j], coordinates[j, 0], coordinates[j, 1])
    119               / (distances[i, j] * distances[i, j]);
     121            stress += Stress(coordinates[i, 0], coordinates[i, 1], distances[i, j], coordinates[j, 0], coordinates[j, 1]);
     122            normalization += (distances[i, j] * distances[i, j]);
    120123          }
    121124        }
    122125      }
    123       return stress;
     126      return Math.Sqrt(stress / normalization); ;
    124127    }
    125128
  • branches/QAP/HeuristicLab.Problems.QuadraticAssignment.Views/3.3/QAPView.cs

    r5820 r5855  
    155155
    156156    private void WriteCenteredTextToBitmap(ref Bitmap bitmap, string text) {
     157      if (bitmap == null) return;
    157158      using (Graphics g = Graphics.FromImage(bitmap)) {
    158159        g.TextRenderingHint = TextRenderingHint.AntiAlias;
  • branches/QAP/HeuristicLab.Problems.QuadraticAssignment/3.3/QuadraticAssignmentProblem.cs

    r5838 r5855  
    104104      : base() {
    105105      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution which is updated whenever a new better solution is found or may be the optimal solution if it is known beforehand.", null));
    106       Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The coordinates of the locations. If this is changed the distance matrix is calculated automatically using the euclidean distance."));
     106      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The coordinates of the locations. If this is changed the distance matrix is calculated automatically using the euclidean distance."));
    107107      Parameters.Add(new ValueParameter<DoubleMatrix>("Weights", "The strength of the connection between the facilities.", new DoubleMatrix(5, 5)));
    108108      Parameters.Add(new ValueParameter<DoubleMatrix>("Distances", "The distance matrix which can either be specified directly without the coordinates, or can be calculated automatically from the coordinates.", new DoubleMatrix(5, 5)));
     
    178178    private void WeightsParameter_ValueChanged(object sender, EventArgs e) {
    179179      Weights.RowsChanged += new EventHandler(Weights_RowsChanged);
     180      Weights.ColumnsChanged += new EventHandler(Weights_ColumnsChanged);
    180181      ParameterizeSolutionCreator();
    181182      ParameterizeEvaluator();
    182183      ParameterizeOperators();
     184      AdjustDistanceMatrix();
    183185    }
    184186    private void Weights_RowsChanged(object sender, EventArgs e) {
     187      if (Weights.Rows != Weights.Columns)
     188        ((IStringConvertibleMatrix)Weights).Columns = Weights.Rows;
     189      else {
     190        ParameterizeSolutionCreator();
     191        ParameterizeEvaluator();
     192        ParameterizeOperators();
     193        AdjustDistanceMatrix();
     194      }
     195    }
     196    private void Weights_ColumnsChanged(object sender, EventArgs e) {
     197      if (Weights.Rows != Weights.Columns)
     198        ((IStringConvertibleMatrix)Weights).Rows = Weights.Columns;
     199      else {
     200        ParameterizeSolutionCreator();
     201        ParameterizeEvaluator();
     202        ParameterizeOperators();
     203        AdjustDistanceMatrix();
     204      }
     205    }
     206    private void DistancesParameter_ValueChanged(object sender, EventArgs e) {
     207      Distances.RowsChanged += new EventHandler(Distances_RowsChanged);
     208      Distances.ColumnsChanged += new EventHandler(Distances_ColumnsChanged);
    185209      ParameterizeSolutionCreator();
    186210      ParameterizeEvaluator();
    187211      ParameterizeOperators();
     212      AdjustWeightsMatrix();
     213    }
     214    private void Distances_RowsChanged(object sender, EventArgs e) {
     215      if (Distances.Rows != Distances.Columns)
     216        ((IStringConvertibleMatrix)Distances).Columns = Distances.Rows;
     217      else {
     218        ParameterizeSolutionCreator();
     219        ParameterizeEvaluator();
     220        ParameterizeOperators();
     221        AdjustWeightsMatrix();
     222      }
     223    }
     224    private void Distances_ColumnsChanged(object sender, EventArgs e) {
     225      if (Distances.Rows != Distances.Columns)
     226        ((IStringConvertibleMatrix)Distances).Rows = Distances.Columns;
     227      else {
     228        ParameterizeSolutionCreator();
     229        ParameterizeEvaluator();
     230        ParameterizeOperators();
     231        AdjustWeightsMatrix();
     232      }
    188233    }
    189234    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
    190       Coordinates.Reset += new EventHandler(Coordinates_Reset);
    191       Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
    192       UpdateDistanceMatrix();
     235      if (Coordinates != null) {
     236        Coordinates.Reset += new EventHandler(Coordinates_Reset);
     237        Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
     238        UpdateDistancesFromCoordinates();
     239      }
    193240    }
    194241    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
    195       UpdateDistanceMatrix();
     242      UpdateDistancesFromCoordinates();
    196243    }
    197244    private void Coordinates_Reset(object sender, EventArgs e) {
    198       UpdateDistanceMatrix();
     245      UpdateDistancesFromCoordinates();
    199246    }
    200247    #endregion
     
    211258      WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
    212259      Weights.RowsChanged += new EventHandler(Weights_RowsChanged);
     260      Weights.ColumnsChanged += new EventHandler(Weights_ColumnsChanged);
     261      DistancesParameter.ValueChanged += new EventHandler(DistancesParameter_ValueChanged);
     262      Distances.RowsChanged += new EventHandler(Distances_RowsChanged);
     263      Distances.ColumnsChanged += new EventHandler(Distances_ColumnsChanged);
    213264      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
    214265      Coordinates.Reset += new EventHandler(Coordinates_Reset);
     
    273324    }
    274325
    275     private void UpdateDistanceMatrix() {
     326    private void AdjustDistanceMatrix() {
     327      if (Distances.Rows != Weights.Rows || Distances.Columns != Weights.Columns) {
     328        ((IStringConvertibleMatrix)Distances).Rows = Weights.Rows;
     329      }
     330    }
     331
     332    private void AdjustWeightsMatrix() {
     333      if (Weights.Rows != Distances.Rows || Weights.Columns != Distances.Columns) {
     334        ((IStringConvertibleMatrix)Weights).Rows = Distances.Rows;
     335      }
     336    }
     337
     338    private void UpdateDistancesFromCoordinates() {
    276339      if (Coordinates != null && Coordinates.Columns == 2 && Coordinates.Rows > 1) {
    277340        DoubleMatrix distance = new DoubleMatrix(Coordinates.Rows, Coordinates.Rows);
Note: See TracChangeset for help on using the changeset viewer.