Skip to content

Commit

Permalink
Add example as file instead of including in README
Browse files Browse the repository at this point in the history
  • Loading branch information
mmghannam committed Mar 10, 2024
1 parent ad77c61 commit e0b60d8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 78 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ raw = []
bundled = ["scip-sys/bundled"]

[dependencies]
scip-sys = { version= "0.1.7" }
doc-comment = "0.3.3"
scip-sys = { path="../scip-sys", version= "0.1.7" }

[dev-dependencies]
rayon = "1.5.1"
Expand Down
43 changes: 6 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,14 @@ Alternatively, you can specify the installation directory through the `SCIPOPTDI
*russcip* is tested against SCIP 9.0.0 but it might work for other versions depending on which functionality you use.


## Example
Model and solve an integer program.
```rust
use russcip::prelude::*;

fn main() {
// Create model
let mut model = Model::new()
.hide_output()
.include_default_plugins()
.create_prob("test")
.set_obj_sense(ObjSense::Maximize);

// Add variables
let x1 = model.add_var(0., f64::INFINITY, 3., "x1", VarType::Integer);
let x2 = model.add_var(0., f64::INFINITY, 4., "x2", VarType::Integer);

// Add constraints
model.add_cons(vec![x1.clone(), x2.clone()], &[2., 1.], -f64::INFINITY, 100., "c1");
model.add_cons(vec![x1.clone(), x2.clone()], &[1., 2.], -f64::INFINITY, 80., "c2");

let solved_model = model.solve();

let status = solved_model.status();
println!("Solved with status {:?}", status);

let obj_val = solved_model.obj_val();
println!("Objective value: {}", obj_val);

let sol = solved_model.best_sol().unwrap();
let vars = solved_model.vars();

for var in vars {
println!("{} = {}", &var.name(), sol.val(var));
}
}

### Examples
An [example](examples/create_and_solve.rs) on how to model and solve an integer program can be found in the [examples](examples) directory.
To run the example, you can use the following command
```bash
cargo run --example create_and_solve
```


## The `raw` feature
You can enable this feature by specifying the feature in your `Cargo.toml`
```toml
Expand Down
33 changes: 33 additions & 0 deletions examples/create_and_solve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use russcip::prelude::*;

fn main() {
// Create model
let mut model = Model::new()
.hide_output()
.include_default_plugins()
.create_prob("test")
.set_obj_sense(ObjSense::Maximize);

// Add variables
let x1 = model.add_var(0., f64::INFINITY, 3., "x1", VarType::Integer);
let x2 = model.add_var(0., f64::INFINITY, 4., "x2", VarType::Integer);

// Add constraints
model.add_cons(vec![x1.clone(), x2.clone()], &[2., 1.], -f64::INFINITY, 100., "c1");
model.add_cons(vec![x1.clone(), x2.clone()], &[1., 2.], -f64::INFINITY, 80., "c2");

let solved_model = model.solve();

let status = solved_model.status();
println!("Solved with status {:?}", status);

let obj_val = solved_model.obj_val();
println!("Objective value: {}", obj_val);

let sol = solved_model.best_sol().unwrap();
let vars = solved_model.vars();

for var in vars {
println!("{} = {}", &var.name(), sol.val(var));
}
}
41 changes: 2 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,10 @@
//! # russcip
//! Safe Rust interface for SCIP.
//! Safe Rust interface for [SCIP](https://scipopt.org/) optimization suite.
//!
//! # Example
//! Model and solve an integer program.
//! ```rust
//! use russcip::prelude::*;
//!
//! // Create model
//! let mut model = Model::new()
//! .hide_output()
//! .include_default_plugins()
//! .create_prob("test")
//! .set_obj_sense(ObjSense::Maximize);
//!
//! // Add variables
//! let x1 = model.add_var(0., f64::INFINITY, 3., "x1", VarType::Integer);
//! let x2 = model.add_var(0., f64::INFINITY, 4., "x2", VarType::Integer);
//!
//! // Add constraints
//! model.add_cons(vec![x1.clone(), x2.clone()], &[2., 1.], -f64::INFINITY, 100., "c1");
//! model.add_cons(vec![x1.clone(), x2.clone()], &[1., 2.], -f64::INFINITY, 80., "c2");
//!
//! let solved_model = model.solve();
//!
//! let status = solved_model.status();
//! println!("Solved with status {:?}", status);
//!
//! let obj_val = solved_model.obj_val();
//! println!("Objective value: {}", obj_val);
//!
//! let sol = solved_model.best_sol().expect("No solution found");
//! let vars = solved_model.vars();
//!
//! for var in vars {
//! println!("{} = {}", &var.name(), sol.val(var));
//! }
//! For examples and usage, please refer to the [repository](https://github.com/scipopt/russcip).

#![deny(missing_docs)]

extern crate core;
extern crate doc_comment;
doc_comment::doctest!("../README.md");

/// Re-exports the `scip_sys` crate, which provides low-level bindings to the SCIP library.
pub use scip_sys as ffi;

Expand Down

0 comments on commit e0b60d8

Please sign in to comment.