| ︙ | | |
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
-
+
+
+
|
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
//
//! The main bundle method solver.
use crate::{Aggregatable, DVector, Real};
use crate::{Evaluation, FirstOrderProblem, HKWeighter, Update};
use crate::{Evaluation, FirstOrderProblem, Update};
use crate::master::CplexMaster;
use crate::master::{BoxedMasterProblem, MasterProblem, MinimalMaster};
use crate::weighter::{HKWeightable, HKWeighter, Weighter};
use log::{debug, info, warn};
use std::error::Error;
use std::f64::{INFINITY, NEG_INFINITY};
use std::fmt;
use std::mem::swap;
|
| ︙ | | |
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
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
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
sgnorm: $slf.sgnorm,
weight: $slf.master.weight(),
step: $step,
expected_progress: $slf.expected_progress,
}
};
}
impl<'a> HKWeightable for BundleState<'a> {
fn current_weight(&self) -> Real {
self.weight
}
fn center(&self) -> &DVector {
self.cur_y
}
fn center_value(&self) -> Real {
self.cur_val
}
fn candidate_value(&self) -> Real {
self.nxt_val
}
fn candidate_model(&self) -> Real {
self.nxt_mod
}
fn new_cutvalue(&self) -> Real {
self.new_cutval
}
fn sgnorm(&self) -> Real {
self.sgnorm
}
}
/**
* Termination predicate.
*
* Given the current state of the bundle method, this function returns
* whether the solution process should be stopped.
*/
|
| ︙ | | |
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
|
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
-
-
-
-
-
-
-
-
-
-
-
|
fn default() -> StandardTerminator {
StandardTerminator {
termination_precision: 1e-3,
}
}
}
/**
* Bundle weight controller.
*
* Given the current state of the bundle method, this function determines the
* weight factor of the quadratic term for the next iteration.
*/
pub trait Weighter {
/// Return the new weight of the quadratic term.
fn weight(&mut self, state: &BundleState, params: &SolverParams) -> Real;
}
/// An invalid value for some parameter has been passes.
#[derive(Debug)]
pub struct ParameterError(String);
impl fmt::Display for ParameterError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", self.0)
|
| ︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
|
/// This is the last primal generated by the oracle.
pub fn last_primal(&self, fidx: usize) -> Option<&Pr> {
self.minorants[fidx].last().and_then(|m| m.primal.as_ref())
}
}
/// The default bundle solver with general master problem.
pub type DefaultSolver<P> = Solver<P, BoxedMasterProblem<CplexMaster>>;
pub type DefaultSolver<P> = Solver<P, HKWeighter, BoxedMasterProblem<CplexMaster>>;
/// A bundle solver with a minimal cutting plane model.
pub type NoBundleSolver<P> = Solver<P, BoxedMasterProblem<MinimalMaster>>;
pub type NoBundleSolver<P> = Solver<P, HKWeighter, BoxedMasterProblem<MinimalMaster>>;
/**
* Implementation of a bundle method.
*/
pub struct Solver<P, M = BoxedMasterProblem<CplexMaster>>
pub struct Solver<P, W, M = BoxedMasterProblem<CplexMaster>>
where
P: FirstOrderProblem,
{
/// The first order problem description.
problem: P,
/// The solver parameter.
pub params: SolverParams,
/// Termination predicate.
pub terminator: Box<Terminator>,
/// Weighter heuristic.
pub weighter: Box<Weighter>,
pub weighter: W,
/// Lower and upper bounds of all variables.
bounds: Vec<(Real, Real)>,
/// Current center of stability.
cur_y: DVector,
|
| ︙ | | |
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
|
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
|
-
+
+
+
-
+
-
+
|
/// The active minorant indices for each subproblem.
minorants: Vec<Vec<MinorantInfo<P::Primal>>>,
/// Accumulated information about the last iteration.
iterinfos: Vec<IterationInfo>,
}
impl<P, M> Solver<P, M>
impl<P, W, M> Solver<P, W, M>
where
P: FirstOrderProblem,
P::Err: Into<Box<std::error::Error + Send + Sync + 'static>>,
W: for<'a> Weighter<BundleState<'a>> + Default,
M: MasterProblem<MinorantIndex = usize>,
M::Err: Into<Box<std::error::Error + Send + Sync + 'static>>,
{
/**
* Create a new solver for the given problem.
*
* Note that the solver owns the problem, so you cannot use the
* same problem description elsewhere as long as it is assigned to
* the solver. However, it is possible to get a reference to the
* internally stored problem using `Solver::problem()`.
*/
#[allow(clippy::type_complexity)]
pub fn new_params(problem: P, params: SolverParams) -> Result<Solver<P, M>, SolverError<P::Err, M::Err>> {
pub fn new_params(problem: P, params: SolverParams) -> Result<Solver<P, W, M>, SolverError<P::Err, M::Err>> {
Ok(Solver {
problem,
params,
terminator: Box::new(StandardTerminator::default()),
weighter: Box::new(HKWeighter::new()),
weighter: W::default(),
bounds: vec![],
cur_y: dvec![],
cur_val: 0.0,
cur_mod: 0.0,
cur_vals: dvec![],
cur_mods: dvec![],
cur_valid: false,
|
| ︙ | | |
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
+
-
+
|
master: M::new()?,
minorants: vec![],
iterinfos: vec![],
})
}
/// A new solver with default parameter.
#[allow(clippy::type_complexity)]
pub fn new(problem: P) -> Result<Solver<P, M>, SolverError<P::Err, M::Err>> {
pub fn new(problem: P) -> Result<Solver<P, W, M>, SolverError<P::Err, M::Err>> {
Solver::new_params(problem, SolverParams::default())
}
/**
* Set the first order problem description associated with this
* solver.
*
|
| ︙ | | |
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
|
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
|
-
+
|
// *not* be the initial weight for the first iteration.
self.master.set_weight(1.0)?;
self.master.solve(self.cur_val)?;
self.sgnorm = self.master.get_dualoptnorm2().sqrt();
// Compute the real initial weight.
let state = current_state!(self, Step::Term);
let new_weight = self.weighter.weight(&state, &self.params);
let new_weight = self.weighter.initial_weight(&state);
self.master.set_weight(new_weight)?;
debug!("Init master completed");
Ok(())
}
|
| ︙ | | |
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
|
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
|
-
+
-
+
|
}
}
Ok(())
}
/// Perform a descent step.
fn descent_step(&mut self) -> Result<(), SolverError<P::Err, M::Err>> {
let new_weight = self.weighter.weight(¤t_state!(self, Step::Descent), &self.params);
let new_weight = self.weighter.descent_weight(¤t_state!(self, Step::Descent));
self.master.set_weight(new_weight)?;
self.cnt_descent += 1;
swap(&mut self.cur_y, &mut self.nxt_y);
swap(&mut self.cur_val, &mut self.nxt_val);
swap(&mut self.cur_mod, &mut self.nxt_mod);
swap(&mut self.cur_vals, &mut self.nxt_vals);
swap(&mut self.cur_mods, &mut self.nxt_mods);
self.master.move_center(1.0, &self.nxt_d);
debug!("Descent Step");
debug!(" dir ={}", self.nxt_d);
debug!(" newy={}", self.cur_y);
Ok(())
}
/// Perform a null step.
fn null_step(&mut self) -> Result<(), SolverError<P::Err, M::Err>> {
let new_weight = self.weighter.weight(¤t_state!(self, Step::Null), &self.params);
let new_weight = self.weighter.null_weight(¤t_state!(self, Step::Null));
self.master.set_weight(new_weight)?;
self.cnt_null += 1;
debug!("Null Step");
Ok(())
}
/// Perform one bundle iteration.
|
| ︙ | | |