<commit_before>/*
 *
 *  * Project to resolve 2D cutting stock problem for Discreet Optimizations course at Polytech Lyon
 *  * Copyright (C) 2015.  <NAME> and <NAME>
 *  *
 *  * This program is free software: you can redistribute it and/or modify
 *  * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *  *
 *  * You should have received a copy of the GNU Affero General Public License
 *  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.polytech4A.cuttingstock.core.method;

import com.polytech4A.cuttingstock.core.model.Box;
import com.polytech4A.cuttingstock.core.model.Pattern;
import com.polytech4A.cuttingstock.core.model.Solution;
import com.polytech4A.cuttingstock.core.resolution.util.context.Context;
import org.apache.commons.math.optimization.GoalType;
import org.apache.commons.math.optimization.OptimizationException;
import org.apache.commons.math.optimization.RealPointValuePair;
import org.apache.commons.math.optimization.linear.LinearConstraint;
import org.apache.commons.math.optimization.linear.LinearObjectiveFunction;
import org.apache.commons.math.optimization.linear.Relationship;
import org.apache.commons.math.optimization.linear.SimplexSolver;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by <NAME> on 21/03/2015.
 *
 * @author Adrien CHAUSSENDE
 * @version 1.0
 *          <p>
 *          Representation of the linear resolution method for 2D cutting stock problem.
 *          It is a linear programming problem resolution.
 */
public class LinearResolutionMethod {

    /**
     * Function to minimize.
     */
    private LinearObjectiveFunction function;

    /**
     * List containing the constraints that the function is subject to.
     */
    private ArrayList<LinearConstraint> constraints;

    /**
     * Current context.
     */
    private Context context;

    /**
     * Create a LinearResolution Method with initial resolution to initialize function and constraints.
     *
     * @param context Current context.
     */
    public LinearResolutionMethod(Context context) {
        this.context = context;
    }

    /**
     * Resolve linear programming problem when minimizing the equation with current constraints. Returns
     *
     * @param solution Solution to minimize the objective the function from.
     * @return Number of printings and cost value.
     */
    public Result minimize(Solution solution) {
        updateFunction(solution);
        updateConstraints(solution);
        try {
            RealPointValuePair result = new SimplexSolver().optimize(function, constraints, GoalType.MINIMIZE, true);
            double[] point = result.getPoint();
            if (result.getValue() < 0) {
                return null;
            }
            for (int i = 0; i < point.length; ++i) {
                if (point[i] < 0) {
                    return null;
                }
            }
            return new Result(point, context.getSheetCost(), context.getPatternCost());
        } catch (OptimizationException e) {
            e.printStackTrace();
        }<commit_msg>ACE : Add debug message when throwing OptimizationException in LinearResolutionMethod.minimize.<commit_after>/*
 *
 *  * Project to resolve 2D cutting stock problem for Discreet Optimizations course at Polytech Lyon
 *  * Copyright (C) 2015.  <NAME> and <NAME>
 *  *
 *  * This program is free software: you can redistribute it and/or modify
 *  * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *  *
 *  * You should have received a copy of the GNU Affero General Public License
 *  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.polytech4A.cuttingstock.core.method;

import com.polytech4A.cuttingstock.core.model.Box;
import com.polytech4A.cuttingstock.core.model.Pattern;
import com.polytech4A.cuttingstock.core.model.Solution;
import com.polytech4A.cuttingstock.core.resolution.util.context.Context;
import org.apache.commons.math.optimization.GoalType;
import org.apache.commons.math.optimization.OptimizationException;
import org.apache.commons.math.optimization.RealPointValuePair;
import org.apache.commons.math.optimization.linear.LinearConstraint;
import org.apache.commons.math.optimization.linear.LinearObjectiveFunction;
import org.apache.commons.math.optimization.linear.Relationship;
import org.apache.commons.math.optimization.linear.SimplexSolver;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by <NAME> on 21/03/2015.
 *
 * @author <NAME>
 * @version 1.0
 *          <p>
 *          Representation of the linear resolution method for 2D cutting stock problem.
 *          It is a linear programming problem resolution.
 */
public class LinearResolutionMethod {

    /**
     * Logger.
     */
    private static Logger logger = Logger.getLogger(LinearResolutionMethod.class);

    /**
     * Function to minimize.
     */
    private LinearObjectiveFunction function;

    /**
     * List containing the constraints that the function is subject to.
     */
    private ArrayList<LinearConstraint> constraints;

    /**
     * Current context.
     */
    private Context context;

    /**
     * Create a LinearResolution Method with initial resolution to initialize function and constraints.
     *
     * @param context Current context.
     */
    public LinearResolutionMethod(Context context) {
        this.context = context;
    }

    /**
     * Resolve linear programming problem when minimizing the equation with current constraints. Returns
     *
     * @param solution Solution to minimize the objective the function from.
     * @return Number of printings and cost value.
     */
    public Result minimize(Solution solution) {
        updateFunction(solution);
        updateConstraints(solution);
        try {
            RealPointValuePair result = new SimplexSolver().optimize(function, constraints, GoalType.MINIMIZE, true);
            double[] point = result.getPoint();
            if (result.getValue() < 0) {
                return null;
            }
            for (int i = 0; i < point.length; ++i) {
                if (point[i] < 0) {
                    return null;
                }
            }
            return new Result(point, context.getSheetCost(), context.getPatternCost());
        } catch (OptimizationException e) {
            logger.debug("LinearResolutionMethod.minimize: " + e.getMessage());
        }
{'fraction_non_alphanumeric': 0.08922950595319556, 'fraction_numerical': 0.006979608594498426, 'mean_word_length': 2.8893028206492817, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1698809', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1899, 'n_words': 616}