1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/// Crate `config` provides solver's configuration and CLI.
use {crate::types::DecisionLevel, std::path::PathBuf};

/// Configuration built from command line options
#[derive(Clone, Debug)]
pub struct Config {
    //
    // Switches
    //
    #[cfg(feature = "strategy_adaptation")]
    /// Strategy adaptation switch
    a_adaptive: i32,

    /// Eliminator switch
    a_elim: i32,

    /// Use Luby series forcibly
    a_luby: i32,

    /// Clause reduction switch
    a_reduce: i32,

    /// Re-phase switch
    a_rephase: i32,

    /// Reason-Side Rewarding switch
    a_rsr: i32,

    /// Stabilization switch
    a_stabilize: i32,

    /// Staging
    a_stage: i32,

    /// Vivification switch
    a_vivify: i32,

    //
    //## solver configuration
    //
    /// Dec. lvl to use chronoBT
    pub c_cbt_thr: DecisionLevel,

    /// Soft limit of #clauses (6MC/GB)
    pub c_cls_lim: usize,

    /// #cls to start in-processor
    pub c_ip_int: usize,

    /// CPU time limit in sec.
    pub c_tout: f64,

    //
    //## I/O configuration
    //
    /// Build Splr interface
    pub splr_interface: bool,

    /// DIMACS CNF file
    pub cnf_file: PathBuf,

    /// Output directory
    pub io_odir: PathBuf,

    /// DRAT Cert. filename
    pub io_pfile: PathBuf,

    /// Result filename/stdout
    pub io_rfile: PathBuf,

    /// Disable coloring
    pub no_color: bool,

    /// Disable any progress message
    pub quiet_mode: bool,

    /// Writes a DRAT UNSAT certification file
    pub use_certification: bool,

    /// Uses Glucose-like progress report
    pub use_log: bool,

    //
    //## eliminator
    //
    /// Max #lit for clause subsume
    pub elm_cls_lim: usize,

    /// Grow limit of #cls in var elimination
    pub elm_grw_lim: usize,

    /// Max #cls for var elimination
    pub elm_var_occ: usize,

    //
    //## restarter
    //
    /// #conflicts between restarts
    pub rst_step: usize,

    /// Length of assign. fast EMA
    pub rst_asg_len: usize,

    /// Length of assign. slow EMA
    pub rst_asg_slw: usize,

    /// Blocking restart threshold. Originally this was the Glucose's R.
    pub rst_asg_thr: f64,

    #[cfg(feature = "progress_ACC")]
    /// Conflict Correlation threshold
    pub rst_ccc_thr: f64,

    /// Length of LBD fast EMA
    pub rst_lbd_len: usize,

    /// Length of LBD slow EMA
    pub rst_lbd_slw: usize,

    /// Forcing restart threshold
    pub rst_lbd_thr: f64,

    #[cfg(feature = "progress_MLD")]
    /// Scaling for Maximum LBD of a Dep. graph
    pub rst_mld_scl: f64,

    #[cfg(feature = "progress_MLD")]
    /// Threshold for Maximum LBD of a Dep. graph
    pub rst_mld_thr: f64,

    /// Stabilizer scaling
    pub rst_stb_scl: f64,

    //
    //## vivifier
    //
    /// #reduction for next vivification
    pub viv_thr: usize,

    //
    //## staging
    //
    /// Decay rate for Extra reward for vars on stage
    pub stg_rwd_dcy: f64,
    /// Initial value for the extra reward for vars on stage
    pub stg_rwd_val: f64,

    //
    //## var rewarding
    //
    #[cfg(not(feature = "moving_var_reward_rate"))]
    pub vrw_dcy_rat: f64,

    #[cfg(feature = "moving_var_reward_rate")]
    /// Initial var reward decay
    pub vrw_dcy_beg: f64,

    #[cfg(feature = "moving_var_reward_rate")]
    /// Maximum var reward decay
    pub vrw_dcy_end: f64,

    /// Occurrence compression rate in LR rewarding
    pub vrw_occ_cmp: f64,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            #[cfg(feature = "strategy_adaptation")]
            a_adaptive: 0,

            a_elim: 1,
            a_luby: 0,
            a_reduce: 1,
            a_rephase: 1,
            a_rsr: 1,
            a_stabilize: 1,
            a_stage: 1,
            a_vivify: 0,

