You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Luc Maisonobe <Lu...@free.fr> on 2011/03/06 13:55:13 UTC

Re: svn commit: r1078445 - /commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/CMAESOptimizer.java

Le 06/03/2011 11:58, sebb@apache.org a écrit :
> Author: sebb
> Date: Sun Mar  6 10:58:23 2011
> New Revision: 1078445
> 
> URL: http://svn.apache.org/viewvc?rev=1078445&view=rev
> Log:
> Rename private variables to avoid name shadowing

I don't understand this change. Up to now, our naming convention was to
avoid such underscore prefix/postfix.
As the Fitness inner class is not static, as it is built directly using
the attributes from the enclosing class, and as the attributes seems to
be immutable, wouldn't it be better to simply remove the attributes from
the inner class and directly reference the attributes from the enclosing
class without any shadowing ?

Luc

> 
> Modified:
>     commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/CMAESOptimizer.java
> 
> Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/CMAESOptimizer.java
> URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/CMAESOptimizer.java?rev=1078445&r1=1078444&r2=1078445&view=diff
> ==============================================================================
> --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/CMAESOptimizer.java (original)
> +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/CMAESOptimizer.java Sun Mar  6 10:58:23 2011
> @@ -827,7 +827,7 @@ public class CMAESOptimizer extends
>      private class FitnessFunction {
>  
>          /** Optional bounds for the objective variables */
> -        private final double[][] boundaries;
> +        private final double[][] _boundaries;
>          /** Determines the penalty for boundary violations */
>          private double valueRange = 1.0;
>          /**
> @@ -836,7 +836,7 @@ public class CMAESOptimizer extends
>           */
>          private boolean isRepairMode = true;
>          /** Flag indicating the optimization goal. */
> -        private final boolean isMinimize;
> +        private final boolean _isMinimize;
>  
>          /**
>           * @param boundaries
> @@ -846,8 +846,8 @@ public class CMAESOptimizer extends
>           */
>          private FitnessFunction(final double[][] boundaries,
>                  final boolean isMinimize) {
> -            this.boundaries = boundaries;
> -            this.isMinimize = isMinimize;
> +            this._boundaries = boundaries;
> +            this._isMinimize = isMinimize;
>          }
>  
>          /**
> @@ -856,12 +856,12 @@ public class CMAESOptimizer extends
>           * @return Normalized objective variables.
>           */
>          private double[] encode(final double[] x) {
> -            if (boundaries == null)
> +            if (_boundaries == null)
>                  return x;
>              double[] res = new double[x.length];
>              for (int i = 0; i < x.length; i++) {
> -                double diff = boundaries[1][i] - boundaries[0][i];
> -                res[i] = (x[i] - boundaries[0][i]) / diff;
> +                double diff = _boundaries[1][i] - _boundaries[0][i];
> +                res[i] = (x[i] - _boundaries[0][i]) / diff;
>              }
>              return res;
>          }
> @@ -872,12 +872,12 @@ public class CMAESOptimizer extends
>           * @return Original objective variables.
>           */
>          private double[] decode(final double[] x) {
> -            if (boundaries == null)
> +            if (_boundaries == null)
>                  return x;
>              double[] res = new double[x.length];
>              for (int i = 0; i < x.length; i++) {
> -                double diff = boundaries[1][i] - boundaries[0][i];
> -                res[i] = diff * x[i] + boundaries[0][i];
> +                double diff = _boundaries[1][i] - _boundaries[0][i];
> +                res[i] = diff * x[i] + _boundaries[0][i];
>              }
>              return res;
>          }
> @@ -889,7 +889,7 @@ public class CMAESOptimizer extends
>           */
>          private double value(final double[] point) {
>              double value;
> -            if (boundaries != null && isRepairMode) {
> +            if (_boundaries != null && isRepairMode) {
>                  double[] repaired = repair(point);
>                  value = CMAESOptimizer.this
>                          .computeObjectiveValue(decode(repaired)) +
> @@ -897,7 +897,7 @@ public class CMAESOptimizer extends
>              } else
>                  value = CMAESOptimizer.this
>                          .computeObjectiveValue(decode(point));
> -            return isMinimize ? value : -value;
> +            return _isMinimize ? value : -value;
>          }
>  
>          /**
> @@ -906,7 +906,7 @@ public class CMAESOptimizer extends
>           * @return True if in bounds
>           */
>          private boolean isFeasible(final double[] x) {
> -            if (boundaries == null)
> +            if (_boundaries == null)
>                  return true;
>              for (int i = 0; i < x.length; i++) {
>                  if (x[i] < 0)
> @@ -956,7 +956,7 @@ public class CMAESOptimizer extends
>                  double diff = Math.abs(x[i] - repaired[i]);
>                  penalty += diff * valueRange;
>              }
> -            return isMinimize ? penalty : -penalty;
> +            return _isMinimize ? penalty : -penalty;
>          }
>      }
>  
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1078445 - /commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/CMAESOptimizer.java

Posted by sebb <se...@gmail.com>.
On 6 March 2011 12:55, Luc Maisonobe <Lu...@free.fr> wrote:
> Le 06/03/2011 11:58, sebb@apache.org a écrit :
>> Author: sebb
>> Date: Sun Mar  6 10:58:23 2011
>> New Revision: 1078445
>>
>> URL: http://svn.apache.org/viewvc?rev=1078445&view=rev
>> Log:
>> Rename private variables to avoid name shadowing
>
> I don't understand this change. Up to now, our naming convention was to
> avoid such underscore prefix/postfix.

Sorry, I've been working on NET which uses _ extensively.

> As the Fitness inner class is not static, as it is built directly using
> the attributes from the enclosing class, and as the attributes seems to
> be immutable, wouldn't it be better to simply remove the attributes from
> the inner class and directly reference the attributes from the enclosing
> class without any shadowing ?

I did consider that, but thought that was a larger change.

Anyway, now implemented.

> Luc
>
>>
>> Modified:
>>     commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/CMAESOptimizer.java
>>
>> Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/CMAESOptimizer.java
>> URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/CMAESOptimizer.java?rev=1078445&r1=1078444&r2=1078445&view=diff
>> ==============================================================================
>> --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/CMAESOptimizer.java (original)
>> +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/CMAESOptimizer.java Sun Mar  6 10:58:23 2011
>> @@ -827,7 +827,7 @@ public class CMAESOptimizer extends
>>      private class FitnessFunction {
>>
>>          /** Optional bounds for the objective variables */
>> -        private final double[][] boundaries;
>> +        private final double[][] _boundaries;
>>          /** Determines the penalty for boundary violations */
>>          private double valueRange = 1.0;
>>          /**
>> @@ -836,7 +836,7 @@ public class CMAESOptimizer extends
>>           */
>>          private boolean isRepairMode = true;
>>          /** Flag indicating the optimization goal. */
>> -        private final boolean isMinimize;
>> +        private final boolean _isMinimize;
>>
>>          /**
>>           * @param boundaries
>> @@ -846,8 +846,8 @@ public class CMAESOptimizer extends
>>           */
>>          private FitnessFunction(final double[][] boundaries,
>>                  final boolean isMinimize) {
>> -            this.boundaries = boundaries;
>> -            this.isMinimize = isMinimize;
>> +            this._boundaries = boundaries;
>> +            this._isMinimize = isMinimize;
>>          }
>>
>>          /**
>> @@ -856,12 +856,12 @@ public class CMAESOptimizer extends
>>           * @return Normalized objective variables.
>>           */
>>          private double[] encode(final double[] x) {
>> -            if (boundaries == null)
>> +            if (_boundaries == null)
>>                  return x;
>>              double[] res = new double[x.length];
>>              for (int i = 0; i < x.length; i++) {
>> -                double diff = boundaries[1][i] - boundaries[0][i];
>> -                res[i] = (x[i] - boundaries[0][i]) / diff;
>> +                double diff = _boundaries[1][i] - _boundaries[0][i];
>> +                res[i] = (x[i] - _boundaries[0][i]) / diff;
>>              }
>>              return res;
>>          }
>> @@ -872,12 +872,12 @@ public class CMAESOptimizer extends
>>           * @return Original objective variables.
>>           */
>>          private double[] decode(final double[] x) {
>> -            if (boundaries == null)
>> +            if (_boundaries == null)
>>                  return x;
>>              double[] res = new double[x.length];
>>              for (int i = 0; i < x.length; i++) {
>> -                double diff = boundaries[1][i] - boundaries[0][i];
>> -                res[i] = diff * x[i] + boundaries[0][i];
>> +                double diff = _boundaries[1][i] - _boundaries[0][i];
>> +                res[i] = diff * x[i] + _boundaries[0][i];
>>              }
>>              return res;
>>          }
>> @@ -889,7 +889,7 @@ public class CMAESOptimizer extends
>>           */
>>          private double value(final double[] point) {
>>              double value;
>> -            if (boundaries != null && isRepairMode) {
>> +            if (_boundaries != null && isRepairMode) {
>>                  double[] repaired = repair(point);
>>                  value = CMAESOptimizer.this
>>                          .computeObjectiveValue(decode(repaired)) +
>> @@ -897,7 +897,7 @@ public class CMAESOptimizer extends
>>              } else
>>                  value = CMAESOptimizer.this
>>                          .computeObjectiveValue(decode(point));
>> -            return isMinimize ? value : -value;
>> +            return _isMinimize ? value : -value;
>>          }
>>
>>          /**
>> @@ -906,7 +906,7 @@ public class CMAESOptimizer extends
>>           * @return True if in bounds
>>           */
>>          private boolean isFeasible(final double[] x) {
>> -            if (boundaries == null)
>> +            if (_boundaries == null)
>>                  return true;
>>              for (int i = 0; i < x.length; i++) {
>>                  if (x[i] < 0)
>> @@ -956,7 +956,7 @@ public class CMAESOptimizer extends
>>                  double diff = Math.abs(x[i] - repaired[i]);
>>                  penalty += diff * valueRange;
>>              }
>> -            return isMinimize ? penalty : -penalty;
>> +            return _isMinimize ? penalty : -penalty;
>>          }
>>      }
>>
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org