def solve(self, x0=None, tee=False): xl = self._problem.x_lb() xu = self._problem.x_ub() gl = self._problem.g_lb() gu = self._problem.g_ub() if x0 is None: x0 = self._problem.x_init() xstart = x0 nx = len(xstart) ng = len(gl) cyipopt_solver = ipopt.problem( n=nx, m=ng, problem_obj=self._problem, lb=xl, ub=xu, cl=gl, cu=gu ) # check if we need scaling obj_scaling, x_scaling, g_scaling = self._problem.scaling_factors() if any(_ is not None for _ in (obj_scaling, x_scaling, g_scaling)): # need to set scaling factors if obj_scaling is None: obj_scaling = 1.0 if x_scaling is None: x_scaling = np.ones(nx) if g_scaling is None: g_scaling = np.ones(ng) cyipopt_solver.setProblemScaling(obj_scaling, x_scaling, g_scaling) # add options for k, v in self._options.items(): cyipopt_solver.addOption(k, v) if tee: x, info = cyipopt_solver.solve(xstart) else: newstdout = _redirect_stdout() x, info = cyipopt_solver.solve(xstart) os.dup2(newstdout, 1) return x, info
def test_create_objective_from_numpy(self): # Test issue #87 model = ConcreteModel() nsample = 3 nvariables = 2 X0 = np.array(range(nsample)).reshape([nsample, 1]) model.X = 1 + np.array(range(nsample * nvariables)).reshape( (nsample, nvariables)) X = np.concatenate([X0, model.X], axis=1) model.I = RangeSet(1, nsample) model.J = RangeSet(1, nvariables) error = np.ones((nsample, 1)) beta = np.ones((nvariables + 1, 1)) model.Y = np.dot(X, beta) + error model.beta = Var(model.J) model.beta0 = Var() def obj_fun(model): return sum( abs(model.Y[i - 1] - (model.beta0 + sum(model.X[i - 1, j - 1] * model.beta[j] for j in model.J))) for i in model.I) model.OBJ = Objective(rule=obj_fun) def obj_fun_quad(model): return sum( (model.Y[i - 1] - (model.beta0 + sum(model.X[i - 1, j - 1] * model.beta[j] for j in model.J)))**2 for i in model.I) model.OBJ_QUAD = Objective(rule=obj_fun_quad) self.assertEqual( str(model.OBJ.expr), "abs(4.0 - (beta[1] + 2*beta[2] + beta0)) + " "abs(9.0 - (3*beta[1] + 4*beta[2] + beta0)) + " "abs(14.0 - (5*beta[1] + 6*beta[2] + beta0))") self.assertEqual(model.OBJ.expr.polynomial_degree(), None) self.assertEqual(model.OBJ_QUAD.expr.polynomial_degree(), 2)
def solve(self, model, **kwds): config = self.config(kwds, preserve_implicit=True) if not isinstance(model, Block): raise ValueError("PyomoCyIpoptSolver.solve(model): model " "must be a Pyomo Block") # If this is a Pyomo model / block, then we need to create # the appropriate PyomoNLP, then wrap it in a CyIpoptNLP grey_box_blocks = list(model.component_data_objects( egb.ExternalGreyBoxBlock, active=True)) if grey_box_blocks: # nlp = pyomo_nlp.PyomoGreyBoxNLP(model) nlp = pyomo_grey_box.PyomoNLPWithGreyBoxBlocks(model) else: nlp = pyomo_nlp.PyomoNLP(model) problem = CyIpoptNLP(nlp, intermediate_callback=config.intermediate_callback) xl = problem.x_lb() xu = problem.x_ub() gl = problem.g_lb() gu = problem.g_ub() nx = len(xl) ng = len(gl) cyipopt_solver = cyipopt.Problem( n=nx, m=ng, problem_obj=problem, lb=xl, ub=xu, cl=gl, cu=gu ) # check if we need scaling obj_scaling, x_scaling, g_scaling = problem.scaling_factors() if any(_ is not None for _ in (obj_scaling, x_scaling, g_scaling)): # need to set scaling factors if obj_scaling is None: obj_scaling = 1.0 if x_scaling is None: x_scaling = np.ones(nx) if g_scaling is None: g_scaling = np.ones(ng) try: set_scaling = cyipopt_solver.set_problem_scaling except AttributeError: # Fall back to pre-1.0.0 API set_scaling = cyipopt_solver.setProblemScaling set_scaling(obj_scaling, x_scaling, g_scaling) # add options try: add_option = cyipopt_solver.add_option except AttributeError: # Fall back to pre-1.0.0 API add_option = cyipopt_solver.addOption for k, v in config.options.items(): add_option(k, v) timer = TicTocTimer() try: # We preemptively set up the TeeStream, even if we aren't # going to use it: the implementation is such that the # context manager does nothing (i.e., doesn't start up any # processing threads) until afer a client accesses # STDOUT/STDERR with TeeStream(sys.stdout) as _teeStream: if config.tee: try: fd = sys.stdout.fileno() except (io.UnsupportedOperation, AttributeError): # If sys,stdout doesn't have a valid fileno, # then create one using the TeeStream fd = _teeStream.STDOUT.fileno() else: fd = None with redirect_fd(fd=1, output=fd, synchronize=False): x, info = cyipopt_solver.solve(problem.x_init()) solverStatus = SolverStatus.ok except: msg = "Exception encountered during cyipopt solve:" logger.error(msg, exc_info=sys.exc_info()) solverStatus = SolverStatus.unknown raise wall_time = timer.toc(None) results = SolverResults() if config.load_solutions: nlp.set_primals(x) nlp.set_duals(info['mult_g']) nlp.load_state_into_pyomo( bound_multipliers=(info['mult_x_L'], info['mult_x_U'])) else: soln = results.solution.add() soln.variable.update( (i, {'Value':j, 'ipopt_zL_out': zl, 'ipopt_zU_out': zu}) for i,j,zl,zu in zip( nlp.variable_names(), x, info['mult_x_L'], info['mult_x_U'] ) ) soln.constraint.update( (i, {'Dual':j}) for i,j in zip( nlp.constraint_names(), info['mult_g'])) results.problem.name = model.name obj = next(model.component_data_objects(Objective, active=True)) if obj.sense == minimize: results.problem.sense = ProblemSense.minimize results.problem.upper_bound = info['obj_val'] else: results.problem.sense = ProblemSense.maximize results.problem.lower_bound = info['obj_val'] results.problem.number_of_objectives = 1 results.problem.number_of_constraints = ng results.problem.number_of_variables = nx results.problem.number_of_binary_variables = 0 results.problem.number_of_integer_variables = 0 results.problem.number_of_continuous_variables = nx # TODO: results.problem.number_of_nonzeros results.solver.name = 'cyipopt' results.solver.return_code = info['status'] results.solver.message = info['status_msg'] results.solver.wallclock_time = wall_time status_enum = _cyipopt_status_enum[info['status_msg']] results.solver.termination_condition = _ipopt_term_cond[status_enum] results.solver.status = TerminationCondition.to_solver_status( results.solver.termination_condition) if config.return_nlp: return results, nlp return results
def solve(self, x0=None, tee=False): xl = self._problem.x_lb() xu = self._problem.x_ub() gl = self._problem.g_lb() gu = self._problem.g_ub() if x0 is None: x0 = self._problem.x_init() xstart = x0 nx = len(xstart) ng = len(gl) cyipopt_solver = cyipopt.Problem( n=nx, m=ng, problem_obj=self._problem, lb=xl, ub=xu, cl=gl, cu=gu ) # check if we need scaling obj_scaling, x_scaling, g_scaling = self._problem.scaling_factors() if any(_ is not None for _ in (obj_scaling, x_scaling, g_scaling)): # need to set scaling factors if obj_scaling is None: obj_scaling = 1.0 if x_scaling is None: x_scaling = np.ones(nx) if g_scaling is None: g_scaling = np.ones(ng) try: set_scaling = cyipopt_solver.set_problem_scaling except AttributeError: # Fall back to pre-1.0.0 API set_scaling = cyipopt_solver.setProblemScaling set_scaling(obj_scaling, x_scaling, g_scaling) # add options try: add_option = cyipopt_solver.add_option except AttributeError: # Fall back to pre-1.0.0 API add_option = cyipopt_solver.addOption for k, v in self._options.items(): add_option(k, v) # We preemptively set up the TeeStream, even if we aren't # going to use it: the implementation is such that the # context manager does nothing (i.e., doesn't start up any # processing threads) until afer a client accesses # STDOUT/STDERR with TeeStream(sys.stdout) as _teeStream: if tee: try: fd = sys.stdout.fileno() except (io.UnsupportedOperation, AttributeError): # If sys,stdout doesn't have a valid fileno, # then create one using the TeeStream fd = _teeStream.STDOUT.fileno() else: fd = None with redirect_fd(fd=1, output=fd, synchronize=False): x, info = cyipopt_solver.solve(xstart) return x, info
def solve(self, model, **kwds): config = self.config(kwds, preserve_implicit=True) if not isinstance(model, Block): raise ValueError("PyomoCyIpoptSolver.solve(model): model " "must be a Pyomo Block") # If this is a Pyomo model / block, then we need to create # the appropriate PyomoNLP, then wrap it in a CyIpoptNLP grey_box_blocks = list( model.component_data_objects(egb.ExternalGreyBoxBlock, active=True)) if grey_box_blocks: nlp = pyomo_nlp.PyomoGreyBoxNLP(model) else: nlp = pyomo_nlp.PyomoNLP(model) problem = CyIpoptNLP(nlp) xl = problem.x_lb() xu = problem.x_ub() gl = problem.g_lb() gu = problem.g_ub() nx = len(xl) ng = len(gl) cyipopt_solver = ipopt.problem(n=nx, m=ng, problem_obj=problem, lb=xl, ub=xu, cl=gl, cu=gu) # check if we need scaling obj_scaling, x_scaling, g_scaling = problem.scaling_factors() if any(_ is not None for _ in (obj_scaling, x_scaling, g_scaling)): # need to set scaling factors if obj_scaling is None: obj_scaling = 1.0 if x_scaling is None: x_scaling = np.ones(nx) if g_scaling is None: g_scaling = np.ones(ng) cyipopt_solver.setProblemScaling(obj_scaling, x_scaling, g_scaling) # add options for k, v in config.options.items(): cyipopt_solver.addOption(k, v) timer = TicTocTimer() try: if config.tee: x, info = cyipopt_solver.solve(problem.x_init()) else: newstdout = _redirect_stdout() x, info = cyipopt_solver.solve(problem.x_init()) os.dup2(newstdout, 1) solverStatus = SolverStatus.ok except: msg = "Exception encountered during cyipopt solve:" logger.error(msg, exc_info=sys.exc_info()) solverStatus = SolverStatus.unknown raise wall_time = timer.toc(None) results = SolverResults() if config.load_solutions: nlp.set_primals(x) nlp.set_duals(info['mult_g']) nlp.load_state_into_pyomo(bound_multipliers=(info['mult_x_L'], info['mult_x_U'])) else: soln = results.solution.add() soln.variable.update((i, { 'Value': j, 'ipopt_zL_out': zl, 'ipopt_zU_out': zu }) for i, j, zl, zu in zip(nlp.variable_names(), x, info['mult_x_L'], info['mult_x_U'])) soln.constraint.update((i, { 'Dual': j }) for i, j in zip(nlp.constraint_names(), info['mult_g'])) results.problem.name = model.name obj = next(model.component_data_objects(Objective, active=True)) if obj.sense == minimize: results.problem.sense = ProblemSense.minimize results.problem.upper_bound = info['obj_val'] else: results.problem.sense = ProblemSense.maximize results.problem.lower_bound = info['obj_val'] results.problem.number_of_objectives = 1 results.problem.number_of_constraints = ng results.problem.number_of_variables = nx results.problem.number_of_binary_variables = 0 results.problem.number_of_integer_variables = 0 results.problem.number_of_continuous_variables = nx # TODO: results.problem.number_of_nonzeros results.solver.name = 'cyipopt' results.solver.return_code = info['status'] results.solver.message = info['status_msg'] results.solver.wallclock_time = wall_time status_enum = _cyipopt_status_enum[info['status_msg']] results.solver.termination_condition = _ipopt_term_cond[status_enum] results.solver.status = TerminationCondition.to_solver_status( results.solver.termination_condition) return results
def lingen(ntrain, p): nval = 1000 n = ntrain + nval # Define model statistics s = 5 # s determines the number of nonzero regression components snr = 5 # snr = var(response)/var(residuals) rho = 0.5 # Correlation between columns of X 0<= rho <= 1 b_type = [ 1, 2, 3, 5 ] # Defined in accordance with test set used in Tibshirani's comparison # The desired mean values of the sample mu = np.zeros([p]) # The desired covariance matrix r = np.zeros([p, p]) for i in range(p): for j in range(p): r[i, j] = rho**abs(i - j) # Generate the random samples x = np.random.multivariate_normal(mu, r, size=n) # Determine true model(s) t1f = lambda m, n: [i * n // m + n // (2 * m) for i in range(m)] b_true = np.zeros([len(b_type), p]) ind = 0 for i in b_type: if i == 1: # s components equal to 1 at ~equivalently spaced indices b_true[ind][t1f(s, p)] = 1.0 ind += 1 elif i == 2: # First s components equal to 1 b_true[ind][0:s] = 1.0 ind += 1 elif i == 3: # First s components equally spaced between 0.5 and 10 b_true[ind][0:s] = np.linspace(0.5, 10, s) # print( b_true[ind][0:s]) ind += 1 elif i == 5: # First 2 equal to 1, the rest decaying according to 0.5 ** (j - s) b_true[ind][0:s] = 1.0 for j in range(s, p): b_true[ind][j] = 0.5**(j - s) # Sample response data and add noise y = np.zeros([len(b_type), n]) for i in range(len(b_type)): bvec = b_true[i] res = np.matmul(x, bvec) # Add noise of specified signal-to-noise ratio res[:] = res[:] + np.random.normal( np.zeros([n]), np.ones([n]) * np.sqrt(np.var(res) / snr)) y[i, :] = res z = {} z = {str(i): y[0][i - 1] for i in range(1, ntrain + 1)} # z = y[0][0:ntrain] x_n = {} for i in range(1, ntrain + 1): x_n[str(i)] = {('p' + str(j + 1)): x[i - 1, j] for j in range(0, p)} # x_n = x[0:ntrain,:] return z, x_n, ntrain, p