Rust interface#672
Conversation
|
@mmghannam @bzfkocht Claude did a great job 😄 |
51611a9 to
26922ff
Compare
|
Had a quick chat with @mmghannam. The following steps are:
Example file: https://github.com/scipopt/scip-sys/blob/main/build.rs |
|
looks good I would just add a small correction
not precompiled packages, just the source code, and it will pull the precompiled dependencies at build time. |
Ah my bad, it's now fixed. |
Not always, one just needs to try and see :) |
|
Hello, I just looked at the example in example_hs015.rs, and I was wondering whether it would make sense for the crate to provide a more idiomatic Rust interface on top of the raw C callbacks. At the moment, users need to define several functions, such as: This adds a lot of boilerplate for the user. Would it be possible for the crate to handle the conversion between Rust closures/functions and the underlying Uno callback types internally? That way, users could provide ordinary Rust functions or closures, while the crate handles the FFI layer. I'm curious whether you've considered this approach or whether there are technical limitations that make it difficult to support. |
|
@GermanHeim I had Claude rewrite the wrapper and generate a more idiomatic example. Let me know what you think! |
|
I just saw the example, and it is definitely much better! However, this part of the code, in my opinion, isn't very Rusty: problem.set_objective(UNO_MINIMIZE,
|x, obj| { *obj = 100.0 * (x[1] - x[0] * x[0]).powi(2) + (1.0 - x[0]).powi(2); Ok(()) },
|x, g| { g[0] = 400.0 * x[0].powi(3) - 400.0 *x[0] * x[1] + 2.0 * x[0] - 2.0; g[1] = 200.0 * (x[1] - x[0] * x[0]); Ok(()) },
).expect("set objective");This API probably comes from C/C++ style. Maybe because the underlying API is something like: void evaluate_objective(
int n,
const double* x,
double* obj
);? I think a better approach (here it might be more of a personal opinion) for the Rust API might be: problem.set_objective_fn(|x| {
100.0 * (x[1] - x[0] * x[0]).powi(2)
+ (1.0 - x[0]).powi(2)
});
problem.set_gradient_fn(|x, grad| {
grad[0] = ...;
grad[1] = ...;
});Other Rust libraries might implement a Struct. For example, argmin. Here is an example of their API. Again, this is very much a personal preference, so whatever you decide is better will be fine. The API is much better right now and completely usable. |
|
Indeed. In terms of parameters, we can implement something similar to what we have in the Python bindings (see here). I wouldn't necessarily separate |
Closes #357