Commits

mkur committed ac81939

Added equality constraints

Comments (0)

Files changed (3)

   let _ = check_result (ml_add_inequality_constraint opt fconstr tol) in ()
 ;;
 
+external ml_add_equality_constraint: t -> (float array -> (float array) option -> float) -> float -> result = "ml_nlopt_add_equality_constraint"
+let add_equality_constraint opt fconstr tol =
+  let _ = check_result (ml_add_equality_constraint opt fconstr tol) in ()
+;;
+
 (* Stopping criteria *)
 
 external ml_set_stopval: t -> float -> result = "ml_nlopt_set_stopval"
 val get_upper_bounds : t -> float array 
 
 val add_inequality_constraint: t -> (float array -> (float array) option -> float) -> float -> unit
+val add_equality_constraint: t -> (float array -> (float array) option -> float) -> float -> unit
   
 val set_stopval: t -> float -> unit
 val get_stopval: t -> float 

src/nlopt_wrapper.c

     CAMLreturn(Val_int(map_nlopt_result(res)));
 }
 
+
+value ml_nlopt_add_equality_constraint(value ml_opt, value ml_constr, value ml_tol)
+{
+    CAMLparam3(ml_opt, ml_constr, ml_tol);
+    CAMLlocal1(cons);
+
+    nlopt_opt opt = (nlopt_opt) Field(ml_opt, 1);
+    
+    value *constraints = (value *) Field(ml_opt, 3);
+
+    value *cb = (value *) malloc(sizeof(value)); // callback container
+
+    *cb = ml_constr;
+
+    nlopt_result res = nlopt_add_equality_constraint(opt, &ml_nlopt_callback_wrapper, (void *) cb, Double_val(ml_tol));
+
+    if (res == NLOPT_SUCCESS) 
+    {
+	caml_register_global_root(cb);
+
+	cons = caml_alloc(2, 0);
+
+	Store_field(cons, 0, (value) cb);     /* Head */
+	Store_field(cons, 1, *constraints);   /* Tail */
+	
+	*constraints = cons;
+    }
+    else
+	free(cb);
+    
+    CAMLreturn(Val_int(map_nlopt_result(res)));
+}
+
 /* Stopping criteria */
 
 value ml_nlopt_set_stopval(value ml_opt, value stopval)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.