I generally code in MATLAB, but for some reasons I decided to switch to a JAVA approach.
The question is quite easy: I'd like understanding how to translate the following MATLAB code into a working JAVA's one.
Within MATLAB I have a target function called findZ0:
function F = findZ0(V, Z, Latitude, TI, x)
%%% Inputs
% V = Average Wind Speed at Hub Height
% Z = Hub Height;
% Latitude = Specific Site Latitude (default value equal to 50 deg);
% x = Tryout Roughness length;
% TI = Target Turbulent Intensity;
%%% Outputs
% F = Roughness Length tuned to match Target Turbulent Intensity
Latitude = deg2rad(Latitude);
omega = 72.9E-06;
f = 2*omega*sin(Latitude);
ustar = ( 0.4*V - 34.5*f*Z)/log(Z/x);
mu = 1 - ((6*f*Z)/(ustar));
p = mu^(16);
sigmaTarget = (V*TI)/100;
F = sigmaTarget - (( 7.5*mu*ustar*((0.538 + .09*log(Z/x))^p) )/(1 + .156*log(ustar/(f*x))));
end
I then called this lines:
Uhub = 8;
HubHt = 90;
Latitude = 50;
x_trial = 0.01;
TI_target = 24;
find_z0 = #(x) findZ0(Uhub,HubHt,Latitude,TI_target, x);
z0 = fsolve(find_z0,x_trial,{'fsolve','Jacobian','on','levenberg-marquardt',.005,'MaxIter',15000,'TolX',1e-07,'TolFun',1E-07,'Display','off'});
I am aware that Fortran packages have been imported in Java, but I don't really have a clue how to achieve my goal by applying the mentioned tools. Hence, I'd welcome any suggestion on how to overcome this problem.
I'd suggest using an existing solution like Apache Commons - it's a robust library containing a lot of tools you may find helpful.
The LM optimization method is implemented by this class - you can either use it directly or just look at it for inspiration.
Related
Im trying to work out how to calculate where a 3D line(Two 3D points) and a Axis meets.
In my case im trying to calculate the intersection of a 3D Point to the Z Axis.
Lets say i give the line the coords of
Point1.X = 0
Point1.Y = 0
Point1.Z = 0
Point2.X = 50
Point2.Y = 50
Point2.Z = 50
And i want to calculate the intersect at Z 15, how would i accomplish this?
I've thought about using interpolation, but it seems really inefficient here, i've looked at another posts and mathematical equations, but i dont really understand them enough to implement them into usable code.
Suppose you have vector A (like your point1) and B, then a straight line L through both points can be defined by
L(k) = A + k(B - A)
such that L(0) = A and L(1) = B. Suppose you want to find where
L(k).z = z_0
then you need to solve
A.z + k(B.z - A.z) = z_0
so the line L intersects the plane z = z_0 at
k = (z_0 - A.z) / (B.z - A.z)
If (B.z - A.z) is zero, either it does not intersect the plane z = z_0 anywhere, or it is in the plane everywhere.
lambda = ( Point1.Z - Z ) / ( Point1.Z - Point2.Z )
Point3.X = Point1.X + lambda * (Point2.X - Point1.X)
Point3.Y = Point1.Y + lambda * (Point2.Y - Point1.Y)
Point3.Z = Z
I'm implementing the calculations of the book Practical Astronomy with your Calculator or Spreadsheet. Until now my calculations yield exactly the same result as the example calculations in the book.
However, arriving at §39 "Calculating corrections for parallax" I encounter a difference that I fail to understand.
The task at hand is described as follows:
As an example, let us calculate the apparent right ascension and declination of the Moon on 26
February 1979 at 16h 45m UT when observed from a location 60 metres above sea-level on longitude 100° W and latitude 50° N. The geocentric coordinates were α = 22h 35m 19s and δ = −7° 41′ 13′′, and the Moon’s equatorial horizontal parallax was 1° 01′ 09′′.
The book describes the sequence of calculation as follows:
Yet my outcome of step 7 is −31.993415, but book says −31,994415. If I do the mathematics of step 7 with the values of the book on a calculator the result is −31.993415 too, so my outcome seems to be right and the book's wrong....
I could live with that but there is a difference in step 10 too. My result is -8,570634, the books result is -8.538165, a rather large difference. I've read over step 10 time and time again to see if there is an error in my code, but I don't see it.
As until now my calculations and the books calculations are exactly the same I'm stuck. Am I doing something wrong (preferred), or did the book make an error (let's hope there aren't any more...)
My Java code for this function is as follows:
static EquatorialCoordinate parallax(EquatorialCoordinate body, ObserverLocation observer, ZonedDateTime zdt, double P) {
double Hd = 15d * raha(body.α, zdt, observer.λ);
step("α", body.α);
step("δ", body.δ);
step("φ", observer.φ);
step("λ", observer.λ);
step("h", observer.h);
step("H", Hd);
double H = toRadians(Hd);
Parallax ρ = parallax(observer.φ, observer.h);
step("P", P);
P = toRadians(P);
double δ = toRadians(body.δ);
double r = 1d / sin(P);
step("r", r);
double ρsinφ = ρ.sin;
double ρcosφ = ρ.cos;
step("ρcosφ'",ρcosφ);
step("ρsinφ'",ρsinφ);
double Δ = atan((ρcosφ * sin(H)) / ((r * cos(δ)) - (ρcosφ * cos(H))));
step("Δ", toDegrees(Δ));
H += Δ;
step("H'", toDegrees(H));
Δ = toDegrees(Δ);
double α$ = body.α - (Δ / 15d);
step("α'", α$);
double divident = (r * sin(δ)) - ρsinφ;
double divisor = ( r * cos(δ) * cos(H) ) - ρcosφ;
double δ$ = atan(cos(H) * (divident / divisor));
δ$ = toDegrees(δ$);
step("δ'", δ$);
return new EquatorialCoordinate(α$, δ$);
}
The "step" function does a simple formatted printf. The output of this program is:
α 22.588611
δ -7.686944
φ 50.000000
λ -100.000000
h 60.000000
H -31.642500
P 1.019167
r 56.221228
ρcosφ' 0.644060
ρsinφ' 0.762422
Δ -0.350915
H' -31.993414
α' 22.612005
δ' -8.570634
The resulting δ' is -8° 34' 14.28" instead of -8° 32' 17"
I've replaced my calculated value of H' with the books value to see if the book contains a carried error, but even if I do so the value is wrong.
Thus... my big question is, is my implementation wrong (and where, I can't see it), or were the books calculations wrong.
(Edit:) Class is annotated with strictfp, using java.util.StrictMath.
You write
H += Δ;
Which changes the value of H.
Then you write
double δ$ = atan(cos(H) * (divident / divisor));
Which uses the new version of H when it should use the old value.
Thanks to #svasa, I found that the divisor of step 10 should contain H, not H'.
The correct code is:
static EquatorialCoordinate parallax(EquatorialCoordinate body, ObserverLocation observer, ZonedDateTime zdt, double P) {
double H = toRadians(15d * raha(body.α, zdt, observer.λ));
P = toRadians(P);
Parallax ρ = parallax(observer.φ, observer.h);
double δ = toRadians(body.δ);
double r = 1d / sin(P);
double Δ = atan((ρ.cosφ * sin(H)) / ((r * cos(δ)) - (ρ.cosφ * cos(H))));
double H$ = H + Δ;
double α$ = body.α - (toDegrees(Δ) / 15d);
double δ$ = toDegrees(atan(cos(H$) * ((r * sin(δ) - ρ.sinφ) / (r * cos(δ) * cos(H) - ρ.cosφ))));
return new EquatorialCoordinate(α$, δ$);
}
I am implementing a Weighted Moving Average algorithm with the help of convolution.
It is quite easy in Python using the convolution function provided by numpy.
The codes are as follows:
# Method 2 WMA
WINDOW_SIZE = 70
DATA_SET_NUMBER = 7090
smoothedAcc = ndarray(DATA_SET_NUMBER)
weights = ndarray(WINDOW_SIZE)
accWindow = ndarray(WINDOW_SIZE)
for i, v in enumerate(weights):
weights[i] = (WINDOW_SIZE - i) / (WINDOW_SIZE * (WINDOW_SIZE + 1) / 2)
for x in xrange(0, (DATA_SET_NUMBER - WINDOW_SIZE)):
for y in xrange(0, WINDOW_SIZE):
accWindow[y] = acc[x + y]
smoothedAcc[x] = np.convolve(weights, accWindow, 'valid')
However, in the end I have to implement this in Java. I tried to find the readily-built convolution API in Java, but failed.
Anyone can help me get the equivalent Java codes of the above-mentioned Python snippet?
Thanks in advance!
I translated this spline interpolation algorithm from apache.commons.math from Java into Scala in the most straightforward way I could think of (see below). The function I ended up with runs 2 to 3 times slower than the original Java code. My guess is that the problem stems from the extra loops coming from the calls to Array.fill, but I can't think of a straightforward way to get rid of them. Any suggestions on how to make this code perform better? (It would also be nice to write it in a more concise and/or functional way -- suggestions on that front would be appreciated as well.)
type Real = Double
def mySplineInterpolate(x: Array[Real], y: Array[Real]) = {
if (x.length != y.length)
throw new DimensionMismatchException(x.length, y.length)
if (x.length < 3)
throw new NumberIsTooSmallException(x.length, 3, true)
// Number of intervals. The number of data points is n + 1.
val n = x.length - 1
// Differences between knot points
val h = Array.tabulate(n)(i => x(i+1) - x(i))
var mu: Array[Real] = Array.fill(n)(0)
var z: Array[Real] = Array.fill(n+1)(0)
var i = 1
while (i < n) {
val g = 2.0 * (x(i+1) - x(i-1)) - h(i-1) * mu(i-1)
mu(i) = h(i) / g
z(i) = (3.0 * (y(i+1) * h(i-1) - y(i) * (x(i+1) - x(i-1))+ y(i-1) * h(i)) /
(h(i-1) * h(i)) - h(i-1) * z(i-1)) / g
i += 1
}
// cubic spline coefficients -- b is linear, c quadratic, d is cubic (original y's are constants)
var b: Array[Real] = Array.fill(n)(0)
var c: Array[Real] = Array.fill(n+1)(0)
var d: Array[Real] = Array.fill(n)(0)
var j = n-1
while (j >= 0) {
c(j) = z(j) - mu(j) * c(j + 1)
b(j) = (y(j+1) - y(j)) / h(j) - h(j) * (c(j+1) + 2.0 * c(j)) / 3.0
d(j) = (c(j+1) - c(j)) / (3.0 * h(j))
j -= 1
}
Array.tabulate(n)(i => Polynomial(Array(y(i), b(i), c(i), d(i))))
}
You can get rid of all the Array.fill since a new array is always initialized with 0 or null, depending on whether it is a value or a reference (booleans are initialized with false, and characters with \0).
You might be able to simplify the loops by zipping arrays, but you'll only make it slower. The only way functional programming (on the JVM anyway) is going to help you make this faster is if you make it non-strict, such as with a Stream or a view, and then you go ahead and not use all of it.
I work on a very little research team to create/adapt a Genetic Algorithm library in Scala for distributed computation with Scientific Worklow System, in our case we use the open source OpenMole software (http://www.openmole.org/).
Recently, i try to understand and re-implement the SBX crossover operator written in JMetal Metaheuristics library (http://jmetal.sourceforge.net/) to adapt it in functionnal version in our Scala library.
I write some code, but i need our advice or your validation about the SBX defined into java library, because the source code (src in svn) doesn't appear equal to the original equation written here : http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.33.7291&rep=rep1&type=pdf at page 30, in annexe A
First question, i don't understand the java version of JMetal, why they use two different beta values ?!
beta1 which use in the equation the first arg of min[(y1 - yL), ...] and
beta2 which use the second arg of min [... , (yu - y2)])
Beta 1 and 2 are used for computation of alpha value and two (so here and in jmetal we have also two alpha different value alpha1 and 2) ...
Same problem/question, we have in jmetal two computation for betaq (java code) or in Deb equation, result of :
Second question, what is the meaning of the symbol used in (2) and (3) procedure in pseudo-algorithm of SBX, and the difference with simple beta ? Especially when we want to compute children/offsprings of crossover parents, like here :
Edit
Correct a no-op if/else block
Author of code in jmetal give me the link of original source code of Nsga-II algorithm, and he explain me that description of SBX by Deb differs from his implementation :/
http://www.iitk.ac.in/kangal/codes.shtml
I don't understand the difference between the description and the implementation in jmetal and original source code, do you have an explanation ?
Correct if/else return for map
Start of translation into scala
class SBXBoundedCrossover[G <: GAGenome, F <: GAGenomeFactory[G]](rate: Random => Double = _.nextDouble) extends CrossOver [G, F] {
def this(rate: Double) = this( _ => rate)
def crossOver (genomes : IndexedSeq [G], factory: F) (implicit aprng : Random) = {
val g1 = genomes.random
val g2 = genomes.random
val crossoverRate = rate(aprng)
val EPS = 1.0e-14
val numberOfVariables = g1.wrappedValues.size
val distributionIndex = 2
val variableToMutate = (0 until g1.wrappedValues.size).map{x => !(aprng.nextDouble < 0.5)}
//crossover probability
val offspring = {
if (aprng.nextDouble < crossoverRate) {
(variableToMutate zip (g1.wrappedValues zip g2.wrappedValues)) map {
case (b, (g1e, g2e)) =>
if(b) {
if (abs(g1e - g2e) > EPS){
val y1 = min(g1e, g2e)
val y2 = max(g2e, g1e)
var yL = 0.0 //g1e.getLowerBound
var yu = 1.0 //g1e.getUpperBound
var rand = aprng.nextDouble // ui
var beta1 = 1.0 + (2.0 * (y1 - yL)/(y2 - y1))
var alpha1 = 2.0 - pow(beta1,-(distributionIndex+1.0))
var betaq1 = computebetaQ(alpha1,distributionIndex,rand)
//calcul offspring 1 en utilisant betaq1, correspond au β barre
var c1 = 0.5 * ((y1 + y2) - betaq1 * (y2 - y1))
// -----------------------------------------------
var beta2 = 1.0 + (2.0 * (yu - y2) / (y2 - y1))
var alpha2 = 2.0 - pow(beta2, -(distributionIndex + 1.0))
var betaq2 = computebetaQ(alpha2,distributionIndex,rand)
//calcul offspring2 en utilisant betaq2
var c2 = 0.5 * ((y1 + y2) + betaq2 * (y2 - y1))
if (c1 < yL) c1 = yL
if (c1 > yu) c1 = yu
if (c2 < yL) c2 = yL
if (c2 > yu) c2 = yu
if (aprng.nextDouble <= 0.5) {
(c2,c1)
} else {
(c1, c2)
}
}else{
(g1e, g2e)
}
}else{
(g2e, g1e)
}
}
}else{
// not so good here ...
(g1.wrappedValues zip g2.wrappedValues)
}
}
(factory.buildGenome(offspring.map{_._1}), factory.buildGenome(offspring.map{_._2}))
}
def computebetaQ(alpha:Double, distributionIndex:Double, rand:Double):Double = {
if (rand <= (1.0/alpha)){
pow ((rand * alpha),(1.0 / (distributionIndex + 1.0)))
} else {
pow ((1.0 / (2.0 - rand * alpha)),(1.0 / (distributionIndex + 1.0)))
}
}
Thanks a lot for your advice, or help in this problem.
SR
Reyman64, your question is the answer I was looking for. Thank you.
I took the paper you linked and the code of Deb's implementation and tried to understand both. For that, I commented almost every line of the code. They differ only in the calculation of beta.
Since Deb used this code in his implementation of the NSGA-II, I'll stick to this version of the algorithm.
If anyone is in the same situation I was (not understanding how to implement SBX), I left my commentaries in the following gist, take a look.
https://gist.github.com/Tiagoperes/1779d5f1c89bae0cfdb87b1960bba36d
I did an implementation of the SBX (it is called Simulated Binary Crossover btw) for HeuristicLab (C#). You can take a look at the implementation of our SimulatedBinaryCrossover. I took the description from a different reference however (paper title: "Simulated binary crossover for continuous search space" from 1995). The full citation is given in the source code.