lpsolve 1.0.1

High-level lpsolve wrapper
Documentation
//! Simple linear programming example: Production Planning
//!
//! A factory produces two products: chairs and tables
//! - Each chair requires 2 hours of labor and 1 unit of wood, profit $30
//! - Each table requires 3 hours of labor and 2 units of wood, profit $50
//! - Available: 100 hours of labor, 60 units of wood
//! - Goal: Maximize profit

use lpsolve::prelude::*;

fn main() -> Result<()> {
    println!("=== Production Planning Problem ===\n");

    // Using the terse builder API
    let solution = Problem::builder()
        .name("Production Planning")?
        .cols(2)
        .max(&[30.0, 50.0])  // Shorthand for maximize + objective
        .named_constraint("Labor", &[2.0, 3.0], Le, 100.0)?
        .named_constraint("Wood", &[1.0, 2.0], Le, 60.0)?
        .non_negative()  // All variables >= 0
        .variable_names(&["chairs", "tables"])?
        .solve()?;

    // Or even terser without names:
    // let solution = Problem::builder()
    //     .cols(2).max(&[30.0, 50.0]).non_negative()
    //     .le(&[2.0, 3.0], 100.0)
    //     .le(&[1.0, 2.0], 60.0)
    //     .solve()?;

    // Check if we found an optimal solution
    if solution.is_optimal() {
        println!("✓ Found optimal solution!");
        println!("  Objective value (profit): ${:.2}", solution.objective_value());

        // Get individual variable values
        if let Some(chairs) = solution.variable(1) {
            println!("  Chairs to produce: {:.2}", chairs);
        }
        if let Some(tables) = solution.variable(2) {
            println!("  Tables to produce: {:.2}", tables);
        }

        // Alternative: Get all variables at once
        if let Some(vars) = solution.variables() {
            println!("\n  Solution vector: {:?}", vars);
        }

        // Zero-copy access pattern using visitor
        println!("\n  Using visitor pattern:");
        solution.visit_variables(|index, value| {
            let name = if index == 0 { "chairs" } else { "tables" };
            println!("    {} (x{}): {:.2}", name, index + 1, value);
        });

        println!("\n  Solver statistics:");
        println!("    Iterations: {}", solution.iterations());
        println!("    Time elapsed: {:.4}s", solution.time_elapsed());
    } else {
        println!("✗ Problem status: {:?}", solution.status());
    }

    Ok(())
}