This project is read-only.

Optimization

Apr 21, 2010 at 8:03 PM

Hi

Great work..  Thanks for sharing your work, I was looking for this type of math library for long time.

For my project, i have to use excel solver to fine optimum value. But that makes slowing my program and i was thinking for alternative method.

I tried to use your library for optimization. But i don't know how to start it. and i could not find any example for optimization.

is this library limited to particular type of function ?

Because in my case, i have to find a min value for sigmodial function.

 

Nitharsan

Coordinator
Apr 28, 2010 at 5:59 PM
Edited Apr 28, 2010 at 6:00 PM
Hi Nitharsan, for a maximum of flexbility, the specified function to optimize can be defined in a method by using sourcecode. Following example shows the way to do:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMathLibrary;
using SmartMathLibrary.Optimization;

namespace OptSample
{
    class Program
    {
        static double FunctionToOptimize2D(GeneralVector param)
        {
            //function to minimize: x^2 + y
            return param[0] * param[0] + param[1];
        }

        static void Main(string[] args)
        {
            PrincipalAxisMultidimensionalOptimizer opt = new PrincipalAxisMultidimensionalOptimizer(new HardMultivariateRealFunction(FunctionToOptimize2D));

            Console.WriteLine(opt.FindMinimum(1e-5, 0.25, new GeneralVector(new double[] { 1, 1 })));
            Console.ReadKey();
        }
    }
}

Take care, this mostly works only with multivariate functions. In a 1D case, the algorithm does not converges. Another way is to use the HookeJeevesOptimizer class. It works better for 1D functions. Here is an example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMathLibrary;
using SmartMathLibrary.Optimization;

namespace OptSample
{
    class Program
    {
        static double FunctionToOptimize1D(GeneralVector param)
        {
            //function to minimize: 1/(1 + e^-t)
            return 1 / (1 + Math.Pow(Math.E, -param[0]));
        }

        static void Main(string[] args)
        {
            HookeJeevesOptimizer opt = new HookeJeevesOptimizer(new HardMultivariateRealFunction(FunctionToOptimize1D));

            Console.WriteLine(opt.FindMinimum(new GeneralVector(new double[] { 1 }),1e-5,0.25,10));
            Console.ReadKey();
        }
    }
}
Some words about the examples you didn´t find. There are currently no code snippets, but I´m going to write some after I complete the unit tests. I hope the above samples will help you. Please let me know if you have any other problems with the current task or any other. Best regards Benedikt
Apr 28, 2010 at 8:17 PM
I tried ur first example vb.net . But it gives error message.. Error 1 Value of type 'Double' cannot be converted to 'SmartMathLibrary.HardMultivariateRealFunctionPointer'. Error 2 Argument not specified for parameter 'param' of 'Private Shared Function FunctionToOptimize2D(param As SmartMathLibrary.GeneralVector) As Double' my function I have n no of lab data, those data have to be fitted in the following form. f_(x,i)=logE*=δ + α / ( 1 + e^ ( β + γ ( logt-c ( logηi - logηr ) ) ) ) Eqn 1 ,α,β,ϒ,c – Fitting Parameter (have to be found by minimizing eqn2) E* - Predicted from above Eqn 1 for each i. Log t, logηi , logηr - lab data (n number of data) Minimize the function ∑ ( f(x,i) - logEi )^2 Eqn 2 Ei - Lab data (n no of data) I just defined the function as you said . but because of the error , i could not continue. Nitharsan
Coordinator
Apr 28, 2010 at 8:59 PM
Hi, No problem. The VB.Net version has to be set up as following shown:
Imports System
Imports SmartMathLibrary
Imports SmartMathLibrary.Optimization

Module Module1
    Private Function FunctionToOptimize1D(ByVal param As GeneralVector) As Double
        'function to minimize: 1/(1 + e^-t)
        Return 1 / (1 + Math.Pow(Math.E, -param(0)))
    End Function

    Sub Main(ByVal args As String())
        Dim opt As New HookeJeevesOptimizer(New HardMultivariateRealFunction(New HardMultivariateRealFunctionPointer(AddressOf FunctionToOptimize1D)))

        Console.WriteLine(opt.FindMinimum(New GeneralVector(New Double() {1}), 0.00001, 0.25, 10))
        Console.ReadKey()
    End Sub
End Module
Regards Benedikt
Apr 28, 2010 at 9:57 PM
Hi it works. problem was , I did not think about delegates. It is very helpful to my project. Thank you very much Nitharsan
Oct 13, 2010 at 7:17 PM

Hai

I got a problem when using your tools in 64 bit system.

Error :

"Could not load file or assembly 'SmartMathLibrary.InternalRoutines, Version=1.0.3640.38164, Culture=neutral, PublicKeyToken=1dac2ae805ffbca4' or one of its dependencies.

An attempt was made to load a program with an incorrect format."


But it works in 32 bit system.

 

will this tool support for 32 bit system?

 

Nitharsan

 

Coordinator
Oct 22, 2010 at 12:00 PM

Hello Nitharsan,

 

The SmartMathLibrary.InternalRoutines is written in C++/CLI. In my point of view, you have to change the compiler options for this namespace. Try to change it from 32 to 64 bit, compile the source again and check if it works. I´m currently working with a 32 bit OS, so I´m not able to reproduce your error.

Please let me know it the way works, otherwise I will think about another solution.


Kind regards

 

Benedikt

Nov 1, 2010 at 8:34 PM

Hi

You mean i need to create separate dll for 32bit and 64 bit?

 

Thanks

Nitharsan