+ Reply to Thread
Results 1 to 8 of 8

Dynamic code Generation

  1. #1
    Registered User
    Join Date
    07-27-2009
    Location
    L
    MS-Off Ver
    Excel 2003
    Posts
    19

    Dynamic code Generation

    Hi there,

    I need to write some code where I test various functions to see which matches some input data the best. I have 8 functions which can be linearly combined in one of 8^8 combinations.

    You have to use 8 functions each time but the same function can be used 8 times (although this is unlikely to prove interesting).

    So I have to generate a VBA function to test each test function against the data.

    Clearly I can use a combination of for loops and case statements to generate each functional form as follows:

    Please Login or Register  to view this content.
    I then want to paste this function into a VBA function at run time to dynamically each function to be testetd. Is this possible?

    If not is there a different method to generate all these functions without me having to type them all into VBA by have?

    Baz

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Dynamic code Generation

    Maybe you could use something like this as a point of departure:
    Please Login or Register  to view this content.
    To see the results as a UDF, put this in a cell and format to wrap text:

    =FunctionGenerator(INT(RAND()*8^8))
    Last edited by shg; 08-18-2009 at 01:33 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    07-27-2009
    Location
    L
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Dynamic code Generation

    Wow thank you so so much for this!!!!

    I need to use the generated function in a VBA fucntion in the following format:

    Is it possible to pass the generated function in some way?

    I only need to generate the function once per run, but the generated function will be called repeatedly.

    Please Login or Register  to view this content.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Dynamic code Generation

    Sorry, I don't follow that.

  5. #5
    Registered User
    Join Date
    07-19-2009
    Location
    UK
    MS-Off Ver
    Excel 2000
    Posts
    7

    Re: Dynamic code Generation

    What I mean is that each function only needs to be generated once during each run of the program.

    After that though each generated function will be called multiple times during the program until the optimal set of parameters are found.

    the example code below shows how one the function will be called during the optimisation part of the program

    Please Login or Register  to view this content.
    Is there some way to dynamically link the function generated by your code into the function above?

    If not is there another way to achieve the same aim?




    I have now added a copy of the spreadsheet where an example optimisation is being carried out. I need to figure out how to get the functions generated by your code into the optimiser. In the same way that "myfunction" in macro1 is being used. (or alternately any other way that works)
    Attached Files Attached Files
    Last edited by Bazman76; 08-18-2009 at 06:05 PM.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Dynamic code Generation

    You can't bind the variable names in the string to run-time variables.

    I don't see the relationship between your code below and the summation of eight functions in some order. In fact, if all you were doing is summing some combination of 8 functions, order doesn't matter, so that's not what you're trying to do (I surmise).

    You need to back off to 10,000 feet and explain what you're trying to do, without me having to dissect your workbook.

  7. #7
    Registered User
    Join Date
    07-27-2009
    Location
    L
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Dynamic code Generation

    Hi there,

    OK Big picture my problem is two fold:

    1.) I need to generate a set of fucntions from a group of base functions. I want to do this through VBA to avoid having to type them all in manually.
    End goal I want these to be actual functions not strings.

    2.) These functions are then to be passed to Myfunction (shown below) so that each function in turn is fitted and its optimal parameters found.

    You don't need to understand the rest of the sheet just the part below (I think).

    The rest of the sheet is an optimiser. It evaluates "myfunction" with some input parameters. Is then calculates how good a fit you have to the sample data. It then updates the parameters to try and give a better fit. This process is repeated until a best fit is found.

    What I want to do is generate 8^8 permutations of all the possible linear combinations of the functions. I have amended your code to produce the same thing for the simpler case of 2 functions.

    The code below produces the correct set of functions and stores them as strings in asFunc. I would like to find a way to pass these functions to myFunction so that each of these functions in turn becomes the objective function is this possible?

    Or is there another way to achieve the same goal? I want to convert the strings in asFunc to actual functions so that they can be evaluated by the optimiser.
    Or alternately avois using strings all to gether and go stright to the functions.

    I hope this is clear, if not please ask and I will gladly explain further.

    Baz
    Please Login or Register  to view this content.
    Last edited by Bazman2; 08-19-2009 at 07:28 AM.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Dynamic code Generation

    The three ways I can think to do this are:

    1. Make sure the expressions have each variable isolated by white space, and replace each variable with its value, e.g.,

    Please Login or Register  to view this content.
    That will not run fast.

    2. Put the variables on a worskheet and evaluate there:

    Please Login or Register  to view this content.
    3. If the individual functions are known at design time, you can implement each in VBA and invoke them as a computed string

    Please Login or Register  to view this content.
    Last edited by shg; 08-19-2009 at 11:43 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1