jasonrhansen / cut-optimizer-2d

2D Cut Optimizer
Apache License 2.0
21 stars 11 forks source link

Incorrect ResultCutPiece quantity for case "pighetti_c" #13

Closed lukepighetti closed 2 years ago

lukepighetti commented 2 years ago

Summary

Trying to cut 1 piece, getting 16 pieces instead

Version

Currently on cut-optimizer-2d git rev 80d7031

Cut spec

case pighetti_c

kerf width of 2
1x Stock, 2440 x 1220
1x Piece, 775 x 150

Human solution

Screen Shot 2022-03-13 at 11 25 27 AM

MCVE

fn case_pighetti_c() {
    let case_name = "pighetti_c";

    let plywood = StockPiece {
        quantity: Some(1),
        length: 2240,
        width: 1220,
        pattern_direction: PatternDirection::ParallelToLength,
        price: 130,
    };

    let cut_piece_a = CutPiece {
        quantity: 1,
        external_id: Some(1),
        length: 775,
        width: 150,
        can_rotate: false,
        pattern_direction: PatternDirection::ParallelToLength,
    };

    let mut optimizer = Optimizer::new();
    optimizer.add_stock_piece(plywood);
    optimizer.add_cut_piece(cut_piece_a);
    optimizer.set_cut_width(2);

    optimizer.optimize_and_print(case_name);
}

trait OptimizeAndPrint {
    fn optimize_and_print(&self, case_name: &str);
}

impl OptimizeAndPrint for cut_optimizer_2d::Optimizer {
    fn optimize_and_print(&self, case_name: &str) {
        println!("[{}]", case_name);

        fn simple_callback(_: f64) {
            // print!("Progress {}", progress);
        }

        let result = self.optimize_guillotine(simple_callback);
        match result {
            Err(error) => println!("error!, {:?}", error),
            Ok(solution) => {
                println!("Fitness: {}", solution.fitness);
                println!("Stock pieces: {}", solution.stock_pieces.len());
                for stock_piece in solution.stock_pieces {
                    println!(
                        "Stock: {}x{}, cut pieces: {}",
                        stock_piece.length,
                        stock_piece.width,
                        stock_piece.cut_pieces.len()
                    );
                    for cut_piece in stock_piece.cut_pieces {
                        println!(
                            "Cut piece: {}x{} at X={} Y={}",
                            cut_piece.length, cut_piece.width, cut_piece.x, cut_piece.y
                        );
                    }
                }
            }
        }

        println!("");
    }
}

Result

[pighetti_c]
Fitness: 0.47042281670019365
Stock pieces: 1
Stock: 2240x1220, cut pieces: 16
Cut piece: 775x150 at X=0 Y=0
Cut piece: 775x150 at X=0 Y=777
Cut piece: 775x150 at X=152 Y=0
Cut piece: 775x150 at X=152 Y=777
Cut piece: 775x150 at X=304 Y=0
Cut piece: 775x150 at X=304 Y=777
Cut piece: 775x150 at X=456 Y=0
Cut piece: 775x150 at X=456 Y=777
Cut piece: 775x150 at X=608 Y=0
Cut piece: 775x150 at X=760 Y=0
Cut piece: 775x150 at X=912 Y=0
Cut piece: 775x150 at X=1064 Y=0
Cut piece: 775x150 at X=608 Y=777
Cut piece: 775x150 at X=760 Y=777
Cut piece: 775x150 at X=912 Y=777
Cut piece: 775x150 at X=1064 Y=777
Screen Shot 2022-03-13 at 11 25 23 AM
jasonrhansen commented 2 years ago

Interesting. It looks like this is happening on the latest released version as well. I thought it might be an issue with the new quantity field on CutPiece, but that's not the case. I tested this on 12a1c6e and it's giving the same strange result. I'll do some debugging to figure out what's going on.

jasonrhansen commented 2 years ago

If I set the StockPiece quantity to None it gives the correct result so the bug must be in the quantity handling for stock pieces.

jasonrhansen commented 2 years ago

It turned out to be a problem when the quantity of the stock piece was 1.

lukepighetti commented 2 years ago

Thanks for the fast triage and resolution!

lukepighetti commented 2 years ago

This is resolved after upgrading to rev https://github.com/jasonrhansen/cut-optimizer-2d/commit/e382e13ca537303d90ef54d517abc620f31d278a, thanks!