            c_cbt_thr: 100,
            c_cls_lim: 0,
            c_ip_int: 10000,
            c_tout: 5000.0,

            splr_interface: false,
            cnf_file: PathBuf::new(),
            io_odir: PathBuf::from("."),
            io_pfile: PathBuf::from("proof.out"),
            io_rfile: PathBuf::new(),
            no_color: false,
            quiet_mode: false,
            use_certification: false,
            use_log: false,

            elm_cls_lim: 32,
            elm_grw_lim: 0,
            elm_var_occ: 8192,

            rst_step: 24,
            rst_asg_len: 32,
            rst_asg_slw: 10000,
            rst_asg_thr: 0.10,

            #[cfg(feature = "progress_ACC")]
            rst_ccc_thr: 0.7,

            rst_lbd_len: 32,
            rst_lbd_slw: 8192,
            rst_lbd_thr: 1.20,

            #[cfg(feature = "progress_MLD")]
            rst_mld_scl: 0.10,
            #[cfg(feature = "progress_MLD")]
            rst_mld_thr: 0.80,

            rst_stb_scl: 2.0,

            stg_rwd_dcy: 0.5,
            stg_rwd_val: 1.0,

            viv_thr: 200,

            #[cfg(not(feature = "moving_var_reward_rate"))]
            vrw_dcy_rat: 0.96,
            #[cfg(feature = "moving_var_reward_rate")]
            vrw_dcy_beg: 0.90,
            #[cfg(feature = "moving_var_reward_rate")]
            vrw_dcy_end: 0.96,
            vrw_occ_cmp: 0.50,
        }
    }
}

