def setUp(self): """Setup simple problem""" np.random.seed(1) # This needs to work for different p = 50 n = 200 F = np.random.randn(n, p) D = np.diag(np.random.rand(n) * np.sqrt(p)) Sigma = F.dot(F.T) + D gamma = 1.0 mu = cp.Parameter(n, name='mu') x = cp.Variable(n) cost = -mu @ x + gamma * cp.quad_form(x, Sigma) + .5 * cp.norm(x, 1) constraints = [cp.sum(x) == 1, x >= 0] problem = cp.Problem(cp.Minimize(cost), constraints) # Define optimizer # Force mosek to be single threaded m = mlopt.Optimizer(problem, # log_level=logging.DEBUG, ) ''' Sample points ''' theta_bar = 10 * np.random.rand(n) radius = 0.8 ''' Train and solve ''' # Training and testing data n_train = 100 n_test = 10 # Sample points from multivariate ball X_d = uniform_sphere_sample(theta_bar, radius, n=n_train) self.df_train = pd.DataFrame({'mu': list(X_d)}) # # Train and test using pytorch # params = { # 'learning_rate': [0.01], # 'batch_size': [100], # 'n_epochs': [200] # } # # m.train(df, parallel=False, learner=mlopt.PYTORCH, params=params) # Testing data X_d_test = uniform_sphere_sample(theta_bar, radius, n=n_test) df_test = pd.DataFrame({'mu': list(X_d_test)}) # Store stuff self.m = m self.df_test = df_test
def setUp(self): """Setup simple problem""" np.random.seed(1) # This needs to work for different p = 10 n = 30 F = np.random.randn(n, p) D = np.diag(np.random.rand(n) * np.sqrt(p)) Sigma = F.dot(F.T) + D gamma = 1.0 mu = cp.Parameter(n, name='mu') x = cp.Variable(n) cost = -mu @ x + gamma * cp.quad_form(x, Sigma) + .5 * cp.norm(x, 1) constraints = [cp.sum(x) == 1, x >= 0] problem = cp.Problem(cp.Minimize(cost), constraints) # Define optimizer m = mlopt.Optimizer(problem, # log_level=logging.DEBUG ) ''' Sample points ''' theta_bar = 10 * np.random.rand(n) radius = 1.0 ''' Train and solve ''' # Training and testing data n_train = 300 n_test = 10 # Sample points from multivariate ball X_d = uniform_sphere_sample(theta_bar, radius, n=n_train) df = pd.DataFrame({'mu': list(X_d)}) m.train(df, filter_strategies=True, parallel=False, learner=mlopt.PYTORCH, n_train_trials=10) # m.train(df, parallel=False, learner=mlopt.XGBOOST, n_train_trials=10) # Testing data X_d_test = uniform_sphere_sample(theta_bar, radius, n=n_test) df_test = pd.DataFrame({'mu': list(X_d_test)}) # Store stuff self.m = m self.df_test = df_test
def test_parameters_in_vectors(self): """Check if parameters not in matrices are recognized """ # Problem data. m = 2 n = 1 np.random.seed(1) A = np.random.randn(m, n) b = np.random.randn(m) theta = cp.Parameter(nonneg=True) theta.value = 8.9 x = cp.Variable(n) objective = cp.Minimize(cp.sum_squares(A @ x - b) + cp.norm(x, 1)) constraints = [0 <= x, x <= theta] problem = cp.Problem(objective, constraints) m = mlopt.Optimizer(problem) self.assertFalse(m._problem.parameters_in_matrices)