forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 2:20 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: minlmoptimize
PostPosted: Tue Oct 04, 2011 12:27 pm 
Offline

Joined: Tue Oct 04, 2011 12:22 pm
Posts: 4
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


Top
 Profile  
 
 Post subject: Re: minlmoptimize
PostPosted: Tue Oct 04, 2011 1:45 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
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.


Top
 Profile  
 
 Post subject: Re: minlmoptimize
PostPosted: Tue Oct 04, 2011 2:24 pm 
Offline

Joined: Tue Oct 04, 2011 12:22 pm
Posts: 4
Sergey, thanks for the reply. How would I convert the object C back to class Setup?


Top
 Profile  
 
 Post subject: Re: minlmoptimize
PostPosted: Tue Oct 04, 2011 3:02 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
http://msdn.microsoft.com/en-us/library/ms173105.aspx this article covers typecasting in C#


Top
 Profile  
 
 Post subject: Re: minlmoptimize
PostPosted: Tue Oct 04, 2011 5:01 pm 
Offline

Joined: Tue Oct 04, 2011 12:22 pm
Posts: 4
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


Top
 Profile  
 
 Post subject: Re: minlmoptimize
PostPosted: Tue Oct 04, 2011 6:12 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
I think that you can write Setup C2 = (ConsoleApplication1.Setup)C and work with C2.


Top
 Profile  
 
 Post subject: Re: minlmoptimize
PostPosted: Tue Oct 04, 2011 6:36 pm 
Offline

Joined: Tue Oct 04, 2011 12:22 pm
Posts: 4
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.


Top
 Profile  
 
 Post subject: Re: minlmoptimize
PostPosted: Tue Oct 04, 2011 6:54 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 39 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group