1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * https://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /* 19 * This is not the original file distributed by the Apache Software Foundation 20 * It has been modified by the Hipparchus project 21 */ 22 package org.hipparchus.optim.linear; 23 24 import java.util.Collection; 25 import java.util.Collections; 26 27 import org.hipparchus.exception.MathIllegalStateException; 28 import org.hipparchus.optim.OptimizationData; 29 import org.hipparchus.optim.PointValuePair; 30 import org.hipparchus.optim.nonlinear.scalar.MultivariateOptimizer; 31 32 /** 33 * Base class for implementing linear optimizers. 34 * 35 */ 36 public abstract class LinearOptimizer 37 extends MultivariateOptimizer { 38 /** 39 * Linear objective function. 40 */ 41 private LinearObjectiveFunction function; 42 /** 43 * Linear constraints. 44 */ 45 private Collection<LinearConstraint> linearConstraints; 46 /** 47 * Whether to restrict the variables to non-negative values. 48 */ 49 private boolean nonNegative; 50 51 /** 52 * Simple constructor with default settings. 53 * 54 */ 55 protected LinearOptimizer() { 56 super(null); // No convergence checker. 57 } 58 59 /** Check if variables are restricted to non-negative values. 60 * @return {@code true} if the variables are restricted to non-negative values 61 */ 62 protected boolean isRestrictedToNonNegative() { 63 return nonNegative; 64 } 65 66 /** Get optimization type. 67 * @return the optimization type 68 */ 69 protected LinearObjectiveFunction getFunction() { 70 return function; 71 } 72 73 /** Get constraints. 74 * @return the constraints 75 */ 76 protected Collection<LinearConstraint> getConstraints() { 77 return Collections.unmodifiableCollection(linearConstraints); 78 } 79 80 /** 81 * {@inheritDoc} 82 * 83 * @param optData Optimization data. In addition to those documented in 84 * {@link MultivariateOptimizer#parseOptimizationData(OptimizationData[]) 85 * MultivariateOptimizer}, this method will register the following data: 86 * <ul> 87 * <li>{@link LinearObjectiveFunction}</li> 88 * <li>{@link LinearConstraintSet}</li> 89 * <li>{@link NonNegativeConstraint}</li> 90 * </ul> 91 * @return {@inheritDoc} 92 * @throws MathIllegalStateException if the maximal number of 93 * iterations is exceeded. 94 */ 95 @Override 96 public PointValuePair optimize(OptimizationData... optData) 97 throws MathIllegalStateException { 98 // Set up base class and perform computation. 99 return super.optimize(optData); 100 } 101 102 /** 103 * Scans the list of (required and optional) optimization data that 104 * characterize the problem. 105 * 106 * @param optData Optimization data. 107 * The following data will be looked for: 108 * <ul> 109 * <li>{@link LinearObjectiveFunction}</li> 110 * <li>{@link LinearConstraintSet}</li> 111 * <li>{@link NonNegativeConstraint}</li> 112 * </ul> 113 */ 114 @Override 115 protected void parseOptimizationData(OptimizationData... optData) { 116 // Allow base class to register its own data. 117 super.parseOptimizationData(optData); 118 119 // The existing values (as set by the previous call) are reused if 120 // not provided in the argument list. 121 for (OptimizationData data : optData) { 122 if (data instanceof LinearObjectiveFunction) { 123 function = (LinearObjectiveFunction) data; 124 continue; 125 } 126 if (data instanceof LinearConstraintSet) { 127 linearConstraints = ((LinearConstraintSet) data).getConstraints(); 128 continue; 129 } 130 if (data instanceof NonNegativeConstraint) { 131 nonNegative = ((NonNegativeConstraint) data).isRestrictedToNonNegative(); 132 continue; 133 } 134 } 135 } 136 }