View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2024.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.algorithms.optimize.anneal;
39  
40  import org.apache.commons.math3.util.FastMath;
41  
42  /**
43   * Exponential temperature schedule for simulated annealing
44   *
45   * @author Jacob M. Litman
46   * @author Michael J. Schnieders
47   * @since 1.0
48   */
49  public class ExpAnnealSchedule implements AnnealingSchedule {
50  
51    private final int nWindows;
52    private final double tHigh;
53    private final double tLow;
54    private final double gamma;
55    private final String description;
56  
57    /**
58     * Creates an exponential annealing schedule that decays as
59     * tHigh*((tLow/tHigh)^(1/(nWindows-1)))^(n-1). Caution: high values of nWindows could cause
60     * numeric instability.
61     *
62     * @param nWindows Number of windows.
63     * @param tLow Low temperature bound.
64     * @param tHigh High temperature bound.
65     */
66    public ExpAnnealSchedule(int nWindows, double tLow, double tHigh) {
67      assert nWindows > 1;
68      assert tLow < tHigh;
69      assert tLow >= 0;
70      assert Double.isFinite(tHigh);
71  
72      this.nWindows = nWindows;
73      this.tHigh = tHigh;
74      this.tLow = tLow;
75      this.gamma = StrictMath.pow((tLow / tHigh), (1.0 / (nWindows - 1)));
76  
77      description = String.format("Exponential annealing schedule with %d windows, "
78              + "initial temperature %12.7g K, final temperature %12.7g K, gamma %12.7g", nWindows, tHigh,
79          tLow, gamma);
80    }
81  
82    @Override
83    public double getHighTemp() {
84      return tHigh;
85    }
86  
87    @Override
88    public double getLowTemp() {
89      return tLow;
90    }
91  
92    @Override
93    public int getNumWindows() {
94      return nWindows;
95    }
96  
97    @Override
98    public double getTemperature(int i) {
99      assert i >= 0 && i < nWindows;
100     return tHigh * FastMath.pow(gamma, i);
101   }
102 
103   @Override
104   public double[] getTemperatures() {
105     double[] temps = new double[nWindows];
106     for (int i = 0; i < nWindows; i++) {
107       temps[i] = getTemperature(i);
108     }
109     return temps;
110   }
111 
112   @Override
113   public double maxWindowLength() {
114     return 1.0;
115   }
116 
117   @Override
118   public double minWindowLength() {
119     return 1.0;
120   }
121 
122   @Override
123   public String toString() {
124     return description;
125   }
126 
127   @Override
128   public double totalWindowLength() {
129     return nWindows;
130   }
131 
132   @Override
133   public double windowLength(int window) {
134     return 1.0;
135   }
136 }