File size: 6,879 Bytes
b1b3bae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | using System;
using System.Collections.Generic;
using System.Text;
namespace DotNumerics.Optimization.Cobyla
{
internal class CobylaDriver
{
#region Fields
COBYLA MeCOBYLA;
private OptMultivariateFunction Function;
private OptSimplexBoundVariable[] MeSimplexBoundVariableList;
protected double[] MeVariables;
protected double[] MeFreeVariables;
protected int MeNumFreeVariables = 0;
private int MeNumBoundVariables = 0;
private double MeInitialStep = 1;
private int MeFunEvaluations = 0;
double[] W;
int[] IACT;
#endregion
#region Constructor
public CobylaDriver()
{
CALCFC fun = new CALCFC(InternalFunction);
this.MeCOBYLA = new COBYLA(fun);
}
#endregion
#region Methods
public double[] ComputeMin(OptMultivariateFunction function, OptSimplexBoundVariable[] variables, double initialStep, double tolerance, ref int MAXFUN)
{
this.Function = function;
this.MeFunEvaluations = 0;
this.MeSimplexBoundVariableList = OptSimplexBoundVariable.GetClon(variables);
this.MeInitialStep = initialStep;
this.InitializeVariables();
this.InitializeWorkSpace();
if (this.MeNumFreeVariables == 0) return this.MeVariables;
this.MeCOBYLA.Run(this.MeNumFreeVariables, this.MeNumBoundVariables, ref this.MeFreeVariables, 0, initialStep, tolerance, 0, ref MAXFUN, ref W, 0, ref IACT, 0);
int varFreeVarIndex = 0;
for (int i = 0; i < this.MeSimplexBoundVariableList.Length; i++)
{
if (this.MeSimplexBoundVariableList[i].Fixed == false)
{
//mod
this.MeVariables[i] = this.MeFreeVariables[varFreeVarIndex] * this.MeSimplexBoundVariableList[i].ScaleFactor;
varFreeVarIndex++;
}
}
MAXFUN = this.MeFunEvaluations;
return this.MeVariables;
}
private void InitializeVariables()
{
this.MeNumFreeVariables = 0;
this.MeNumBoundVariables = 0;
foreach (OptSimplexBoundVariable var in this.MeSimplexBoundVariableList)
{
if (var.Fixed == false)
{
this.MeNumFreeVariables++;
//Se invierten los limites de ser necesario
if (var.LowerBound > var.UpperBound)
{
double tempBound = var.LowerBound;
var.LowerBound = var.UpperBound;
var.UpperBound = tempBound;
}
//Se revisa que la variable inicial este dentro de los limites
if (var.InitialGuess < var.LowerBound)
{
var.InitialGuess = var.LowerBound;
}
if (var.InitialGuess > var.UpperBound)
{
var.InitialGuess = var.UpperBound;
}
if (var.InitialGuess == 0) var.InitialGuess = Math.Min(1, var.UpperBound); // Si es 0 no ocurre ningun cambio asi que se modifica el valor
if (var.LowerBound == double.NegativeInfinity && var.UpperBound == double.PositiveInfinity) var.UseBounds = false;
if (var.UseBounds == true) this.MeNumBoundVariables++;
}
}
this.MeVariables = new double[this.MeSimplexBoundVariableList.Length];
int index = 0;
foreach (OptSimplexBoundVariable var in this.MeSimplexBoundVariableList)
{
this.MeVariables[index] = var.InitialGuess;
index++;
}
this.MeFreeVariables = new double[this.MeNumFreeVariables];
index = 0;
foreach (OptSimplexBoundVariable var in this.MeSimplexBoundVariableList)
{
if (var.Fixed == false)
{
//mod
this.MeFreeVariables[index] = var.InitialGuess / var.ScaleFactor;
index++;
}
}
}
private void InitializeWorkSpace()
{
//The arguments W and IACT provide real and
// C integer arrays that are used as working space. Their lengths must be at
// C least N*(3*N+2*M+11)+4*M+6 and M+1 respectively.
int N = this.MeNumFreeVariables;
int M = this.MeNumBoundVariables;
int wDim = N * (3 * N + 2 * M + 11) + 4 * M + 6;
this.W = new double[wDim];
this.IACT = new int[M + 1];
}
#endregion
private void InternalFunction(int N, int M, double[] X, int o_x, ref double F, ref double[] CON, int o_con)
{
this.MeFunEvaluations++;
int varFreeVarIndex = 0;
for (int i = 0; i < this.MeSimplexBoundVariableList.Length; i++)
{
if (this.MeSimplexBoundVariableList[i].Fixed == false)
{
//mod
this.MeVariables[i] = X[varFreeVarIndex + o_x] * this.MeSimplexBoundVariableList[i].ScaleFactor;
varFreeVarIndex++;
}
}
F = this.Function(this.MeVariables);
varFreeVarIndex = 0;
int index = 0;
double valLower = 0;
double valUpper = 0;
double fact = Math.Max(1, Math.Abs(F));
for (int i = 0; i < this.MeSimplexBoundVariableList.Length; i++)
{
if (this.MeSimplexBoundVariableList[i].Fixed == false)
{
if (this.MeSimplexBoundVariableList[i].UseBounds)
{
if (fact == 0) fact = 1;
//mod
valLower = fact * (X[varFreeVarIndex + o_x] - this.MeSimplexBoundVariableList[i].LowerBound / this.MeSimplexBoundVariableList[i].ScaleFactor);
//mod
valUpper = fact * (this.MeSimplexBoundVariableList[i].UpperBound / this.MeSimplexBoundVariableList[i].ScaleFactor - X[varFreeVarIndex + o_x]);
CON[index + o_con] = Math.Min(valLower, valUpper);
index++;
}
varFreeVarIndex++;
}
}
}
}
}
|