impl Config {
    pub fn inject_from_args(&mut self) {
        let mut help = false;
        let mut version = false;
        if let Some(ref cnf) = std::env::args().last() {
            let path = PathBuf::from(cnf.clone());
            if path.exists() {
                self.cnf_file = path;
            }
        }
        let args = std::env::args();
        let mut iter = args.skip(1);
        while let Some(arg) = iter.next() {
            if let Some(stripped) = arg.strip_prefix("--") {
                let flags = ["no-color", "quiet", "certify", "log", "help", "version"];
                let options_i32 = [
                    "ADP", "ELI", "LBY", "RDC", "RPH", "RSR", "STB", "STG", "VIV",
                ];
                let options_u32 = ["cbt"];
                let options_usize = [
                    "cl", "ii", "stat", "ecl", "evl", "evo", "rs", "ral", "ras", "rll", "rls",
                    "vit",
                ];
                #[cfg(not(feature = "moving_var_reward_rate"))]
                let options_f64 = [
                    "timeout", "rat", "rct", "rlt", "rms", "rmt", "rss", "srd", "srv", "vdr", "vro",
                ];
                #[cfg(feature = "moving_var_reward_rate")]
                let options_f64 = [
                    "timeout", "rat", "rct", "rlt", "rms", "rmt", "rss", "srd", "srv", "vri",
                    "vrm", "vro",
                ];
                let options_path = ["dir", "proof", "result"];
                let seg: Vec<&str> = stripped.split('=').collect();
                match seg.len() {
                    1 => {
                        let name = &arg[2..];
                        if flags.contains(&name) {
                            match name {
                                "no-color" => self.no_color = true,
                                "quiet" => self.quiet_mode = true,
                                "certify" => self.use_certification = true,
                                "log" => self.use_log = true,
                                "help" => help = true,
                                "version" => version = true,
                                _ => panic!("invalid flag: {}", name),
                            }
                        } else if options_i32.contains(&name) {
                            if let Some(str) = iter.next() {
                                if let Ok(val) = str.parse::<i32>() {
                                    match name {
                                        #[cfg(feature = "strategy_adaptation")]
                                        "ADP" => self.a_adaptive = val,

                                        "ELI" => self.a_elim = val,
                                        "LBY" => self.a_luby = val,
                                        "RDC" => self.a_reduce = val,
                                        "RPH" => self.a_rephase = val,
                                        "RSR" => self.a_rsr = val,
                                        "STB" => self.a_stabilize = val,
                                        "STG" => self.a_stage = val,
                                        "VIV" => self.a_vivify = val,
                                        _ => panic!("invalid option: {}", name),
                                    }
                                } else {
                                    panic!("invalid value {}", name);
                                }
                            } else {
                                panic!("no argument for {}", name);
                            }
                        } else if options_u32.contains(&name) {
                            if let Some(str) = iter.next() {
                                if let Ok(val) = str.parse::<u32>() {
                                    match name {
                                        "cbt" => self.c_cbt_thr = val,
                                        _ => panic!("invalid option: {}", name),
                                    }
                                } else {
                                    panic!("invalid value {}", name);
                                }
                            } else {
                                panic!("no argument for {}", name);
                            }
                        } else if options_usize.contains(&name) {
                            if let Some(str) = iter.next() {
                                if let Ok(val) = str.parse::<usize>() {
                                    match name {
                                        "cl" => self.c_cls_lim = val,
                                        "ii" => self.c_ip_int = val,
                                        "ecl" => self.elm_cls_lim = val,
                                        "evl" => self.elm_grw_lim = val,
                                        "evo" => self.elm_var_occ = val,
                                        "rs" => self.rst_step = val,
                                        "ral" => self.rst_asg_len = val,
                                        "ras" => self.rst_asg_slw = val,
                                        "rll" => self.rst_lbd_len = val,
                                        "rls" => self.rst_lbd_slw = val,
                                        "vit" => self.viv_thr = val,
                                        _ => panic!("invalid option: {}", name),
                                    }
                                } else {
                                    panic!("invalid value {}", name);
                                }
                            } else {
                                panic!("no argument for {}", name);
                            }
                        } else if options_f64.contains(&name) {
                            if let Some(str) = iter.next() {
                                if let Ok(val) = str.parse::<f64>() {
                                    match name {
                                        "timeout" => self.c_tout = val,
                                        "rat" => self.rst_asg_thr = val,

                                        #[cfg(feature = "progress_ACC")]
                                        "rct" => self.rst_ccc_thr = val,

                                        "rlt" => self.rst_lbd_thr = val,

                                        #[cfg(feature = "progress_MLD")]
                                        "rms" => self.rst_mld_scl = val,
                                        #[cfg(feature = "progress_MLD")]
                                        "rmt" => self.rst_mld_thr = val,

                                        "rss" => self.rst_stb_scl = val,
                                        "srd" => self.stg_rwd_dcy = val,
                                        "srv" => self.stg_rwd_val = val,
                                        #[cfg(not(feature = "moving_var_reward_rate"))]
                                        "vdr" => self.vrw_dcy_rat = val,
                                        #[cfg(feature = "moving_var_reward_rate")]
                                        "vri" => self.vrw_dcy_beg = val,
                                        #[cfg(feature = "moving_var_reward_rate")]
                                        "vrm" => self.vrw_dcy_end = val,
                                        "vro" => self.vrw_occ_cmp = val,
                                        _ => panic!("invalid option: {}", name),
                                    }
                                } else {
                                    panic!("invalid value {}", name);
                                }
                            } else {
                                panic!("no argument for {}", name);
                            }
                        } else if options_path.contains(&name) {
                            if let Some(val) = iter.next() {
                                match name {
                                    "dir" => self.io_odir = PathBuf::from(val),
                                    "proof" => self.io_pfile = PathBuf::from(val),
                                    "result" => self.io_rfile = PathBuf::from(val),
                                    _ => panic!("invalid option: {}", name),
                                }
                            } else {
                                panic!("invalid value {}", name);
                            }
                        } else {
                            panic!("unknown option name {}", name);
                        }
                    }
                    _ => {
                        println!("connected long arg: {:?} = {:?}", seg[0], seg[1]);
                    }
                }
            } else if let Some(name) = arg.strip_prefix('-') {
                let flags = ["C", "q", "c", "l", "h", "V"];
                let options_path = ["o", "p", "r", "t"];
                if flags.contains(&name) {
                    match name {
                        "C" => self.no_color = true,
                        "q" => self.quiet_mode = true,
                        "c" => self.use_certification = true,
                        "l" => self.use_log = true,
                        "h" => help = true,
                        "V" => version = true,
                        _ => panic!("invalid flag: {}", name),
                    }
                } else if options_path.contains(&name) {
                    if let Some(val) = iter.next() {
                        match name {
                            "o" => self.io_odir = PathBuf::from(val),
                            "p" => self.io_pfile = PathBuf::from(val),
                            "r" => self.io_rfile = PathBuf::from(val),
                            "t" => self.c_tout = val.parse::<f64>().expect("-t requires a number"),
                            _ => panic!("invalid option: {}", name),
                        }
                    } else {
                        panic!("no argument for {}", name);
                    }
                } else {
                    panic!("unknown option name {}", name);
                }
            } else if !self.cnf_file.exists() || self.cnf_file.to_string_lossy() != arg {
                panic!("invalid argument: {}", arg);
            }
        }
        if help {
            println!("{}\n{}", env!("CARGO_PKG_DESCRIPTION"), help_string());
            std::process::exit(0);
        }
        if version {
            println!("{}", env!("CARGO_PKG_VERSION"));
            std::process::exit(0);
        }
    }
}

