Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

C# dtrnlspbc_init

Gianluca_G_1
Beginner
569 Views

I'm trying to understand how to declare and use in C#: RCI TR Routines for Problem with Bound Constraints.

I have created a simple test console program, and declared just one function (dtrnlspbc_init) but  I receive that "there was an error in the input parameters".

It is a Marshalling problem, I suppose, but I don't understand where there is the error.

Is there someone that can help me?

Thankyou 

Gianluca

0 Kudos
10 Replies
mecej4
Honored Contributor III
569 Views

Check the second and third arguments to dtrnlspbc_init(). I believe that you have them interchanged.

0 Kudos
Gianluca_G_1
Beginner
569 Views

Now it works! thank you 

by the way the documentations it is not so clear.

Best Regards

Gianluca

 

0 Kudos
mecej4
Honored Contributor III
569 Views

Which version of the documentation do you mean? The current MKL documentation at https://software.intel.com/en-us/node/471100#867C84A5-D029-4C48-B63B-B68E04CE043D clearly states that the second and third arguments are the number of variables and number of functions whose sum of squares is to be minimized.

0 Kudos
Ying_H_Intel
Employee
569 Views

Hi Thank you a lot for the reply mecej4.

Hi Gianluca,

You can take the second and third arguments are x length  and F(x) length (or   m - dimension of function value */.) 

actually,  the solver required m>>n.Where  n is  array X's size;  m is  fvec Array of size m.  So you may be change the m = xxx. when go to solve step based on your fvec.

Best Regards,

Ying


 

0 Kudos
Gianluca_G_1
Beginner
569 Views

If I read this page https://software.intel.com/en-us/node/471100#867C84A5-D029-4C48-B63B-B68E04CE043D,

n is the number of variables and, m is the number of functions. In my case I have n=5 and m=1 but this is not allowed because m >> n.

Maybe the parameter are swapped in the dtrnlspbc_init().

 

Best Regards

Gianluca

 

 

0 Kudos
mecej4
Honored Contributor III
569 Views

In that case, you are attempting to obtain five unknowns from a single scalar observation, i.e., you have a standard bound-constrained minimization problem -- instead of the kind of problem that ?TRNLSBC is designed to solve -- a least-squares minimization of an overdetermined problem. For your class of problems, you should use a solver such as those listed for that class in http://plato.asu.edu/sub/nlores.html and http://plato.asu.edu/sub/nlounres.html .

0 Kudos
Gianluca_G_1
Beginner
569 Views

Thank you for your suggestions.

now it is more clear to me, by the way, I've implemented a test code like the example in the web page (https://software.intel.com/en-us/node/522321).

The objective_function is my_function().

The loop exited correctly but I can't understand where to find the solution!

check row code number 411.

What I miss?

Thank you very much

Gianluca

0 Kudos
mecej4
Honored Contributor III
569 Views

The solution is in xres[], since you used Marshall.Copy to put it there. All that you have to do is to add a formatted output statement such as

     Console.WriteLine(string.Format("{0:0.00} {1:0.00} {2:0.00} {3:0.00} {4:0.00}",
                  xres[0],xres[1],xres[2],xres[3],xres[4]));

0 Kudos
Gianluca_G_1
Beginner
569 Views

I'm sorry, maybe I was not clear ...

Do you really thought that after the Marshal.Copy(), I couldn't get the array values?

The problem is that the result it is not right!

I mean, these are the initial values. The solutions should be differents.

These should be the correct results:

0 = 197
1 = 351
2 = 185
3 = 1,7
4 = 8,8

We are sure of this, because we got it with another algoritms (Amoeba)

Thank you 

Gianluca

 

0 Kudos
Gianluca_G_1
Beginner
569 Views

mecej4 i got the solution!

now it works ....

I forgot to copy the result with Marshal.Copy  in my_function proc.

public static void my_function(ref int m, ref int n, [In] IntPtr xp, [Out] IntPtr fp)
        {
            double[] x = new double;
            Marshal.Copy(xp, x, 0, n);
            double[] f = new double;
            Marshal.Copy(fp, f, 0, m);

            double rho1 = x[0];
            double rho2 = x[1];
            double rho3 = x[2];
            double h1 = x[3];
            double h2 = x[4];

            //double phi = 0;
            //int iN = m; // mlMeasures.Count; //Da inserire

            for (int i = 0; i < m; i++)
            {
                f = Math.Pow((mlMeasures.RhoM - RhoCalc(mlMeasures.A, rho1, rho2, rho3, h1, h2)) / mlMeasures.RhoM, 2);
            }

            Marshal.Copy(f, 0, fp, m);
        }

Thank you very much

Gianluca

 

 

0 Kudos
Reply