forum.alglib.net http://forum.alglib.net/ |
|
minlmoptimize http://forum.alglib.net/viewtopic.php?f=2&t=453 |
Page 1 of 1 |
Author: | racer23 [ Tue Oct 04, 2011 12:27 pm ] |
Post subject: | minlmoptimize |
I have a question on passing in variables using minlmoptimize. For minlmoptimize the objective function must be of the the form: objective_function(double[] initial_guess, double[] fi, object var) However in place of object var I want to pass in an instance of my custom class (C): Setup C = new Setup(); Thus I would have: objective_function(double[] initial_guess, double[] fi, Setup C) When I try to do this I get an error "cannot convert from method group to alglib.ndimensional_fvec" I want to pass the instance of my class so the objective function can modify certain values in that class. Thanks, Scott |
Author: | Sergey.Bochkanov [ Tue Oct 04, 2011 1:45 pm ] |
Post subject: | Re: minlmoptimize |
You have to convert object to your class Setup in the objective_function, without changing its signature. You can pass your own class, but as object only - however, you can easily convert it back to its original representation. |
Author: | racer23 [ Tue Oct 04, 2011 2:24 pm ] |
Post subject: | Re: minlmoptimize |
Sergey, thanks for the reply. How would I convert the object C back to class Setup? |
Author: | Sergey.Bochkanov [ Tue Oct 04, 2011 3:02 pm ] |
Post subject: | Re: minlmoptimize |
http://msdn.microsoft.com/en-us/library/ms173105.aspx this article covers typecasting in C# |
Author: | racer23 [ Tue Oct 04, 2011 5:01 pm ] |
Post subject: | Re: minlmoptimize |
Ok, lets look at an example: namespace ConsoleApplication1 { class Program { static void Main(string[] args) { object O = new object(); Setup C = new Setup(); C.A = 2; C.B = 3; testfnc(C); } private static void testfnc(object C) { ((ConsoleApplication1.Setup)(C)).A = 4; } } public class Setup { public double A = 0; public double B = 0; } } How can I get my testfnc to use just C.A instead of ((ConsoleApplication1.Setup)(C)).A? Thats what I am confused on right now. Scott |
Author: | Sergey.Bochkanov [ Tue Oct 04, 2011 6:12 pm ] |
Post subject: | Re: minlmoptimize |
I think that you can write Setup C2 = (ConsoleApplication1.Setup)C and work with C2. |
Author: | racer23 [ Tue Oct 04, 2011 6:36 pm ] |
Post subject: | Re: minlmoptimize |
But then C2 is a different instance of the same object. Therefore once I exit the objective function any variables that were changed to C2 I will not see. |
Author: | Sergey.Bochkanov [ Tue Oct 04, 2011 6:54 pm ] |
Post subject: | Re: minlmoptimize |
I think that you should review basics of C# object model. Speaking short, C and C2 are references to the same underlying object. Hence, changes made to C2 will be instantly visible when you look at C. |
Page 1 of 1 | All times are UTC |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |