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
|
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
//! An asynchronous parallel bundle solver.
use crossbeam::channel::{unbounded as channel, Receiver, Sender};
use num_cpus;
use std::sync::Arc;
use threadpool::ThreadPool;
use crate::{DVector, Minorant, Real};
use super::problem::{EvalResult, FirstOrderProblem};
use crate::master::{BoxedMasterProblem, CplexMaster, MasterProblem, UnconstrainedMasterProblem};
/// The default iteration limit.
pub const DEFAULT_ITERATION_LIMIT: usize = 10_000;
type MasterProblemError = <BoxedMasterProblem<CplexMaster> as MasterProblem>::Err;
/// Error raised by the parallel bundle [`Solver`].
#[derive(Debug)]
pub enum Error<E> {
/// An error raised by the master problem process.
Master(MasterProblemError),
/// The iteration limit has been reached.
IterationLimit { limit: usize },
/// An error raised by a subproblem evaluation.
Evaluation(E),
}
impl<E> std::fmt::Display for Error<E>
where
E: std::fmt::Display,
{
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
use Error::*;
match self {
Master(err) => writeln!(fmt, "Error in master problem: {}", err),
IterationLimit { limit } => writeln!(fmt, "The iteration limit has been reached: {}", limit),
Evaluation(err) => writeln!(fmt, "Error in subproblem evaluation: {}", err),
}
}
}
impl<E> std::error::Error for Error<E>
where
E: std::error::Error + 'static,
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use Error::*;
match self {
//Master(err) => Some(err),
Evaluation(err) => Some(err),
_ => None,
}
}
}
/// A task for the master problem.
enum MasterTask {
/// Add a new minorant for a subfunction to the master problem.
AddMinorant(usize, Minorant),
/// Move the center of the master problem in the given direction.
MoveCenter(Real, Arc<DVector>),
/// Start a new computation of the master problem.
Solve { center_value: Real },
}
type MasterSender = Sender<std::result::Result<Real, MasterProblemError>>;
type MasterReceiver = Receiver<MasterTask>;
/// Implementation of a parallel bundle method.
pub struct Solver<P>
where
P: FirstOrderProblem,
{
/// The first order problem.
problem: P,
/// Current center of stability.
cur_y: DVector,
/// Function value in the current point.
cur_val: Real,
/// The threadpool of the solver.
threadpool: ThreadPool,
/// The channel to transmit new tasks to the master problem.
master_tx: Option<Sender<MasterTask>>,
/// The channel to receive solutions from the master problem.
master_rx: Option<Receiver<std::result::Result<Real, MasterProblemError>>>,
/// The channel to receive the evaluation results from subproblems.
client_tx: Option<Sender<std::result::Result<EvalResult<usize, P::Primal>, P::Err>>>,
/// The channel to receive the evaluation results from subproblems.
client_rx: Option<Receiver<std::result::Result<EvalResult<usize, P::Primal>, P::Err>>>,
/// Number of descent steps.
cnt_descent: usize,
/// Number of function evaluation.
cnt_evals: usize,
}
impl<P> Solver<P>
where
P: FirstOrderProblem,
P::Err: std::error::Error + Send + Sync + 'static,
{
pub fn new(problem: P) -> Result<Solver<P>, Error<P::Err>> {
Ok(Solver {
problem,
cur_y: dvec![],
cur_val: 0.0,
threadpool: ThreadPool::with_name("Parallel bundle solver".to_string(), num_cpus::get()),
master_tx: None,
master_rx: None,
client_tx: None,
client_rx: None,
cnt_descent: 0,
cnt_evals: 0,
})
}
/// Return the underlying threadpool.
///
/// In order to use the same threadpool for concurrent processes,
/// just clone the returned `ThreadPool`.
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
//! An asynchronous parallel bundle solver.
use crossbeam::channel::{select, unbounded as channel, Receiver, Sender};
use log::{debug, info, warn};
use num_cpus;
use num_traits::Float;
use std::sync::Arc;
use std::time::Instant;
use threadpool::ThreadPool;
use crate::{DVector, Minorant, Real};
use super::problem::{EvalResult, FirstOrderProblem};
use crate::master::{BoxedMasterProblem, CplexMaster, MasterProblem, UnconstrainedMasterProblem};
use crate::solver::{BundleState, SolverParams, StandardTerminator, Step, Terminator, Weighter};
use crate::HKWeighter;
/// The default iteration limit.
pub const DEFAULT_ITERATION_LIMIT: usize = 10_000;
type MasterProblemError = <BoxedMasterProblem<CplexMaster> as MasterProblem>::Err;
/// Error raised by the parallel bundle [`Solver`].
#[derive(Debug)]
pub enum Error<E> {
/// An error raised by the master problem process.
Master(MasterProblemError),
/// The iteration limit has been reached.
IterationLimit { limit: usize },
/// An error raised by a subproblem evaluation.
Evaluation(E),
/// The dimension of some data is wrong.
Dimension(String),
/// An error occurred in a subprocess.
Process(Box<dyn std::error::Error>),
/// A method requiring an initialized solver has been called.
NotInitialized,
}
impl<E> std::fmt::Display for Error<E>
where
E: std::fmt::Display,
{
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
use Error::*;
match self {
Master(err) => writeln!(fmt, "Error in master problem: {}", err),
IterationLimit { limit } => writeln!(fmt, "The iteration limit has been reached: {}", limit),
Evaluation(err) => writeln!(fmt, "Error in subproblem evaluation: {}", err),
Dimension(what) => writeln!(fmt, "Wrong dimension for {}", what),
Process(err) => writeln!(fmt, "Error in subprocess: {}", err),
NotInitialized => writeln!(fmt, "The solver must be initialized (called Solver::init()?)"),
}
}
}
impl<E> std::error::Error for Error<E>
where
E: std::error::Error + 'static,
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use Error::*;
match self {
Master(err) => Some(err),
Evaluation(err) => Some(err),
Process(err) => Some(err.as_ref()),
_ => None,
}
}
}
/// Information about a minorant.
#[derive(Debug, Clone)]
struct MinorantInfo<Pr> {
/// The minorant's index in the master problem
index: usize,
/// Current multiplier.
multiplier: Real,
/// Primal associated with this minorant.
primal: Option<Pr>,
}
/// Configuration information for setting up a master problem.
struct MasterConfig {
/// The number of subproblems.
num_subproblems: usize,
/// The number of variables.
num_vars: usize,
/// The lower bounds on the variables.
lower_bounds: Option<DVector>,
/// The lower bounds on the variables.
upper_bounds: Option<DVector>,
/// The maximal number of inner updates.
max_updates: usize,
}
/// A task for the master problem.
enum MasterTask<Pr> {
/// Add a new minorant for a subfunction to the master problem.
AddMinorant(usize, Minorant, Pr),
/// Move the center of the master problem in the given direction.
MoveCenter(Real, Arc<DVector>),
/// Start a new computation of the master problem.
Solve { center_value: Real },
/// Compress the bundle.
Compress,
/// Set the weight parameter of the master problem.
SetWeight { weight: Real },
}
/// The response send from a master process.
///
/// The response contains the evaluation results of the latest
struct MasterResponse {
nxt_d: DVector,
nxt_mod: Real,
sgnorm: Real,
}
type MasterSender = Sender<std::result::Result<MasterResponse, MasterProblemError>>;
type MasterReceiver<Pr> = Receiver<MasterTask<Pr>>;
/// Parameters for tuning the solver.
pub type Parameters = SolverParams;
/// Implementation of a parallel bundle method.
pub struct Solver<P>
where
P: FirstOrderProblem,
{
/// Parameters for the solver.
pub params: Parameters,
/// Termination predicate.
pub terminator: Box<Terminator>,
/// Weighter heuristic.
pub weighter: Box<Weighter>,
/// The first order problem.
problem: P,
/// Current center of stability.
cur_y: DVector,
/// Function value in the current point.
cur_val: Real,
/// Model value at the current candidate.
nxt_mod: Real,
/// The currently used master problem weight.
cur_weight: Real,
/// The threadpool of the solver.
threadpool: ThreadPool,
/// The channel to transmit new tasks to the master problem.
master_tx: Option<Sender<MasterTask<P::Primal>>>,
/// The channel to receive solutions from the master problem.
master_rx: Option<Receiver<std::result::Result<MasterResponse, MasterProblemError>>>,
/// The channel to receive the evaluation results from subproblems.
client_tx: Option<Sender<std::result::Result<EvalResult<usize, P::Primal>, P::Err>>>,
/// The channel to receive the evaluation results from subproblems.
client_rx: Option<Receiver<std::result::Result<EvalResult<usize, P::Primal>, P::Err>>>,
/// Number of descent steps.
cnt_descent: usize,
/// Number of null steps.
cnt_null: usize,
/// Number of function evaluation.
cnt_evals: usize,
/// Time when the solution process started.
///
/// This is actually the time of the last call to `Solver::init`.
start_time: Instant,
}
impl<P> Solver<P>
where
P: FirstOrderProblem,
P::Primal: Send + Sync + 'static,
P::Err: std::error::Error + Send + Sync + 'static,
{
pub fn new(problem: P) -> Result<Solver<P>, Error<P::Err>> {
Ok(Solver {
params: Parameters::default(),
terminator: Box::new(StandardTerminator {
termination_precision: 1e-3,
}),
weighter: Box::new(HKWeighter::new()),
problem,
cur_y: dvec![],
cur_val: 0.0,
nxt_mod: 0.0,
cur_weight: 1.0,
threadpool: ThreadPool::with_name("Parallel bundle solver".to_string(), num_cpus::get()),
master_tx: None,
master_rx: None,
client_tx: None,
client_rx: None,
cnt_descent: 0,
cnt_null: 0,
cnt_evals: 0,
start_time: Instant::now(),
})
}
/// Return the underlying threadpool.
///
/// In order to use the same threadpool for concurrent processes,
/// just clone the returned `ThreadPool`.
|
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
|
/// This will reset the internal data structures so that a new fresh
/// solution process can be started.
///
/// It will also setup all worker processes.
///
/// This function is automatically called by [`solve`].
pub fn init(&mut self) -> Result<(), Error<P::Err>> {
let n = self.problem.num_variables();
let m = self.problem.num_subproblems();
self.cur_y.init0(n);
self.cnt_descent = 0;
self.cnt_evals = 0;
let (tx, rx) = channel();
self.client_tx = Some(tx);
self.client_rx = Some(rx);
let (tx, rx) = channel();
let (rev_tx, rev_rx) = channel();
self.master_tx = Some(tx);
self.master_rx = Some(rev_rx);
self.threadpool.execute(move || {
let mut rev_tx = rev_tx;
if let Err(err) = Self::master_main(&mut rev_tx, rx) {
#[allow(unused_must_use)]
{
rev_tx.send(Err(err));
}
}
});
// We need an initial evaluation of all oracles for the first center.
let y = Arc::new(self.cur_y.clone());
for i in 0..m {
self.problem
.evaluate(i, y.clone(), i, self.client_tx.clone().unwrap())
.map_err(Error::Evaluation)?;
}
let mut center_values: Vec<Option<Real>> = vec![None; m];
let mut minorants: Vec<Option<Minorant>> = vec![None; m];
let mut cnt_remaining_objs = m;
let mut cnt_remaining_mins = m;
for m in self.client_rx.as_ref().unwrap() {
match m {
Ok(EvalResult::ObjectiveValue { index: i, value }) => {
println!("Receive objective {}", i);
if center_values[i].is_none() {
cnt_remaining_objs -= 1;
center_values[i] = Some(value);
}
}
Ok(EvalResult::Minorant { index: i, minorant, .. }) => {
println!("Receive minorant {}", i);
if let Some(ref mut min) = minorants[i] {
if min.constant < minorant.constant {
*min = minorant;
}
} else {
cnt_remaining_mins -= 1;
minorants[i] = Some(minorant);
}
}
Err(err) => return Err(Error::Evaluation(err)),
};
if cnt_remaining_mins == 0 && cnt_remaining_objs == 0 {
break;
}
}
panic!("Initial evaluation completed");
Ok(())
}
fn master_main(tx: &mut MasterSender, rx: MasterReceiver) -> std::result::Result<(), MasterProblemError> {
let mut master = CplexMaster::new().map(BoxedMasterProblem::with_master)?;
for m in rx {
match m {
MasterTask::AddMinorant(i, m) => {
master.add_minorant(i, m)?;
}
MasterTask::MoveCenter(alpha, d) => {
master.move_center(alpha, &d);
}
MasterTask::Solve { center_value } => {
master.solve(center_value)?;
}
};
}
Ok(())
}
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
<
>
|
|
>
>
>
>
|
>
|
>
|
|
<
<
<
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
/// This will reset the internal data structures so that a new fresh
/// solution process can be started.
///
/// It will also setup all worker processes.
///
/// This function is automatically called by [`solve`].
pub fn init(&mut self) -> Result<(), Error<P::Err>> {
debug!("Initialize solver");
let n = self.problem.num_variables();
let m = self.problem.num_subproblems();
self.cur_y.init0(n);
self.cnt_descent = 0;
self.cnt_null = 0;
self.cnt_evals = 0;
let (tx, rx) = channel();
self.client_tx = Some(tx);
self.client_rx = Some(rx);
let (tx, rx) = channel();
let (rev_tx, rev_rx) = channel();
self.master_tx = Some(tx);
self.master_rx = Some(rev_rx);
let master_config = MasterConfig {
num_subproblems: m,
num_vars: n,
lower_bounds: self.problem.lower_bounds().map(DVector),
upper_bounds: self.problem.upper_bounds().map(DVector),
max_updates: self.params.max_updates,
};
if master_config
.lower_bounds
.as_ref()
.map(|lb| lb.len() != n)
.unwrap_or(false)
{
return Err(Error::Dimension("lower bounds".to_string()));
}
if master_config
.upper_bounds
.as_ref()
.map(|ub| ub.len() != n)
.unwrap_or(false)
{
return Err(Error::Dimension("upper bounds".to_string()));
}
debug!("Start master process");
self.threadpool.execute(move || {
debug!("Master process started");
let mut rev_tx = rev_tx;
if let Err(err) = Self::master_main(master_config, &mut rev_tx, rx) {
#[allow(unused_must_use)]
{
rev_tx.send(Err(err));
}
}
debug!("Master proces stopped");
});
debug!("Initial problem evaluation");
// We need an initial evaluation of all oracles for the first center.
let y = Arc::new(self.cur_y.clone());
for i in 0..m {
self.problem
.evaluate(i, y.clone(), i, self.client_tx.clone().unwrap())
.map_err(Error::Evaluation)?;
}
let mut have_minorants = vec![false; m];
let mut center_values: Vec<Option<Real>> = vec![None; m];
let mut cnt_remaining_objs = m;
let mut cnt_remaining_mins = m;
let master_tx = self.master_tx.as_ref().unwrap();
for m in self.client_rx.as_ref().unwrap() {
match m {
Ok(EvalResult::ObjectiveValue { index: i, value }) => {
debug!("Receive objective from subproblem {}: {}", i, value);
if center_values[i].is_none() {
cnt_remaining_objs -= 1;
center_values[i] = Some(value);
}
}
Ok(EvalResult::Minorant {
index: i,
minorant,
primal,
}) => {
debug!("Receive minorant from subproblem {}", i);
master_tx
.send(MasterTask::AddMinorant(i, minorant, primal))
.map_err(|err| Error::Process(err.into()))?;
if !have_minorants[i] {
have_minorants[i] = true;
cnt_remaining_mins -= 1;
}
}
Err(err) => return Err(Error::Evaluation(err)),
};
if cnt_remaining_mins == 0 && cnt_remaining_objs == 0 {
break;
}
}
self.cur_weight = Real::infinity(); // gets initialized when the master problem is complete
master_tx
.send(MasterTask::SetWeight { weight: 1.0 })
.map_err(|err| Error::Process(err.into()))?;
master_tx
.send(MasterTask::Solve {
center_value: self.cur_val,
})
.map_err(|err| Error::Process(err.into()))?;
debug!("Initialization complete");
self.start_time = Instant::now();
Ok(())
}
fn master_main(
master_config: MasterConfig,
tx: &mut MasterSender,
rx: MasterReceiver<P::Primal>,
) -> std::result::Result<(), MasterProblemError> {
let mut master = CplexMaster::new().map(BoxedMasterProblem::with_master)?;
let mut minorants: Vec<MinorantInfo<P::Primal>> = vec![];
// Initialize the master problem.
master.set_num_subproblems(master_config.num_subproblems)?;
master.set_vars(
master_config.num_vars,
master_config.lower_bounds,
master_config.upper_bounds,
)?;
master.set_max_updates(master_config.max_updates)?;
// The main iteration: wait for new tasks.
for m in rx {
match m {
MasterTask::AddMinorant(i, m, primal) => {
debug!("master: add minorant to subproblem {}", i);
let index = master.add_minorant(i, m)?;
minorants.push(MinorantInfo {
index,
multiplier: 0.0,
primal: Some(primal),
});
}
MasterTask::MoveCenter(alpha, d) => {
debug!("master: move center");
master.move_center(alpha, &d);
}
MasterTask::Compress => {
debug!("Compress bundle");
warn!("Bundle compression not yet implemented");
}
MasterTask::Solve { center_value } => {
debug!("master: solve with center_value {}", center_value);
master.solve(center_value)?;
let master_response = MasterResponse {
nxt_d: master.get_primopt(),
nxt_mod: master.get_primoptval(),
sgnorm: master.get_dualoptnorm2().sqrt(),
};
if let Err(err) = tx.send(Ok(master_response)) {
warn!("Master process cancelled because of channel error: {}", err);
break;
}
}
MasterTask::SetWeight { weight } => {
debug!("master: set weight {}", weight);
master.set_weight(weight)?;
}
};
}
Ok(())
}
|
285
286
287
288
289
290
291
292
293
294
|
/// has been satisfied. Otherwise it returns `Ok(false)` or an
/// error code.
///
/// If this function is called again, the solution process is
/// continued from the previous point. Because of this one *must*
/// call `init()` before the first call to this function.
pub fn solve_iter(&mut self, niter: usize) -> Result<bool, Error<P::Err>> {
unimplemented!()
}
}
|
>
>
>
>
>
|
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
|
/// has been satisfied. Otherwise it returns `Ok(false)` or an
/// error code.
///
/// If this function is called again, the solution process is
/// continued from the previous point. Because of this one *must*
/// call `init()` before the first call to this function.
pub fn solve_iter(&mut self, niter: usize) -> Result<bool, Error<P::Err>> {
debug!("Start solving up to {} iterations", niter);
let client_tx = self.client_tx.as_ref().ok_or(Error::NotInitialized)?;
let client_rx = self.client_rx.as_ref().ok_or(Error::NotInitialized)?;
let master_tx = self.master_tx.as_ref().ok_or(Error::NotInitialized)?;
let master_rx = self.master_rx.as_ref().ok_or(Error::NotInitialized)?;
let mut cnt_iter = 0;
let mut nxt_ubs = vec![Real::infinity(); self.problem.num_subproblems()];
let mut cnt_remaining_ubs = self.problem.num_subproblems();
let mut nxt_d = Arc::new(dvec![]);
let mut nxt_y = Arc::new(dvec![]);
let mut sgnorm = 0.0;
let mut expected_progress = 0.0;
loop {
select! {
recv(client_rx) -> msg => {
let msg = msg
.map_err(|err| Error::Process(err.into()))?
.map_err(Error::Evaluation)?;
match msg {
EvalResult::ObjectiveValue { index, value } => {
debug!("Receive objective from subproblem {}: {}", index, value);
if nxt_ubs[index] == Real::infinity() {
cnt_remaining_ubs -= 1;
}
nxt_ubs[index] = nxt_ubs[index].min(value);
}
EvalResult::Minorant { index, mut minorant, primal } => {
debug!("Receive minorant from subproblem {}", index);
// move center of minorant to cur_y
minorant.move_center(-1.0, &nxt_d);
// add minorant to master problem
master_tx
.send(MasterTask::AddMinorant(index, minorant, primal))
.map_err(|err| Error::Process(err.into()))?;
}
}
if cnt_remaining_ubs == 0 {
// All subproblems have been evaluated, do a step.
let nxt_ub = nxt_ubs.iter().sum::<Real>();
let descent_bnd = self.get_descent_bound();
debug!("Step");
debug!(" cur_val ={}", self.cur_val);
debug!(" nxt_mod ={}", self.nxt_mod);
debug!(" nxt_ub ={}", nxt_ub);
debug!(" descent_bnd={}", descent_bnd);
let step;
if nxt_ub <= descent_bnd {
step = Step::Descent;
self.cnt_descent += 1;
self.cur_y = nxt_y.as_ref().clone();
self.cur_val = nxt_ub;
master_tx
.send(MasterTask::MoveCenter(1.0, nxt_d.clone()))
.map_err(|err| Error::Process(err.into()))?;
debug!("Descent Step");
debug!(" dir ={}", nxt_d);
debug!(" newy={}", self.cur_y);
} else {
step = Step::Null;
self.cnt_null += 1;
}
// Update the weight
let weight = self.weighter.weight(&BundleState {
cur_y: &self.cur_y,
cur_val: self.cur_val,
nxt_y: &nxt_y,
nxt_mod: self.nxt_mod,
nxt_val: nxt_ub,
new_cutval: nxt_ub,
sgnorm: sgnorm,
weight: self.cur_weight,
step,
expected_progress,
}, &self.params);
self.cur_weight = weight;
master_tx
.send(MasterTask::SetWeight { weight })
.map_err(|err| Error::Process(err.into()))?;
self.show_info(step, expected_progress, self.nxt_mod, nxt_ub, self.cur_val);
cnt_iter += 1;
if cnt_iter >= niter { break }
master_tx
.send(MasterTask::Solve { center_value: self.cur_val })
.map_err(|err| Error::Process(err.into()))?;
}
},
recv(master_rx) -> msg => {
debug!("Receive master response");
// Receive result (new candidate) from the master
let master_res = msg
.map_err(|err| Error::Process(err.into()))?
.map_err(Error::Master)?;
if self.cur_weight < Real::infinity() && self.terminator.terminate(&BundleState {
cur_y: &self.cur_y,
cur_val: self.cur_val,
nxt_y: &nxt_y,
nxt_mod: self.nxt_mod,
nxt_val: self.cur_val, // hopefully does not matter
new_cutval: self.cur_val, // hopefully does not matter
sgnorm: sgnorm,
weight: self.cur_weight,
step: Step::Term,
expected_progress,
}, &self.params) {
info!("Termination criterion satisfied");
return Ok(true)
}
// Compress bundle
master_tx.send(MasterTask::Compress).map_err(|err| Error::Process(err.into()))?;
// Compute new candidate.
self.nxt_mod = master_res.nxt_mod;
expected_progress = self.cur_val - self.nxt_mod;
sgnorm = master_res.sgnorm;
let mut next_y = dvec![];
nxt_d = Arc::new(master_res.nxt_d);
next_y.add(&self.cur_y, &nxt_d);
nxt_y = Arc::new(next_y);
// Reset evaluation data.
nxt_ubs.clear();
nxt_ubs.resize(self.problem.num_subproblems(), Real::infinity());
cnt_remaining_ubs = self.problem.num_subproblems();
// Start evaluation of all subproblems at the new candidate.
for i in 0..self.problem.num_subproblems() {
self.problem.evaluate(i, nxt_y.clone(), i, client_tx.clone()) .map_err(Error::Evaluation)?;
}
// Compute the real initial weight.
if self.cur_weight == Real::infinity() {
let state = BundleState {
cur_y: &self.cur_y,
cur_val: self.cur_val,
nxt_y: &nxt_y,
nxt_mod: self.nxt_mod,
nxt_val: 42.0, // does not matter
new_cutval: 42.0, // does not matter
sgnorm: sgnorm,
weight: 1.0, // does not matter
step: Step::Term,
expected_progress,
};
let weight = self.weighter.weight(&state, &self.params);
self.cur_weight = weight;
master_tx
.send(MasterTask::SetWeight { weight })
.map_err(|err| Error::Process(err.into()))?;
}
},
}
}
Ok(false)
}
/// Return the bound the function value must be below of to enforce a descent step.
///
/// If the oracle guarantees that $f(\bar{y}) \le$ this bound, the
/// bundle method will perform a descent step.
///
/// This value is $f(\hat{y}) + \varrho \cdot \Delta$ where
/// $\Delta = f(\hat{y}) - \hat{f}(\bar{y})$ is the expected
/// progress and $\varrho$ is the `acceptance_factor`.
fn get_descent_bound(&self) -> Real {
self.cur_val - self.params.acceptance_factor * (self.cur_val - self.nxt_mod)
}
fn show_info(&self, step: Step, expected_progress: Real, nxt_mod: Real, nxt_val: Real, cur_val: Real) {
let time = self.start_time.elapsed();
info!(
"{} {:0>2}:{:0>2}:{:0>2}.{:0>2} {:4} {:4} {:4}{:1} {:9.4} {:9.4} \
{:12.6e}({:12.6e}) {:12.6e}",
if step == Step::Term { "_endit" } else { "endit " },
time.as_secs() / 3600,
(time.as_secs() / 60) % 60,
time.as_secs() % 60,
time.subsec_nanos() / 10_000_000,
self.cnt_descent,
self.cnt_descent + self.cnt_null,
0, /*self.master.cnt_updates(),*/
if step == Step::Descent { "*" } else { " " },
self.cur_weight,
expected_progress,
nxt_mod,
nxt_val,
cur_val
);
}
}
|