cureos / csnumerics Goto Github PK
View Code? Open in Web Editor NEWPortable numerical algorithms in C#
License: GNU Lesser General Public License v3.0
Portable numerical algorithms in C#
License: GNU Lesser General Public License v3.0
I'm new to this library, let me know if this isn't the right place to ask this question.
I have called Minimize with no constraints (ie matrix A and vector B are both of size zero), and I get the output shown below.
It seems to say the least value of F is zero, despite finding some negative F values previously, and then terminates.
What could I be doing wrong?
thanks
Function number 1 F = NaN The corresponding X is:
0.000000E+000 0.000000E+000 0.000000E+000
Function number 2 F = -1.1845510303E+001 The corresponding X is:
1.000000E+000 0.000000E+000 0.000000E+000
Function number 3 F = -4.7807335395E+000 The corresponding X is:
0.000000E+000 1.000000E+000 0.000000E+000
Function number 4 F = -8.1966579100E+000 The corresponding X is:
0.000000E+000 0.000000E+000 1.000000E+000
Function number 5 F = 1.1845510303E+001 The corresponding X is:
-1.000000E+000 0.000000E+000 0.000000E+000
Function number 6 F = 4.7807335395E+000 The corresponding X is:
0.000000E+000 -1.000000E+000 0.000000E+000
Function number 7 F = 8.1966579100E+000 The corresponding X is:
0.000000E+000 0.000000E+000 -1.000000E+000New RHO = 1.0000E-001 Number of function values = 7
Least value of F = NaN The corresponding X is:
0.000000E+000 0.000000E+000 0.000000E+000
Function number 8 F = -1.1845510303E+001 The corresponding X is:
1.000000E-001 0.000000E+000 0.000000E+000
Function number 9 F = -4.7807335395E+000 The corresponding X is:
0.000000E+000 1.000000E-001 0.000000E+000
Function number 10 F = -8.1966579100E+000 The corresponding X is:
0.000000E+000 0.000000E+000 1.000000E-001
Function number 11 F = 1.1845510303E+001 The corresponding X is:
-1.000000E-001 0.000000E+000 0.000000E+000New RHO = 1.0000E-002 Number of function values = 11
Least value of F = NaN The corresponding X is:
0.000000E+000 0.000000E+000 0.000000E+000
Function number 12 F = 4.7807335395E+000 The corresponding X is:
0.000000E+000 -1.000000E-002 0.000000E+000
Function number 13 F = 8.1966579100E+000 The corresponding X is:
0.000000E+000 0.000000E+000 -1.000000E-002
Function number 14 F = -1.1845510303E+001 The corresponding X is:
1.000000E-002 0.000000E+000 0.000000E+000
Function number 15 F = -4.7807335395E+000 The corresponding X is:
0.000000E+000 1.000000E-002 0.000000E+000
At the return from LINCOA Number of function values = 15
Least value of F = 0.000000000000000E+000 The corresponding X is:
0.000000E+000 1.000000E-002 0.000000E+000``
Reported by Ulrich Römer, Karlsruhe Institute of Technology:
I believe there is a bug in line 1308 (in subroutine ALTMOV).
It states
for (var i = 1; i >= n; ++i)
whereas I believe it should be
for (var i = 1; i <= n; ++i)
for the loop to run more than one time.
Please check this line (and please let me know).
When tot
at Cobyla.cs:962
is very small, this line evaluates to temp = 0.0
. This causes line 963 to divide by zero, evaluating to alpha = NaN
. This eventually causes both step and stepful to both be NaN
, which causes Cobyla.cs:1292
to always evaluate to false (because (NaN == NaN) == false
), looping forever.
This is annoying since it makes the algorithm unusable for production purposes.
Hi,
I am looking for C# library to replace 'fmincon' Matlab function in my application.
It seems that COBYLA is suitable for me.
However, in my 'fmicon' solution I have got equality constraints as set functions like this:
function [c, ceq] = confun(x)
global at aw;
% Nonlinear inequality constraints
c = [];
% Nonlinear equality constraints
ceq = [x(1)-(at(3)+x(4)*(1-x(5)/x(6))+x(7)*x(8)/x(6));
x(7)-(at(5)+x(9)*(1-x(10)/x(11)));
aw(1)+aw(2)+aw(3)+aw(4)-at(1)-at(2)-at(3)-at(4)-at(5)-at(6)-x(4)-x(9)];
Can I use COBYLA in that case?
If so, is there any example how can I code it?
Best Regards,
Alex
Provide the NEWUOA algorithm for unconstrained problems.
Dear csnumrics maintainers,
This is Dr. Zaikun Zhang from the Hong Kong Polytechnic University. Together with Professor N.I.M. Gould, I am responsible for maintaining the derivative-free optimization solvers of late Professor M.J.D. Powell.
Thank you for making COBYLA and BOBYQA available in csnumrics. I note that the current version is based on the original Fortran 77 implementation, the latter being not maintained anymore.
Although Powell's Fortran 77 implementation is truly a masterpiece, it contains many bugs, most of which are due to the language itself. For example, see Section 4.4 of our recent paper and the GitHub issues / requests listed at the end of this message (not all of them concern csnumrics).
To avoid the problems originating from the Fortran 77 code, I suggest you use the PRIMA implementation of Powell's solvers. PRIMA provides the reference implementation for Powell's renowned derivative-free optimization methods, namely COBYLA, UOBYQA, NEWUOA, BOBYQA, and LINCOA. The "P" in the name stands for Powell, and "RIMA" is an acronym for "Reference Implementation with Modernization and Amelioration".
PRIMA provides the modern implementation of the solvers in Fortran 2008. It fixes bugs in the original Fortran 77 code. In addition, it introduces improvements that boost the performance in terms of the number of function evaluations, which is the standard measure of computational costs in derivative-free optimization.
See the GitHub repo of PRIMA for more information. I will be glad to provide assistance if help is needed.
Thanks.
The Fortran 77 solvers may get stuck in infinite loops.
The Fortran 77 solvers may crash with segmentation faults
due to uninitialized variables that are used as indices.
Fortran 77 COBYLA may not return the best point that is evaluated; sometimes, the returned point can have a
large constraint violation even though the starting point is feasible.
Hi,
I've been using this library for a couple of weeks now, thanks very much.
On occasion, FindMinimum never returns a value. Presumably it is stuck in a loop (one core is being using from looking at Task Manager), however the objective function is not being called.
Is that something you've experienced?
Is there a fix, or way of setting a timeout?
thanks
Ben
Dear csnumerics team,
I'm trying to solve an optimization problem where the objective function is calculated using precalculated variables, therefore these variables need to be passed as additional parameters to the "calcfun", however, Bobyqa class only permits two parameters in the objective function: n & x vector. How can I pass extra parameters to the objective function?
Best Regards.
Hi, I'm trying to solve trilateration problem assuming ranges are erroneous.
I know 3 position with 3 ranges and i want to compute the best position of the 4th sensor.
My code is as follow :
(Error function)
public double BobyqaTestCalfun(int n, double[] x)
{
var f = 0.0;
//f = (Math.Abs(Math.Sqrt((x[0] - pos[0][0]) * (x[0] - pos[0][0]) + (x[1] - pos[0][1]) * (x[1] - pos[0][1]) + (x[2] - pos[0][2]) * (x[2] - pos[0][2])) - range[0]) + Math.Abs(Math.Sqrt((x[0] - pos[1][0]) * (x[0] - pos[1][0]) + (x[1] - pos[1][1]) * (x[1] - pos[1][1]) + (x[2] - pos[1][2]) * (x[2] - pos[1][2])) - range[1]) + Math.Abs(Math.Sqrt((x[0] - pos[2][0]) * (x[0] - pos[2][0]) + (x[1] - pos[2][1]) * (x[1] - pos[2][1]) + (x[2] - pos[2][2]) * (x[2] - pos[2][2])) - range[2])) / 3;
f = (Math.Pow(Math.Sqrt((x[0] - pos[0][0]) * (x[0] - pos[0][0]) + (x[1] - pos[0][1]) * (x[1] - pos[0][1]) + (x[2] - pos[0][2]) * (x[2] - pos[0][2])) - range[0],2.0) + Math.Pow(Math.Sqrt((x[0] - pos[1][0]) * (x[0] - pos[1][0]) + (x[1] - pos[1][1]) * (x[1] - pos[1][1]) + (x[2] - pos[1][2]) * (x[2] - pos[1][2])) - range[1], 2.0) + Math.Pow(Math.Sqrt((x[0] - pos[2][0]) * (x[0] - pos[2][0]) + (x[1] - pos[2][1]) * (x[1] - pos[2][1]) + (x[2] - pos[2][2]) * (x[2] - pos[2][2])) - range[2],2.0)) / 3;
// Debug.WriteLine(f);
return f;
}
(set & Optimization)
public double[] FindMinimum(List<double[]> poss, List<double> ranges)
{
pos = poss;
range = ranges;
var computedPos = new double[3];
//Ref formuule :https://pdfs.semanticscholar.org/62ef/01bf24ce619e5e9bd4ae911115381000679b.pdf
const int iprint = 1;
const int maxfun = 500000;
const double rhobeg = 200f;
const double rhoend = 0.000000000000000000000000000000001f;
var optimizer = new Bobyqa(3, this.BobyqaTestCalfun, new double[] { -10000, -10000, 0 }, new double[] { 10000, 10000, 3000 });
optimizer.TrustRegionRadiusStart = rhobeg;
optimizer.InterpolationConditions = 8; //2*n+1
optimizer.TrustRegionRadiusEnd = rhoend;
optimizer.PrintLevel = iprint;
optimizer.MaximumFunctionCalls = maxfun;
optimizer.Logger = Console.Out;
double[] midPts = new double[] { (pos[0][0] + pos[1][0] + pos[2][0]) / 3, (pos[0][1] + pos[1][1] + pos[2][1]) / 3, (pos[0][2] + pos[1][2] + pos[2][2])/3 };
/*
if (range[0] < range[1] && range[0] < range[2])
{
midPts = pos[0];
}
else if(range[1] < range[0] && range[1] < range[2])
{
midPts = pos[1];
}
else
{
midPts = pos[2];
}
*/
var summary = optimizer.FindMinimum(midPts);
computedPos = summary.X;
// var ftest = Math.Abs(Math.Sqrt((summary.X[0] - pos[0][0]) * (summary.X[0] - pos[0][0]) + (summary.X[1] - pos[0][1]) * (summary.X[1] - pos[0][1]) + (summary.X[2] - pos[0][2]) * (summary.X[2] - pos[0][2])) - range[0]) + Math.Abs(Math.Sqrt((summary.X[0] - pos[1][0]) * (summary.X[0] - pos[1][0]) + (summary.X[1] - pos[1][1]) * (summary.X[1] - pos[1][1]) + (summary.X[2] - pos[1][2]) * (summary.X[2] - pos[1][2])) - range[1]) + Math.Abs(Math.Sqrt((summary.X[0] - pos[2][0]) * (summary.X[0] - pos[2][0]) + (summary.X[1] - pos[2][1]) * (summary.X[1] - pos[2][1]) + (summary.X[2] - pos[2][2]) * (summary.X[2] - pos[2][2])) - range[2]);
// Debug.Write(" ftest " + ftest + " ");
return computedPos;
}
This code is my best set of parameter so far, i'm wondering if i'm setting "rhobeg" and "rhoend" well... The optimization sometimes works but most of the time it fails while being far from asked accuracy.
Any clue on why optimization is innacurate most of the time ?
After calling BOBYQA successfully several thousand times in a row, it seems to be getting stuck on specific data sets. It gets into an infinite loop internally which stops calling the penalty delegate, and just stays in that loop forever. It keeps bouncing between:
if (resid <= ZERO) goto L_90;
and
if (crvmin != ZERO) goto L_50;
Upon debugging inspection, I see the s[], hq[] and other arrays have huge values in them like 2.3E141, ds is -4.9272591443386037E+132 and shs is Infinity. This leads me to believe something is wrong.
I am trying to solve for a few variables in reasonable ranges like -1000 to 1000 and am not doing anything out of the ordinary.
Do you have any idea what might lead to this and how I can overcome it?
I also am unsure what to do with the TrustRegion properties. I leave them as their default of -1.0. Would setting them to something different help out here?
Thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.