Hi,
 
for your information a .net wrapper for the nice NLopt project is part of the
Dodoni.net project. 
 
http://dodoni.codeplex.com
 
The following link contains a short summary how NLopt functions can be applied: 
 
http://dodoni.codeplex.com/wikipage?title=Dodoni.MathLibrary.Native.NLopt&referringTitle=Documentation
 
Feedback is welcome!
 
 
 
I have some problem with vector-valued constraints, perhaps there is a small
bug? The following is a small C# wrapper that shows the problem - I was to lazy
to apply it directly in C++, but it should be no difference. In the code
fragment I apply "nlopt_add_inequality_mconstraint" to a specific function
"TestConstraint" with several parameters which represents the function c:R^n -->
R^m in the Reference of the Nlopt project. In my case n = 3 and m = 2, i.e. two
constraints, but the argument x in "TestConstraint" have a length of m = 2 only.
Any idea why this happens?
 
Best wishes
Markus Wendt
 
 
using System;
using System.Text;
using System.Security;
using System.Collections.Generic;
using System.Runtime.InteropServices;
 
namespace NLoptProblemPrj
 {
  [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
     public delegate double NLoptObjectiveFunction(int n,
       [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] x,
       [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0), In, Out] double[]
grad,
        IntPtr data);
 
  [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
     public delegate void NLoptVectorInequalityConstraintFunction(int m,
       [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0), Out] double[]
result, 
       int n, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] double[] x,
       [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0), In, Out] double[]
grad,
       IntPtr data);
 
     public enum NLoptResultCode
     {
         Failure = -1,
         InvalidArguments = -2,
         OutOfMemory = -3,
         RoundoffLimited = -4,
         ForcedStop = -5,
         Success = 1,
         StopValReached = 2,
         FToleranceReached = 3,
         XToleranceReached = 4,
         MaximalNumberOfFunctionEvaluationsReached = 5,
         MaximalTimeReached = 6
     }
 
 public class NLoptTest
 {
  [DllImport("libNLopt.dll", EntryPoint = "nlopt_create",
         CallingConvention = CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
    private static extern IntPtr nlopt_create(int algorithm, int n);
 
  [DllImport("libNLopt.dll", EntryPoint = "nlopt_optimize",
        CallingConvention = CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
   private static extern NLoptResultCode nlopt_optimize(IntPtr opt, double[]
argMin, 
                   out double minimum);
 
  [DllImport("libNLopt.dll", EntryPoint = "nlopt_set_min_objective",
        CallingConvention = CallingConvention.Cdecl)]
  private static extern NLoptResultCode nlopt_set_min_objective(IntPtr opt, 
      [MarshalAs(UnmanagedType.FunctionPtr)] NLoptObjectiveFunction f, IntPtr
data);
 
  [DllImport("libNLopt.dll", EntryPoint = "nlopt_set_ftol_rel",
      CallingConvention = CallingConvention.Cdecl)]
   private static extern NLoptResultCode nlopt_set_ftol_rel(IntPtr opt, double
tolerance);
 
  [DllImport("libNLopt.dll", EntryPoint = "nlopt_set_lower_bounds",
     CallingConvention = CallingConvention.Cdecl)]
  private static extern NLoptResultCode nlopt_set_lower_bounds(IntPtr opt,
       double[] lowerBounds);
 
 [DllImport("libNLopt.dll", EntryPoint = "nlopt_set_upper_bounds",
      CallingConvention = CallingConvention.Cdecl)]
  private static extern NLoptResultCode nlopt_set_upper_bounds(IntPtr opt,
double[] upperBounds);
 
         [DllImport("libNLopt.dll", EntryPoint =
 "nlopt_add_inequality_mconstraint", CallingConvention =
 CallingConvention.Cdecl)]
         private static extern NLoptResultCode
 nlopt_add_inequality_mconstraint(IntPtr opt, int m,
 [MarshalAs(UnmanagedType.FunctionPtr)] NLoptVectorInequalityConstraintFunction
 constraintFunction, IntPtr data, double[] tolerance);
 
 
         public NLoptTest()
         {
             int d = 3; // = dimension
 
             int optType = 25; // LN_COBYLA
 
             IntPtr ptr = nlopt_create(optType, d);
             nlopt_set_ftol_rel(ptr, 1E-5);
             nlopt_set_lower_bounds(ptr, new[] { 0.0, 0.0, 0.0 });
             nlopt_set_upper_bounds(ptr, new[] { 999.0, 999.0, 999.0 });
 
             int m = 2; // two inequality constraints
             var res = nlopt_add_inequality_mconstraint(ptr, m, TestConstraint,
 IntPtr.Zero, new[] { 0.001, 0.001 });
             nlopt_set_min_objective(ptr, TestFct, IntPtr.Zero);
 
             var argMin = new[] { 1.0, 1.0, 1.0 };
             double min;
             nlopt_optimize(ptr, argMin, out min);
         }
 
         private void TestConstraint(int m, double[] result, int n, double[] x,
 double[] grad, IntPtr data)
         {
             /* it holds m = 2 and n = 2, as well as result.Length = 2, thus
o.k.
              * but x.Length = 2 and not = 3! */
             if (x.Length < 3)
             {
                 throw new ArgumentException("x has wrong dimension!");  //
  <-------- this exception will be thrown in any case!
             }
         }
 
         private double TestFct(int n, double[] x, double[] grad, IntPtr data)
         {
             return Math.Pow(x[0], 2) + Math.Pow(x[1], 2) + Math.Pow(x[2], 2);
         }
     }
 }

_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss

Reply via email to