Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| Comment: | Merge release |
|---|---|
| Downloads: | Tarball | ZIP archive |
| Timelines: | family | ancestors | modifyprimals |
| Files: | files | file ages | folders |
| SHA1: |
51732172c2b7f261f97dd886f69ab0d6 |
| User & Date: | fifr 2019-12-21 22:48:01.177 |
Context
|
2019-12-21
| ||
| 22:48 | Merge release Leaf check-in: 51732172c2 user: fifr tags: modifyprimals | |
| 21:28 | Add `dyn` to trait object types check-in: 45d9ecf62b user: fifr tags: modifyprimals | |
| 21:07 | Update version to 0.6.3 check-in: e4a7214ded user: fifr tags: release, v0.6.3 | |
Changes
Changes to .fossil-settings/ignore-glob.
1 2 3 4 | target/ *.log *.cpxlog Cargo.lock | > | 1 2 3 4 5 | target/ *.log *.cpxlog Cargo.lock instances/ |
Changes to Cargo.toml.
1 2 | [package] name = "bundle" | | | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [package] name = "bundle" version = "0.7.0-dev" edition = "2018" authors = ["Frank Fischer <frank-fischer@shadow-soft.de>"] [dependencies] itertools = "^0.8" libc = "^0.2.6" log = "^0.4" c_str_macro = "^1.0" cplex-sys = "^0.6" [dev-dependencies] env_logger = "^0.7" |
Changes to src/firstorderproblem.rs.
| ︙ | ︙ | |||
189 190 191 192 193 194 195 |
_state: &UpdateState<Self::Primal>,
) -> Result<Vec<Update<Self::Primal, Self::Err>>, Self::Err> {
Ok(vec![])
}
/// Return new components for a subgradient.
///
| | | | > > > > > | 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
_state: &UpdateState<Self::Primal>,
) -> Result<Vec<Update<Self::Primal, Self::Err>>, Self::Err> {
Ok(vec![])
}
/// Return new components for a subgradient.
///
/// The components are typically generated by some primal information. The
/// corresponding primal along with its subproblem index is passed as a
/// parameter.
///
/// The default implementation fails because it should never be
/// called.
fn extend_subgradient(
&mut self,
_i: usize,
_primal: &Self::Primal,
_vars: &[usize],
) -> Result<Vec<Real>, Self::Err> {
unimplemented!()
}
}
|
Changes to src/master/base.rs.
|
| | | 1 2 3 4 5 6 7 8 | // Copyright (c) 2016, 2017, 2019 Frank Fischer <frank-fischer@shadow-soft.de> // // This program is free software: you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but |
| ︙ | ︙ | |||
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
}
}
}
/// Result type of master problems.
pub type Result<T> = result::Result<T, MasterProblemError>;
pub trait MasterProblem {
/// Unique index for a minorant.
type MinorantIndex: Copy + Eq;
/// Set the number of subproblems.
fn set_num_subproblems(&mut self, n: usize) -> Result<()>;
| > > > | 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
}
}
}
/// Result type of master problems.
pub type Result<T> = result::Result<T, MasterProblemError>;
/// Callback for subgradient extensions.
pub type SubgradientExtension<'a, I> = dyn FnMut(usize, I, &[usize]) -> result::Result<DVector, Box<dyn Error>> + 'a;
pub trait MasterProblem {
/// Unique index for a minorant.
type MinorantIndex: Copy + Eq;
/// Set the number of subproblems.
fn set_num_subproblems(&mut self, n: usize) -> Result<()>;
|
| ︙ | ︙ | |||
79 80 81 82 83 84 85 |
/// Set the maximal number of inner iterations.
fn set_max_updates(&mut self, max_updates: usize) -> Result<()>;
/// Return the current number of inner iterations.
fn cnt_updates(&self) -> usize;
| | | < < < < | 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
/// Set the maximal number of inner iterations.
fn set_max_updates(&mut self, max_updates: usize) -> Result<()>;
/// Return the current number of inner iterations.
fn cnt_updates(&self) -> usize;
/// Add or move some variables with bounds.
///
/// If an index is specified, existing variables are moved,
/// otherwise new variables are generated.
fn add_vars(
&mut self,
bounds: &[(Option<usize>, Real, Real)],
extend_subgradient: &mut SubgradientExtension<Self::MinorantIndex>,
) -> Result<()>;
/// Add a new minorant to the model.
///
/// The function returns a unique (among all minorants of all
/// subproblems) index of the minorant. This index must remain
/// valid until the minorant is aggregated.
|
| ︙ | ︙ |
Changes to src/master/boxed.rs.
| ︙ | ︙ | |||
10 11 12 13 14 15 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/> // | | | < < | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
//
use crate::master::UnconstrainedMasterProblem;
use crate::master::{MasterProblem, SubgradientExtension};
use crate::{DVector, Minorant, Real};
use super::Result;
use itertools::multizip;
use log::debug;
use std::f64::{EPSILON, INFINITY, NEG_INFINITY};
/**
* Turn unconstrained master problem into box-constrained one.
*
* This master problem adds box constraints to an unconstrainted
* master problem implementation. The box constraints are enforced by
* an additional outer optimization loop.
|
| ︙ | ︙ | |||
170 171 172 173 174 175 176 |
/**
* Return $\\|G \alpha - \eta\\|_2\^2$.
*
* This is the norm-square of the dual optimal solution including
* the current box-multipliers $\eta$.
*/
fn get_norm_subg2(&self) -> Real {
| | < | 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
/**
* Return $\\|G \alpha - \eta\\|_2\^2$.
*
* This is the norm-square of the dual optimal solution including
* the current box-multipliers $\eta$.
*/
fn get_norm_subg2(&self) -> Real {
self.eta.dot(self.master.dualopt())
}
}
impl<M: UnconstrainedMasterProblem> MasterProblem for BoxedMasterProblem<M> {
type MinorantIndex = M::MinorantIndex;
fn set_num_subproblems(&mut self, n: usize) -> Result<()> {
|
| ︙ | ︙ | |||
209 210 211 212 213 214 215 |
fn set_weight(&mut self, weight: Real) -> Result<()> {
self.master.set_weight(weight)
}
fn add_vars(
&mut self,
bounds: &[(Option<usize>, Real, Real)],
| | < < < < | | 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 |
fn set_weight(&mut self, weight: Real) -> Result<()> {
self.master.set_weight(weight)
}
fn add_vars(
&mut self,
bounds: &[(Option<usize>, Real, Real)],
extend_subgradient: &mut SubgradientExtension<Self::MinorantIndex>,
) -> Result<()> {
if !bounds.is_empty() {
for (index, l, u) in bounds.iter().filter_map(|v| v.0.map(|i| (i, v.1, v.2))) {
self.lb[index] = l;
self.ub[index] = u;
}
self.lb.extend(bounds.iter().filter(|v| v.0.is_none()).map(|x| x.1));
self.ub.extend(bounds.iter().filter(|v| v.0.is_none()).map(|x| x.2));
self.eta.resize(self.lb.len(), 0.0);
self.need_new_candidate = true;
let nnew = bounds.iter().filter(|v| v.0.is_none()).count();
let changed = bounds.iter().filter_map(|v| v.0).collect::<Vec<_>>();
self.master.add_vars(nnew, &changed, extend_subgradient)
} else {
Ok(())
}
}
#[allow(clippy::cognitive_complexity)]
fn solve(&mut self, center_value: Real) -> Result<()> {
debug!("Solve Master");
debug!(" lb ={}", self.lb);
debug!(" ub ={}", self.ub);
if self.need_new_candidate {
self.compute_candidate();
|
| ︙ | ︙ |
Changes to src/master/cpx.rs.
| ︙ | ︙ | |||
14 15 16 17 18 19 20 | // along with this program. If not, see <http://www.gnu.org/licenses/> // //! Master problem implementation using CPLEX. #![allow(unused_unsafe)] | | | < > < | 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 |
// along with this program. If not, see <http://www.gnu.org/licenses/>
//
//! Master problem implementation using CPLEX.
#![allow(unused_unsafe)]
use crate::master::{Error as MasterProblemError, SubgradientExtension, UnconstrainedMasterProblem};
use crate::{DVector, Minorant, Real};
use super::Result;
use c_str_macro::c_str;
use cplex_sys as cpx;
use cplex_sys::trycpx;
use log::{debug, warn};
use std;
use std::f64::{self, NEG_INFINITY};
use std::iter::{once, repeat};
use std::os::raw::{c_char, c_int};
use std::ptr;
impl From<cpx::CplexError> for MasterProblemError {
fn from(err: cpx::CplexError) -> MasterProblemError {
MasterProblemError::Solver(err.into())
}
}
|
| ︙ | ︙ | |||
157 158 159 160 161 162 163 |
}
}
fn add_vars(
&mut self,
nnew: usize,
changed: &[usize],
| | < < < < | | | 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 |
}
}
fn add_vars(
&mut self,
nnew: usize,
changed: &[usize],
extend_subgradient: &mut SubgradientExtension<Self::MinorantIndex>,
) -> Result<()> {
debug_assert!(!self.minorants[0].is_empty());
let noldvars = self.minorants[0][0].linear.len();
let nnewvars = noldvars + nnew;
let mut changedvars = vec![];
changedvars.extend_from_slice(changed);
changedvars.extend(noldvars..nnewvars);
for (fidx, mins) in self.minorants.iter_mut().enumerate() {
if !mins.is_empty() {
for (i, m) in mins.iter_mut().enumerate() {
let new_subg = extend_subgradient(fidx, self.min2index[fidx][i], &changedvars)
.map_err(MasterProblemError::SubgradientExtension)?;
for (&j, &g) in changed.iter().zip(new_subg.iter()) {
m.linear[j] = g;
}
m.linear.extend_from_slice(&new_subg[changed.len()..]);
}
}
}
|
| ︙ | ︙ | |||
238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
nvars as c_int,
inds.as_ptr(),
c.as_ptr()
));
}
trycpx!(cpx::qpopt(cpx::env(), self.lp));
let mut sol = vec![0.0; nvars];
trycpx!(cpx::getx(cpx::env(), self.lp, sol.as_mut_ptr(), 0, nvars as c_int - 1));
let mut idx = 0;
let mut mults = Vec::with_capacity(nvars);
let mut mins = Vec::with_capacity(nvars);
for fidx in 0..self.minorants.len() {
| > > > > | 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
nvars as c_int,
inds.as_ptr(),
c.as_ptr()
));
}
trycpx!(cpx::qpopt(cpx::env(), self.lp));
let solstat = unsafe { cpx::getstat(cpx::env(), self.lp) };
if solstat != cpx::Stat::Optimal.to_c() && solstat != cpx::Stat::NumBest.to_c() {
warn!("Problem solving the master QP with Cplex (status: {:?})", solstat)
}
let mut sol = vec![0.0; nvars];
trycpx!(cpx::getx(cpx::env(), self.lp, sol.as_mut_ptr(), 0, nvars as c_int - 1));
let mut idx = 0;
let mut mults = Vec::with_capacity(nvars);
let mut mins = Vec::with_capacity(nvars);
for fidx in 0..self.minorants.len() {
|
| ︙ | ︙ | |||
300 301 302 303 304 305 306 |
sum_coeffs += self.opt_mults[fidx][self.index2min[i].1];
}
let aggr_coeffs = if sum_coeffs != 0.0 {
mins.iter()
.map(|&i| self.opt_mults[fidx][self.index2min[i].1] / sum_coeffs)
.collect::<DVector>()
} else {
| > | | 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
sum_coeffs += self.opt_mults[fidx][self.index2min[i].1];
}
let aggr_coeffs = if sum_coeffs != 0.0 {
mins.iter()
.map(|&i| self.opt_mults[fidx][self.index2min[i].1] / sum_coeffs)
.collect::<DVector>()
} else {
// All coefficients are zero, we can simply remove all but one of the minorants.
once(1.0).chain(repeat(0.0)).take(mins.len()).collect()
};
// compute aggregated diagonal term
let mut aggr_diag = 0.0;
for (idx_i, &i) in mins.iter().enumerate() {
for (idx_j, &j) in mins.iter().enumerate() {
aggr_diag += aggr_coeffs[idx_i] * aggr_coeffs[idx_j] * self.qterm[i][j];
|
| ︙ | ︙ |
Changes to src/master/minimal.rs.
| ︙ | ︙ | |||
10 11 12 13 14 15 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/> // | | | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
//
use crate::master::{Error as MasterProblemError, SubgradientExtension, UnconstrainedMasterProblem};
use crate::{DVector, Minorant, Real};
use super::Result;
use log::debug;
use std::error::Error;
|
| ︙ | ︙ | |||
124 125 126 127 128 129 130 |
Ok(self.minorants.len() - 1)
}
fn add_vars(
&mut self,
nnew: usize,
changed: &[usize],
| | < < < < | 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
Ok(self.minorants.len() - 1)
}
fn add_vars(
&mut self,
nnew: usize,
changed: &[usize],
extend_subgradient: &mut SubgradientExtension<Self::MinorantIndex>,
) -> Result<()> {
if !self.minorants.is_empty() {
let noldvars = self.minorants[0].linear.len();
let mut changedvars = vec![];
changedvars.extend_from_slice(changed);
changedvars.extend(noldvars..noldvars + nnew);
for (i, m) in self.minorants.iter_mut().enumerate() {
|
| ︙ | ︙ |
Changes to src/master/mod.rs.
|
| | < | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Copyright (c) 2016, 2017, 2019 Frank Fischer <frank-fischer@shadow-soft.de>
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
//
//! Bundle master problem solver.
//!
//! This module contains solvers for the bundle master problem, i.e.
//! for solving convex optimization problems of the form
//!
//! \\[ \min \left\\{ \hat{f}(d) + \frac{w}{2} \\|d\\|\^2 \colon d \in [l,u] \right\\}, \\]
//!
|
| ︙ | ︙ | |||
34 35 36 37 38 39 40 |
//! * changing the weight parameter $w$,
//! * modifying $\hat{f}$ by adding or removing linear functions $\ell_i$,
//! * moving the center of the linear functions $\ell_i$ (and the
//! bounds), i.e. replacing $\hat{f}$ by $d \mapsto \hat{f}(d -
//! \hat{d})$ for some given $\hat{d} \in \mathbb{R}\^n$.
mod base;
| | | 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
//! * changing the weight parameter $w$,
//! * modifying $\hat{f}$ by adding or removing linear functions $\ell_i$,
//! * moving the center of the linear functions $\ell_i$ (and the
//! bounds), i.e. replacing $\hat{f}$ by $d \mapsto \hat{f}(d -
//! \hat{d})$ for some given $\hat{d} \in \mathbb{R}\^n$.
mod base;
pub use self::base::{MasterProblem, MasterProblemError as Error, Result, SubgradientExtension};
mod boxed;
pub use self::boxed::BoxedMasterProblem;
mod unconstrained;
pub use self::unconstrained::UnconstrainedMasterProblem;
|
| ︙ | ︙ |
Changes to src/master/unconstrained.rs.
| ︙ | ︙ | |||
12 13 14 15 16 17 18 |
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
//
use crate::{DVector, Minorant, Real};
| | < < < | 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
//
use crate::{DVector, Minorant, Real};
use super::{Result, SubgradientExtension};
/**
* Trait for master problems without box constraints.
*
* Implementors of this trait are supposed to solve quadratic
* optimization problems of the form
*
|
| ︙ | ︙ | |||
77 78 79 80 81 82 83 |
/// The variables in `changed` have been changed, so the subgradient
/// information must be updated. Furthermore, `nnew` new variables
/// are added.
fn add_vars(
&mut self,
nnew: usize,
changed: &[usize],
| | < < < < | 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
/// The variables in `changed` have been changed, so the subgradient
/// information must be updated. Furthermore, `nnew` new variables
/// are added.
fn add_vars(
&mut self,
nnew: usize,
changed: &[usize],
extend_subgradient: &mut SubgradientExtension<Self::MinorantIndex>,
) -> Result<()>;
/// Solve the master problem.
fn solve(&mut self, eta: &DVector, fbound: Real, augbound: Real, relprec: Real) -> Result<()>;
/// Return the current dual optimal solution.
fn dualopt(&self) -> &DVector;
|
| ︙ | ︙ |
Changes to src/mcf/problem.rs.
|
| | | 1 2 3 4 5 6 7 8 | // Copyright (c) 2016, 2017, 2019 Frank Fischer <frank-fischer@shadow-soft.de> // // This program is free software: you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but |
| ︙ | ︙ |
Changes to src/mcf/solver.rs.
|
| | | 1 2 3 4 5 6 7 8 | // Copyright (c) 2016, 2017, 2018, 2019 Frank Fischer <frank-fischer@shadow-soft.de> // // This program is free software: you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but |
| ︙ | ︙ | |||
46 47 48 49 50 51 52 |
impl Solver {
pub fn new(nnodes: usize) -> Result<Solver> {
let mut status: c_int;
let mut net = ptr::null_mut();
unsafe {
| | | 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
impl Solver {
pub fn new(nnodes: usize) -> Result<Solver> {
let mut status: c_int;
let mut net = ptr::null_mut();
unsafe {
#[allow(clippy::never_loop)]
loop {
status = cpx::setlogfilename(cpx::env(), c_str!("mcf.cpxlog").as_ptr(), c_str!("w").as_ptr());
if status != 0 {
break;
}
net = cpx::NETcreateprob(cpx::env(), &mut status, c_str!("mcf").as_ptr());
|
| ︙ | ︙ |
Changes to src/solver.rs.
|
| | | 1 2 3 4 5 6 7 8 | // Copyright (c) 2016, 2017, 2018, 2019 Frank Fischer <frank-fischer@shadow-soft.de> // // This program is free software: you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but |
| ︙ | ︙ | |||
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
}
impl<E> From<ParameterError> for SolverError<E> {
fn from(err: ParameterError) -> SolverError<E> {
SolverError::Parameter(err)
}
}
/**
* The current state of the bundle method.
*
* Captures the current state of the bundle method during the run of
* the algorithm. This state is passed to certain callbacks like
* Terminator or Weighter so that they can compute their result
| > > > > > > | 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
}
impl<E> From<ParameterError> for SolverError<E> {
fn from(err: ParameterError) -> SolverError<E> {
SolverError::Parameter(err)
}
}
impl<E> From<MasterProblemError> for SolverError<E> {
fn from(err: MasterProblemError) -> SolverError<E> {
SolverError::Master(err)
}
}
/**
* The current state of the bundle method.
*
* Captures the current state of the bundle method during the run of
* the algorithm. This state is passed to certain callbacks like
* Terminator or Weighter so that they can compute their result
|
| ︙ | ︙ | |||
328 329 330 331 332 333 334 |
Descent,
/// No step but the algorithm has been terminated.
Term,
}
/// Information about a minorant.
#[derive(Debug, Clone)]
| | < < | > > | | | 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 |
Descent,
/// No step but the algorithm has been terminated.
Term,
}
/// Information about a minorant.
#[derive(Debug, Clone)]
struct MinorantInfo {
/// The minorant's index in the master problem
index: usize,
/// Current multiplier.
multiplier: Real,
}
/// Information about the last iteration.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum IterationInfo {
NewMinorantTooHigh { new: Real, old: Real },
UpperBoundNullStep,
ShallowCut,
}
/// State information for the update callback.
pub struct UpdateState<'a, Pr: 'a> {
/// Current model minorants.
minorants: &'a [Vec<MinorantInfo>],
/// The primals.
primals: &'a Vec<Option<Pr>>,
/// The last step type.
pub step: Step,
/// Iteration information.
pub iteration_info: &'a [IterationInfo],
/// The current candidate. If the step was a descent step, this is
/// the new center.
pub nxt_y: &'a DVector,
/// The center. IF the step was a descent step, this is the old
/// center.
pub cur_y: &'a DVector,
}
impl<'a, Pr: 'a> UpdateState<'a, Pr> {
pub fn aggregated_primals(&self, subproblem: usize) -> Vec<(Real, &Pr)> {
self.minorants[subproblem]
.iter()
.map(|m| (m.multiplier, self.primals[m.index].as_ref().unwrap()))
.collect()
}
/// Return the last primal for a given subproblem.
///
/// 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| self.primals[m.index].as_ref())
}
}
/**
* Implementation of a bundle method.
*/
pub struct Solver<P: FirstOrderProblem> {
|
| ︙ | ︙ | |||
463 464 465 466 467 468 469 |
*/
start_time: Instant,
/// The master problem.
master: Box<dyn MasterProblem<MinorantIndex = usize>>,
/// The active minorant indices for each subproblem.
| | > > > | 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
*/
start_time: Instant,
/// The master problem.
master: Box<dyn MasterProblem<MinorantIndex = usize>>,
/// The active minorant indices for each subproblem.
minorants: Vec<Vec<MinorantInfo>>,
/// The primals associated with each global minorant index.
primals: Vec<Option<P::Primal>>,
/// Accumulated information about the last iteration.
iterinfos: Vec<IterationInfo>,
}
impl<P: FirstOrderProblem> Solver<P>
where
|
| ︙ | ︙ | |||
508 509 510 511 512 513 514 |
nxt_mods: dvec![],
new_cutval: 0.0,
sgnorm: 0.0,
expected_progress: 0.0,
cnt_descent: 0,
cnt_null: 0,
start_time: Instant::now(),
| | < < > | 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
nxt_mods: dvec![],
new_cutval: 0.0,
sgnorm: 0.0,
expected_progress: 0.0,
cnt_descent: 0,
cnt_null: 0,
start_time: Instant::now(),
master: Box::new(BoxedMasterProblem::new(MinimalMaster::new()?)),
minorants: vec![],
primals: vec![],
iterinfos: vec![],
})
}
/// A new solver with default parameter.
pub fn new(problem: P) -> Result<Solver<P>, SolverError<P::Err>> {
Solver::new_params(problem, SolverParams::default())
|
| ︙ | ︙ | |||
581 582 583 584 585 586 587 |
self.nxt_mods.init0(m);
self.start_time = Instant::now();
Ok(())
}
| | > > > | > > > > > > | | | 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 |
self.nxt_mods.init0(m);
self.start_time = Instant::now();
Ok(())
}
/// Solve the problem with at most 10_000 iterations.
///
/// Use `solve_with_limit` for an explicit iteration limit.
pub fn solve(&mut self) -> Result<(), SolverError<P::Err>> {
const LIMIT: usize = 10_000;
self.solve_with_limit(LIMIT)
}
/// Solve the problem with explicit iteration limit.
pub fn solve_with_limit(&mut self, iter_limit: usize) -> Result<(), SolverError<P::Err>> {
// First initialize the internal data structures.
self.init()?;
if self.solve_iter(iter_limit)? {
Ok(())
} else {
Err(SolverError::IterationLimit { limit: iter_limit })
}
}
/// Solve the problem but stop after `niter` iterations.
///
/// The function returns `Ok(true)` if the termination criterion
/// has been satisfied. Otherwise it returns `Ok(false)` or an
|
| ︙ | ︙ | |||
625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
///
/// Calling this function typically triggers the problem to
/// separate new constraints depending on the current solution.
fn update_problem(&mut self, term: Step) -> Result<bool, SolverError<P::Err>> {
let updates = {
let state = UpdateState {
minorants: &self.minorants,
step: term,
iteration_info: &self.iterinfos,
// this is a dirty trick: when updating the center, we
// simply swapped the `cur_*` fields with the `nxt_*`
// fields
cur_y: if term == Step::Descent {
&self.nxt_y
| > | 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 |
///
/// Calling this function typically triggers the problem to
/// separate new constraints depending on the current solution.
fn update_problem(&mut self, term: Step) -> Result<bool, SolverError<P::Err>> {
let updates = {
let state = UpdateState {
minorants: &self.minorants,
primals: &self.primals,
step: term,
iteration_info: &self.iterinfos,
// this is a dirty trick: when updating the center, we
// simply swapped the `cur_*` fields with the `nxt_*`
// fields
cur_y: if term == Step::Descent {
&self.nxt_y
|
| ︙ | ︙ | |||
692 693 694 695 696 697 698 |
if subproblem >= self.minorants.len() {
return Err(SolverError::InvalidSubproblem {
subproblem: subproblem,
nsubs: self.minorants.len(),
});
}
for m in &mut self.minorants[subproblem] {
| | | | < | | | | | | | | < | 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 |
if subproblem >= self.minorants.len() {
return Err(SolverError::InvalidSubproblem {
subproblem: subproblem,
nsubs: self.minorants.len(),
});
}
for m in &mut self.minorants[subproblem] {
if let Some(ref mut p) = self.primals[m.index] {
if let Err(err) = modify(p) {
return Err(SolverError::Update(err));
}
}
}
}
}
}
if !newvars.is_empty() {
let problem = &mut self.problem;
let primals = &self.primals;
self.master.add_vars(
&newvars.iter().map(|v| (v.0, v.1, v.2)).collect::<Vec<_>>(),
&mut |fidx, minidx, vars| {
problem
.extend_subgradient(fidx, primals[minidx].as_ref().unwrap(), vars)
.map(DVector)
.map_err(|e| e.into())
},
)?;
// modify moved variables
for (index, val) in newvars.iter().filter_map(|v| v.0.map(|i| (i, v.3))) {
self.cur_y[index] = val;
self.nxt_y[index] = val;
self.nxt_d[index] = 0.0;
}
// add new variables
|
| ︙ | ︙ | |||
741 742 743 744 745 746 747 |
/// This function returns all currently used minorants $x_i$ along
/// with their coefficients $\alpha_i$. The aggregated primal can
/// be computed by combining the minorants $\bar{x} =
/// \sum_{i=1}\^m \alpha_i x_i$.
pub fn aggregated_primals(&self, subproblem: usize) -> Vec<(Real, &P::Primal)> {
self.minorants[subproblem]
.iter()
| | | 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 |
/// This function returns all currently used minorants $x_i$ along
/// with their coefficients $\alpha_i$. The aggregated primal can
/// be computed by combining the minorants $\bar{x} =
/// \sum_{i=1}\^m \alpha_i x_i$.
pub fn aggregated_primals(&self, subproblem: usize) -> Vec<(Real, &P::Primal)> {
self.minorants[subproblem]
.iter()
.map(|m| (m.multiplier, self.primals[m.index].as_ref().unwrap()))
.collect()
}
fn show_info(&self, step: Step) {
let time = self.start_time.elapsed();
info!(
"{} {:0>2}:{:0>2}:{:0>2}.{:0>2} {:4} {:4} {:4}{:1} {:9.4} {:9.4} \
|
| ︙ | ︙ | |||
789 790 791 792 793 794 795 |
* information.
*/
fn init_master(&mut self) -> Result<(), SolverError<P::Err>> {
let m = self.problem.num_subproblems();
self.master = if m == 1 && self.params.max_bundle_size == 2 {
debug!("Use minimal master problem");
| | < < | < < | < | < < | < > | < > > > > | | | | | 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 |
* information.
*/
fn init_master(&mut self) -> Result<(), SolverError<P::Err>> {
let m = self.problem.num_subproblems();
self.master = if m == 1 && self.params.max_bundle_size == 2 {
debug!("Use minimal master problem");
Box::new(BoxedMasterProblem::new(MinimalMaster::new()?))
} else {
debug!("Use CPLEX master problem");
Box::new(BoxedMasterProblem::new(CplexMaster::new()?))
};
let lb = self.problem.lower_bounds().map(DVector);
let ub = self.problem.upper_bounds().map(DVector);
if lb
.as_ref()
.map(|lb| lb.len() != self.problem.num_variables())
.unwrap_or(false)
{
return Err(SolverError::Dimension);
}
if ub
.as_ref()
.map(|ub| ub.len() != self.problem.num_variables())
.unwrap_or(false)
{
return Err(SolverError::Dimension);
}
self.master.set_num_subproblems(m)?;
self.master.set_vars(self.problem.num_variables(), lb, ub)?;
self.master.set_max_updates(self.params.max_updates)?;
self.minorants = (0..m).map(|_| vec![]).collect();
self.cur_val = 0.0;
for i in 0..m {
let result = self
.problem
.evaluate(i, &self.cur_y, INFINITY, 0.0)
.map_err(SolverError::Evaluation)?;
self.cur_vals[i] = result.objective();
self.cur_val += self.cur_vals[i];
let mut minorants = result.into_iter();
if let Some((minorant, primal)) = minorants.next() {
self.cur_mods[i] = minorant.constant;
self.cur_mod += self.cur_mods[i];
let minidx = self.master.add_minorant(i, minorant)?;
self.minorants[i].push(MinorantInfo {
index: minidx,
multiplier: 0.0,
});
if minidx >= self.primals.len() {
self.primals.resize_with(minidx + 1, || None);
}
self.primals[minidx] = Some(primal);
} else {
return Err(SolverError::NoMinorant);
}
}
self.cur_valid = true;
// Solve the master problem once to compute the initial
// subgradient.
//
// We could compute that subgradient directly by
// adding up the initial minorants, but this would not include
// the eta terms. However, this is a heuristic anyway because
// we assume an initial weight of 1.0, which, in general, will
// *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);
self.master.set_weight(new_weight)?;
debug!("Init master completed");
Ok(())
}
/// Solve the model (i.e. master problem) to compute the next candidate.
fn solve_model(&mut self) -> Result<(), SolverError<P::Err>> {
self.master.solve(self.cur_val)?;
self.nxt_d = self.master.get_primopt();
self.nxt_y.add(&self.cur_y, &self.nxt_d);
self.nxt_mod = self.master.get_primoptval();
self.sgnorm = self.master.get_dualoptnorm2().sqrt();
self.expected_progress = self.cur_val - self.nxt_mod;
// update multiplier from master solution
|
| ︙ | ︙ | |||
906 907 908 909 910 911 912 |
for i in 0..self.problem.num_subproblems() {
let n = self.master.num_minorants(i);
if n >= self.params.max_bundle_size {
// aggregate minorants with smallest coefficients
self.minorants[i].sort_by_key(|m| -((1e6 * m.multiplier) as isize));
let aggr = self.minorants[i].split_off(self.params.max_bundle_size - 2);
let aggr_sum = aggr.iter().map(|m| m.multiplier).sum();
| | | > > | > | | | < | | | | | 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 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 |
for i in 0..self.problem.num_subproblems() {
let n = self.master.num_minorants(i);
if n >= self.params.max_bundle_size {
// aggregate minorants with smallest coefficients
self.minorants[i].sort_by_key(|m| -((1e6 * m.multiplier) as isize));
let aggr = self.minorants[i].split_off(self.params.max_bundle_size - 2);
let aggr_sum = aggr.iter().map(|m| m.multiplier).sum();
let (aggr_mins, aggr_primals): (Vec<_>, Vec<_>) = aggr
.into_iter()
.map(|m| (m.index, self.primals[m.index].take().unwrap()))
.unzip();
let (aggr_min, aggr_coeffs) = self.master.aggregate(i, &aggr_mins)?;
// append aggregated minorant
self.minorants[i].push(MinorantInfo {
index: aggr_min,
multiplier: aggr_sum,
});
self.primals[aggr_min] = Some(
self.problem
.aggregate_primals(aggr_coeffs.into_iter().zip(aggr_primals.into_iter()).collect()),
);
}
}
Ok(())
}
/// Perform a descent step.
fn descent_step(&mut self) -> Result<(), SolverError<P::Err>> {
let new_weight = self.weighter.weight(¤t_state!(self, Step::Descent), &self.params);
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>> {
let new_weight = self.weighter.weight(¤t_state!(self, Step::Null), &self.params);
self.master.set_weight(new_weight)?;
self.cnt_null += 1;
debug!("Null Step");
Ok(())
}
/// Perform one bundle iteration.
#[allow(clippy::collapsible_if)]
pub fn step(&mut self) -> Result<Step, SolverError<P::Err>> {
self.iterinfos.clear();
if !self.cur_valid {
// current point needs new evaluation
self.init_master()?;
}
|
| ︙ | ︙ | |||
1003 1004 1005 1006 1007 1008 1009 1010 |
nxt_lb += fun_lb;
nxt_ub += fun_ub;
self.nxt_vals[fidx] = fun_ub;
// move center of minorant to cur_y
nxt_minorant.move_center(-1.0, &self.nxt_d);
self.new_cutval += nxt_minorant.constant;
self.minorants[fidx].push(MinorantInfo {
| > | < < < < > > > > | 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 |
nxt_lb += fun_lb;
nxt_ub += fun_ub;
self.nxt_vals[fidx] = fun_ub;
// move center of minorant to cur_y
nxt_minorant.move_center(-1.0, &self.nxt_d);
self.new_cutval += nxt_minorant.constant;
let minidx = self.master.add_minorant(fidx, nxt_minorant)?;
self.minorants[fidx].push(MinorantInfo {
index: minidx,
multiplier: 0.0,
});
if minidx >= self.primals.len() {
self.primals.resize_with(minidx + 1, || None);
}
self.primals[minidx] = Some(nxt_primal);
}
if self.new_cutval > self.cur_val + 1e-3 {
warn!(
"New minorant has higher value in center new:{} old:{}",
self.new_cutval, self.cur_val
);
|
| ︙ | ︙ |
Changes to src/vector.rs.
| ︙ | ︙ | |||
187 188 189 190 191 192 193 |
/// the smaller vector are assumed to be 0.0.
pub fn add_scaled_begin(&mut self, alpha: Real, y: &DVector) {
for (x, y) in self.iter_mut().zip(y.iter()) {
*x += alpha * y;
}
let n = self.len();
if n < y.len() {
| | | 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
/// the smaller vector are assumed to be 0.0.
pub fn add_scaled_begin(&mut self, alpha: Real, y: &DVector) {
for (x, y) in self.iter_mut().zip(y.iter()) {
*x += alpha * y;
}
let n = self.len();
if n < y.len() {
self.extend(y[n..].iter().map(|y| alpha * y));
}
// if self.len() < y.len() {
// self.resize(y.len(), 0.0);
// }
// for i in 0..y.len() {
// unsafe { *self.get_unchecked_mut(i) += alpha * *y.get_unchecked(i) };
// }
|
| ︙ | ︙ | |||
281 282 283 284 285 286 287 |
unsafe { *v.get_unchecked_mut(i) = x };
}
DVector(v)
}
}
}
}
| > > > > > > > > | 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
unsafe { *v.get_unchecked_mut(i) = x };
}
DVector(v)
}
}
}
}
#[test]
fn test_add_scaled_begin() {
let mut x = dvec![1.0; 5];
let y = dvec![2.0; 7];
x.add_scaled_begin(3.0, &y);
assert_eq!(x, dvec![7.0, 7.0, 7.0, 7.0, 7.0, 6.0, 6.0]);
}
|