fn help_string() -> String {
    let config = Config::default();
    format!(
        "
USAGE:
  splr [FLAGS] [OPTIONS] <cnf-file>
FLAGS:
  -h, --help               Prints help information
  -C, --no-color           Disable coloring
  -q, --quiet              Disable any progress message
  -c, --certify            Writes a DRAT UNSAT certification file
  -l, --log                Uses Glucose-like progress report
  -V, --version            Prints version information
OPTIONS (\x1B[000m\x1B[031mred options depend on features in Cargo.toml\x1B[000m):
      \x1B[000m\x1B[031m--ADP <a-adaptive>   Strategy adaptation switch     {:>10}\x1B[000m
      --ELI <a-elim>       Eliminator switch              {:>10}
      --LBY <a-luby>       Use Luby series for restart    {:>10}
      --RDC <a-reduce>     Clause reduction switch        {:>10}
      --RPH <a-rephase>    Re-phase switch                {:>10}
      --RSR <a-rsr>        Reason-Side Rewarding switch   {:>10}
      --STB <a-stabilize>  Stabilization switch           {:>10}
      --STG <a-stage>      Stage switch                   {:>10}
      --VIV <a-vivify>     Vivification switch            {:>10}
      --cbt <c-cbt-thr>    Dec. lvl to use chronoBT       {:>10}
      --cl <c-cls-lim>     Soft limit of #clauses (6MC/GB){:>10}
      --ii <c-ip-int>      #cls to start in-processor     {:>10}
  -t, --timeout <c-tout>   CPU time limit in sec.         {:>10}
      --ecl <elm-cls-lim>  Max #lit for clause subsume    {:>10}
      --evl <elm-grw-lim>  Grow limit of #cls in var elim.{:>10}
      --evo <elm-var-occ>  Max #cls for var elimination   {:>10}
  -o, --dir <io-odir>      Output directory                {:>10}
  -p, --proof <io-pfile>   DRAT Cert. filename                {:>10}
  -r, --result <io-rfile>  Result filename/stdout             {:>10}
      --ral <rst-asg-len>  Length of assign. fast EMA     {:>10}
      --ras <rst-asg-slw>  Length of assign. slow EMA     {:>10}
      --rat <rst-asg-thr>  Blocking restart threshold        {:>10.2}
      \x1B[000m\x1B[031m--rct <rst-ccc-thr>  Conflict Correlation threshold    {:>10.2}\x1B[000m
      --rll <rst-lbd-len>  Length of LBD fast EMA         {:>10}
      --rls <rst-lbd-slw>  Length of LBD slow EMA         {:>10}
      --rlt <rst-lbd-thr>  Forcing restart threshold         {:>10.2}
      \x1B[000m\x1B[031m--rms <rst-mld-scl>  Scaling for Max LBD of Dep.       {:>10.2}\x1B[000m
      \x1B[000m\x1B[031m--rmt <rst-mld-thr>  Threshold for Max LBD of Dep.     {:>10.2}\x1B[000m
      --rss <rst-stb-scl>  Stabilizer scaling                {:>10.2}
      --rs  <rst-step>     #conflicts between restarts    {:>10}
      --srd <stg-rwd-dcy>  Decay rate for staged vare reward {:>10.2}
      --srv <stg-rwd-val>  Extra reward for staged vars      {:>10.2}
      --vit <viv-thr>      #clause to try to vivify       {:>10}
      \x1B[000m\x1B[031m--vri <vrw-dcy-beg>  Initial var reward decay          {:>10.2}\x1B[000m
      \x1B[000m\x1B[031m--vrm <vrw-dcy-end>  Maximum var reward decay          {:>10.2}\x1B[000m
      \x1B[000m\x1B[031m--vro <vrw-occ-cmp>  Occ. compression rate in LR       {:>10.2}\x1B[000m
ARGS:
  <cnf-file>    DIMACS CNF file
",
        {
            #[cfg(not(feature = "strategy_adaptation"))]
            {
                0.0
            }
            #[cfg(feature = "strategy_adaptation")]
            {
                config.a_adaptive
            }
        },
        config.a_elim,
        config.a_luby,
        config.a_reduce,
        config.a_rephase,
        config.a_rsr,
        config.a_stabilize,
        config.a_stage,
        config.a_vivify,
        config.c_cbt_thr,
        config.c_cls_lim,
        config.c_ip_int,
        config.c_tout,
        config.elm_cls_lim,
        config.elm_grw_lim,
        config.elm_var_occ,
        config.io_odir.to_string_lossy(),
        config.io_pfile.to_string_lossy(),
        config.io_rfile.to_string_lossy(),
        config.rst_asg_len,
        config.rst_asg_slw,
        config.rst_asg_thr,
        {
            #[cfg(not(feature = "progress_ACC"))]
            {
                0.0
            }
            #[cfg(feature = "progress_ACC")]
            {
                config.rst_ccc_thr
            }
        },
        config.rst_lbd_len,
        config.rst_lbd_slw,
        config.rst_lbd_thr,
        {
            #[cfg(feature = "progress_MLD")]
            {
                config.rst_mld_scl
            }
            #[cfg(not(feature = "progress_MLD"))]
            {
                0.0
            }
        },
        {
            #[cfg(feature = "progress_MLD")]
            {
                config.rst_mld_thr
            }
            #[cfg(not(feature = "progress_MLD"))]
            {
                0.0
            }
        },
        config.rst_stb_scl,
        config.rst_step,
        config.stg_rwd_dcy,
        config.stg_rwd_val,
        config.viv_thr,
        {
            #[cfg(not(feature = "moving_var_reward_rate"))]
            {
                0.0
            }
            #[cfg(feature = "moving_var_reward_rate")]
            {
                config.vrw_dcy_beg
            }
        },
        {
            #[cfg(not(feature = "moving_var_reward_rate"))]
            {
                0.0
            }
            #[cfg(feature = "moving_var_reward_rate")]
            {
                config.vrw_dcy_end
            }
        },
        {
            #[cfg(not(feature = "moving_var_reward_rate"))]
            {
                0.0
            }
            #[cfg(feature = "moving_var_reward_rate")]
            {
                config.vrw_occ_cmp
            }
        },
    )
}

impl<T> From<T> for Config
where
    PathBuf: From<T>,
{
    fn from(path: T) -> Config {
        Config {
            cnf_file: PathBuf::from(path),
            ..Config::default()
        }
    }
}

macro_rules! dispatch {
    // from `0` and `1`
    ($field: expr) => {
        0 != $field
    };
    // from -1, 0, 1
    ($field: expr, $default: expr) => {
        match $field {
            0 => $default,
            x => 0 < x,
        }
    };
}

impl Config {
    #[allow(unused_mut)]
    pub fn override_args(mut self) -> Config {
        self
    }
    pub fn use_elim(&self) -> bool {
        dispatch!(self.a_elim)
    }
    pub fn use_luby(&self) -> bool {
        dispatch!(self.a_luby)
    }
    pub fn use_reduce(&self) -> bool {
        dispatch!(self.a_reduce)
    }
    pub fn use_rephase(&self) -> bool {
        dispatch!(self.a_rephase)
    }
    pub fn use_vivify(&self) -> bool {
        dispatch!(self.a_vivify)
    }
    pub fn use_reason_side_rewarding(&self) -> bool {
        dispatch!(self.a_rsr)
    }
    pub fn use_stabilize(&self) -> bool {
        dispatch!(self.a_stabilize)
    }
    pub fn use_stage(&self) -> bool {
        dispatch!(self.a_stage)
    }
    #[cfg(feature = "strategy_adaptation")]
    pub fn use_adaptive(&self) -> bool {
        dispatch!(self.a_adaptive)
    }
}