/*
 * Copyright (c) 2016 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/>
 */

use {Real, DVector, Minorant};

use std::error;
use std::result;

quick_error! {
    /// A master problem error.
    #[derive(Debug)]
    pub enum Error {
        Solver(err: Box<error::Error>) {
            cause(&**err)
            description(err.description())
            display("Master problem solver error: {}", err)
        }
    }
}


/// Result type for master problems.
pub type Result<T> = result::Result<T, Error>;

pub trait MasterProblem {
    /// The the lower and upper bounds of the variables.
    fn set_vars(&mut self, nvars: usize, lb : Option<DVector>, ub: Option<DVector>);

    /// Return the current number of minorants of subproblem `fidx`.
    fn num_minorants(&self, fidx : usize) -> usize;

    /// Add a new minorant to the model.
    fn add_minorant(&mut self, fidx: usize, minorant: Minorant) -> Result<()>;

    /// Return the current weight of the quadratic term.
    fn weight(&self) -> Real;

    /// Set the weight of the quadratic term, must be > 0.
    fn set_weight(&mut self, weight: Real);

    /// Solve the master problem.
    fn solve(&mut self, cur_value: Real) -> Result<()>;

    /// Aggregate the given minorants according to the current solution.
    fn aggregate(&mut self, fidx: usize, mins: &[usize]);

    /// Return the primal optimal solution.
    fn get_primopt(&self) -> DVector;

    /// Return the primal optimal solution value.
    fn get_primoptval(&self) -> Real;

    /// Return $\\|d\^*\\|\^2$ of the current dual optimal solution $d\^*$.
    fn get_dualoptnorm2(&self) -> Real;

    /// Move the center of the master problem to $\alpha \cdot d$.
    fn move_center(&mut self, alpha: Real, d: &DVector);

    /// Set the maximal number of inner iterations.
    fn set_max_updates(&mut self, max_updates: usize);

    /// Return the current number of inner iterations.
    fn cnt_updates(&self) -> usize;
}
