[8371] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8371] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
[8401] | 29 | namespace HeuristicLab.Algorithms.GradientDescent {
|
---|
[8371] | 30 | [StorableClass]
|
---|
[8396] | 31 | [Item("LbfgsState", "Internal state for the limited-memory BFGS optimization algorithm.")]
|
---|
| 32 | public sealed class LbfgsState : Item {
|
---|
[8371] | 33 | private alglib.minlbfgs.minlbfgsstate state;
|
---|
| 34 | public alglib.minlbfgs.minlbfgsstate State { get { return state; } }
|
---|
| 35 |
|
---|
| 36 | [StorableConstructor]
|
---|
[8396] | 37 | private LbfgsState(bool deserializing)
|
---|
[8371] | 38 | : base(deserializing) {
|
---|
| 39 | state = new alglib.minlbfgs.minlbfgsstate();
|
---|
| 40 | }
|
---|
[8396] | 41 | private LbfgsState(LbfgsState original, Cloner cloner)
|
---|
[8371] | 42 | : base(original, cloner) {
|
---|
| 43 | this.state = new alglib.minlbfgs.minlbfgsstate();
|
---|
| 44 | this.state.autobuf = CopyArray(original.state.autobuf);
|
---|
| 45 | this.state.d = CopyArray(original.state.d);
|
---|
| 46 | this.state.denseh = CopyArray(original.state.denseh);
|
---|
| 47 | this.state.diagh = CopyArray(original.state.diagh);
|
---|
| 48 | this.state.diffstep = original.state.diffstep;
|
---|
| 49 | this.state.epsf = original.state.epsf;
|
---|
| 50 | this.state.epsg = original.state.epsg;
|
---|
| 51 | this.state.epsx = original.state.epsx;
|
---|
| 52 | this.state.f = original.state.f;
|
---|
| 53 | this.state.fbase = original.state.fbase;
|
---|
| 54 | this.state.fm1 = original.state.fm1;
|
---|
| 55 | this.state.fm2 = original.state.fm2;
|
---|
| 56 | this.state.fold = original.state.fold;
|
---|
| 57 | this.state.fp1 = original.state.fp1;
|
---|
| 58 | this.state.fp2 = original.state.fp1;
|
---|
| 59 | this.state.g = CopyArray(original.state.g);
|
---|
| 60 | this.state.gammak = original.state.gammak;
|
---|
| 61 | this.state.k = original.state.k;
|
---|
| 62 |
|
---|
| 63 | this.state.lstate.brackt = original.state.lstate.brackt;
|
---|
| 64 | this.state.lstate.dg = original.state.lstate.dg;
|
---|
| 65 | this.state.lstate.dginit = original.state.lstate.dginit;
|
---|
| 66 | this.state.lstate.dgm = original.state.lstate.dgm;
|
---|
| 67 | this.state.lstate.dgtest = original.state.lstate.dgtest;
|
---|
| 68 | this.state.lstate.dgx = original.state.lstate.dgx;
|
---|
| 69 | this.state.lstate.dgxm = original.state.lstate.dgxm;
|
---|
| 70 | this.state.lstate.dgy = original.state.lstate.dgy;
|
---|
| 71 | this.state.lstate.dgym = original.state.lstate.dgym;
|
---|
| 72 | this.state.lstate.finit = original.state.lstate.finit;
|
---|
| 73 | this.state.lstate.fm = original.state.lstate.fm;
|
---|
| 74 | this.state.lstate.ftest1 = original.state.lstate.ftest1;
|
---|
| 75 | this.state.lstate.fx = original.state.lstate.fx;
|
---|
| 76 | this.state.lstate.fxm = original.state.lstate.fxm;
|
---|
| 77 | this.state.lstate.fy = original.state.lstate.fy;
|
---|
| 78 | this.state.lstate.fym = original.state.lstate.fym;
|
---|
| 79 | this.state.lstate.infoc = original.state.lstate.infoc;
|
---|
| 80 | this.state.lstate.stage1 = original.state.lstate.stage1;
|
---|
| 81 | this.state.lstate.stmax = original.state.lstate.stmax;
|
---|
| 82 | this.state.lstate.stmin = original.state.lstate.stmin;
|
---|
| 83 | this.state.lstate.stx = original.state.lstate.stx;
|
---|
| 84 | this.state.lstate.sty = original.state.lstate.sty;
|
---|
| 85 | this.state.lstate.width = original.state.lstate.width;
|
---|
| 86 | this.state.lstate.width1 = original.state.lstate.width1;
|
---|
| 87 | this.state.lstate.xtrapf = original.state.lstate.xtrapf;
|
---|
| 88 |
|
---|
| 89 | this.state.m = original.state.m;
|
---|
| 90 | this.state.maxits = original.state.maxits;
|
---|
| 91 | this.state.mcstage = original.state.mcstage;
|
---|
| 92 | this.state.n = original.state.n;
|
---|
| 93 | this.state.needf = original.state.needf;
|
---|
| 94 | this.state.needfg = original.state.needfg;
|
---|
| 95 | this.state.nfev = original.state.nfev;
|
---|
| 96 | this.state.p = original.state.p;
|
---|
| 97 | this.state.prectype = original.state.prectype;
|
---|
| 98 | this.state.q = original.state.q;
|
---|
| 99 | this.state.repiterationscount = original.state.repiterationscount;
|
---|
| 100 | this.state.repnfev = original.state.repnfev;
|
---|
| 101 | this.state.repterminationtype = original.state.repterminationtype;
|
---|
| 102 | this.state.rho = CopyArray(original.state.rho);
|
---|
| 103 | this.state.rstate.ba = CopyArray(original.state.rstate.ba);
|
---|
| 104 | this.state.rstate.ca = CopyArray(original.state.rstate.ca);
|
---|
| 105 | this.state.rstate.ia = CopyArray(original.state.rstate.ia);
|
---|
| 106 | this.state.rstate.ra = CopyArray(original.state.rstate.ra);
|
---|
| 107 | this.state.rstate.stage = original.state.rstate.stage;
|
---|
| 108 |
|
---|
| 109 | this.state.s = CopyArray(original.state.s);
|
---|
| 110 | this.state.sk = CopyArray(original.state.sk);
|
---|
| 111 | this.state.stp = original.state.stp;
|
---|
| 112 | this.state.stpmax = original.state.stpmax;
|
---|
| 113 | this.state.theta = CopyArray(original.state.theta);
|
---|
| 114 | this.state.trimthreshold = original.state.trimthreshold;
|
---|
| 115 | this.state.work = CopyArray(original.state.work);
|
---|
| 116 | this.state.x = CopyArray(original.state.x);
|
---|
| 117 | this.state.xrep = original.state.xrep;
|
---|
| 118 | this.state.xupdated = original.state.xupdated;
|
---|
| 119 | this.state.yk = CopyArray(original.state.yk);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[8396] | 122 | public LbfgsState(alglib.minlbfgs.minlbfgsstate state)
|
---|
[8371] | 123 | : base() {
|
---|
| 124 | this.state = state;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[8396] | 128 | return new LbfgsState(this, cloner);
|
---|
[8371] | 129 | }
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | private T[] CopyArray<T>(T[] a) {
|
---|
| 133 | var c = new T[a.Length];
|
---|
| 134 | Array.Copy(a, c, c.Length);
|
---|
| 135 | return c;
|
---|
| 136 | }
|
---|
| 137 | private T[,] CopyArray<T>(T[,] a) {
|
---|
| 138 | var c = new T[a.GetLength(0), a.GetLength(1)];
|
---|
| 139 | Array.Copy(a, c, c.Length);
|
---|
| 140 | return c;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | #region persistence
|
---|
| 144 | [Storable]
|
---|
| 145 | private double[] Autobuf { get { return state.autobuf; } set { state.autobuf = value; } }
|
---|
| 146 | [Storable]
|
---|
| 147 | private double[] D { get { return state.d; } set { state.d = value; } }
|
---|
| 148 | [Storable]
|
---|
| 149 | private double[,] Denseh { get { return state.denseh; } set { state.denseh = value; } }
|
---|
| 150 | [Storable]
|
---|
| 151 | private double[] Diagh { get { return state.diagh; } set { state.diagh = value; } }
|
---|
| 152 | [Storable]
|
---|
| 153 | private double Diffstep { get { return state.diffstep; } set { state.diffstep = value; } }
|
---|
| 154 | [Storable]
|
---|
| 155 | private double Epsf { get { return state.epsf; } set { state.epsf = value; } }
|
---|
| 156 | [Storable]
|
---|
| 157 | private double Epsg { get { return state.epsg; } set { state.epsg = value; } }
|
---|
| 158 | [Storable]
|
---|
| 159 | private double Epsx { get { return state.epsx; } set { state.epsx = value; } }
|
---|
| 160 | [Storable]
|
---|
| 161 | private double F { get { return state.f; } set { state.f = value; } }
|
---|
| 162 | [Storable]
|
---|
| 163 | private double Fbase { get { return state.fbase; } set { state.fbase = value; } }
|
---|
| 164 | [Storable]
|
---|
| 165 | private double Fm1 { get { return state.fm1; } set { state.fm1 = value; } }
|
---|
| 166 | [Storable]
|
---|
| 167 | private double Fm2 { get { return state.fm2; } set { state.fm2 = value; } }
|
---|
| 168 | [Storable]
|
---|
| 169 | private double Fold { get { return state.fold; } set { state.fold = value; } }
|
---|
| 170 | [Storable]
|
---|
| 171 | private double Fp1 { get { return state.fp1; } set { state.fp1 = value; } }
|
---|
| 172 | [Storable]
|
---|
| 173 | private double Fp2 { get { return state.fp2; } set { state.fp2 = value; } }
|
---|
| 174 | [Storable]
|
---|
| 175 | private double[] G { get { return state.g; } set { state.g = value; } }
|
---|
| 176 | [Storable]
|
---|
| 177 | private double Gammak { get { return state.gammak; } set { state.gammak = value; } }
|
---|
| 178 | [Storable]
|
---|
| 179 | private int K { get { return state.k; } set { state.k = value; } }
|
---|
| 180 | [Storable]
|
---|
| 181 | private bool LstateBrackt { get { return state.lstate.brackt; } set { state.lstate.brackt = value; } }
|
---|
| 182 | [Storable]
|
---|
| 183 | private double LstateDg { get { return state.lstate.dg; } set { state.lstate.dg = value; } }
|
---|
| 184 | [Storable]
|
---|
| 185 | private double LstateDginit { get { return state.lstate.dginit; } set { state.lstate.dginit = value; } }
|
---|
| 186 | [Storable]
|
---|
| 187 | private double LstateDgm { get { return state.lstate.dgm; } set { state.lstate.dgm = value; } }
|
---|
| 188 | [Storable]
|
---|
| 189 | private double LstateDgtest { get { return state.lstate.dgtest; } set { state.lstate.dgtest = value; } }
|
---|
| 190 | [Storable]
|
---|
| 191 | private double LstateDgx { get { return state.lstate.dgx; } set { state.lstate.dgx = value; } }
|
---|
| 192 | [Storable]
|
---|
| 193 | private double LstateDgxm { get { return state.lstate.dgxm; } set { state.lstate.dgxm = value; } }
|
---|
| 194 | [Storable]
|
---|
| 195 | private double LstateDgy { get { return state.lstate.dgy; } set { state.lstate.dgy = value; } }
|
---|
| 196 | [Storable]
|
---|
| 197 | private double LstateDgym { get { return state.lstate.dgym; } set { state.lstate.dgym = value; } }
|
---|
| 198 | [Storable]
|
---|
| 199 | private double LstateFinit { get { return state.lstate.finit; } set { state.lstate.finit = value; } }
|
---|
| 200 | [Storable]
|
---|
| 201 | private double LstateFm { get { return state.lstate.fm; } set { state.lstate.fm = value; } }
|
---|
| 202 | [Storable]
|
---|
| 203 | private double LstateFtest1 { get { return state.lstate.ftest1; } set { state.lstate.ftest1 = value; } }
|
---|
| 204 | [Storable]
|
---|
| 205 | private double LstateFx { get { return state.lstate.fx; } set { state.lstate.fx = value; } }
|
---|
| 206 | [Storable]
|
---|
| 207 | private double LstateFxm { get { return state.lstate.fxm; } set { state.lstate.fxm = value; } }
|
---|
| 208 | [Storable]
|
---|
| 209 | private double LstateFy { get { return state.lstate.fy; } set { state.lstate.fy = value; } }
|
---|
| 210 | [Storable]
|
---|
| 211 | private double LstateFym { get { return state.lstate.fym; } set { state.lstate.fym = value; } }
|
---|
| 212 | [Storable]
|
---|
| 213 | private int LstateInfoc { get { return state.lstate.infoc; } set { state.lstate.infoc = value; } }
|
---|
| 214 | [Storable]
|
---|
| 215 | private bool LstateStage1 { get { return state.lstate.stage1; } set { state.lstate.stage1 = value; } }
|
---|
| 216 | [Storable]
|
---|
| 217 | private double LstateStmax { get { return state.lstate.stmax; } set { state.lstate.stmax = value; } }
|
---|
| 218 | [Storable]
|
---|
| 219 | private double LstateStmin { get { return state.lstate.stmin; } set { state.lstate.stmin = value; } }
|
---|
| 220 | [Storable]
|
---|
| 221 | private double LstateStx { get { return state.lstate.stx; } set { state.lstate.stx = value; } }
|
---|
| 222 | [Storable]
|
---|
| 223 | private double LstateSty { get { return state.lstate.sty; } set { state.lstate.sty = value; } }
|
---|
| 224 | [Storable]
|
---|
| 225 | private double LstateWidth { get { return state.lstate.width; } set { state.lstate.width = value; } }
|
---|
| 226 | [Storable]
|
---|
| 227 | private double LstateWidth1 { get { return state.lstate.width1; } set { state.lstate.width1 = value; } }
|
---|
| 228 | [Storable]
|
---|
| 229 | private double LstateXtrapf { get { return state.lstate.xtrapf; } set { state.lstate.xtrapf = value; } }
|
---|
| 230 |
|
---|
| 231 | [Storable]
|
---|
| 232 | private int M { get { return state.m; } set { state.m = value; } }
|
---|
| 233 | [Storable]
|
---|
| 234 | private int MaxIts { get { return state.maxits; } set { state.maxits = value; } }
|
---|
| 235 | [Storable]
|
---|
| 236 | private int Mcstage { get { return state.mcstage; } set { state.mcstage = value; } }
|
---|
| 237 | [Storable]
|
---|
| 238 | private int N { get { return state.n; } set { state.n = value; } }
|
---|
| 239 | [Storable]
|
---|
| 240 | private bool Needf { get { return state.needf; } set { state.needf = value; } }
|
---|
| 241 | [Storable]
|
---|
| 242 | private bool Needfg { get { return state.needfg; } set { state.needfg = value; } }
|
---|
| 243 | [Storable]
|
---|
| 244 | private int Nfev { get { return state.nfev; } set { state.nfev = value; } }
|
---|
| 245 | [Storable]
|
---|
| 246 | private int P { get { return state.p; } set { state.p = value; } }
|
---|
| 247 | [Storable]
|
---|
| 248 | private int Prectype { get { return state.prectype; } set { state.prectype = value; } }
|
---|
| 249 | [Storable]
|
---|
| 250 | private int Q { get { return state.q; } set { state.q = value; } }
|
---|
| 251 | [Storable]
|
---|
| 252 | private int Repiterationscount { get { return state.repiterationscount; } set { state.repiterationscount = value; } }
|
---|
| 253 | [Storable]
|
---|
| 254 | private int Repnfev { get { return state.repnfev; } set { state.repnfev = value; } }
|
---|
| 255 | [Storable]
|
---|
| 256 | private int Repterminationtype { get { return state.repterminationtype; } set { state.repterminationtype = value; } }
|
---|
| 257 | [Storable]
|
---|
| 258 | private double[] Rho { get { return state.rho; } set { state.rho = value; } }
|
---|
| 259 | [Storable]
|
---|
| 260 | private bool[] RstateBa { get { return state.rstate.ba; } set { state.rstate.ba = value; } }
|
---|
| 261 | [Storable]
|
---|
| 262 | private IList<Tuple<double, double>> RStateCa {
|
---|
| 263 | get { return state.rstate.ca.Select(c => Tuple.Create(c.x, c.y)).ToList(); }
|
---|
| 264 | set { state.rstate.ca = value.Select(t => new alglib.complex(t.Item1, t.Item2)).ToArray(); }
|
---|
| 265 | }
|
---|
| 266 | [Storable]
|
---|
| 267 | private int[] RstateIa { get { return state.rstate.ia; } set { state.rstate.ia = value; } }
|
---|
| 268 | [Storable]
|
---|
| 269 | private double[] RstateRa { get { return state.rstate.ra; } set { state.rstate.ra = value; } }
|
---|
| 270 | [Storable]
|
---|
[8416] | 271 | private int RstateStage { get { return state.rstate.stage; } set { state.rstate.stage = value; } }
|
---|
[8371] | 272 | [Storable]
|
---|
| 273 | private double[] S { get { return state.s; } set { state.s = value; } }
|
---|
| 274 | [Storable]
|
---|
| 275 | private double[,] Sk { get { return state.sk; } set { state.sk = value; } }
|
---|
| 276 | [Storable]
|
---|
| 277 | private double Stp { get { return state.stp; } set { state.stp = value; } }
|
---|
| 278 | [Storable]
|
---|
| 279 | private double Stpmax { get { return state.stpmax; } set { state.stpmax = value; } }
|
---|
| 280 | [Storable]
|
---|
| 281 | private double[] Theta { get { return state.theta; } set { state.theta = value; } }
|
---|
| 282 | [Storable]
|
---|
| 283 | private double Trimthreshold { get { return state.trimthreshold; } set { state.trimthreshold = value; } }
|
---|
| 284 | [Storable]
|
---|
| 285 | private double[] Work { get { return state.work; } set { state.work = value; } }
|
---|
| 286 | [Storable]
|
---|
| 287 | private double[] X { get { return state.x; } set { state.x = value; } }
|
---|
| 288 | [Storable]
|
---|
| 289 | private bool Xrep { get { return state.xrep; } set { state.xrep = value; } }
|
---|
| 290 | [Storable]
|
---|
| 291 | private bool Xupdated { get { return state.xupdated; } set { state.xupdated = value; } }
|
---|
| 292 | [Storable]
|
---|
| 293 | private double[,] Yk { get { return state.yk; } set { state.yk = value; } }
|
---|
| 294 | #endregion
|
---|
| 295 | }
|
---|
| 296 | }
|
---|