Spaces:
Sleeping
Sleeping
File size: 7,537 Bytes
165ee00 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
import os
import subprocess
import stat
import pandas as pd
import torch
import numpy as np
def Mazda(init_samples):
##########################################
# Scaling
##########################################
# Define the path to your Excel file
file_path = '/home/turbo/rosenyu/Bank_High_DIM/Mazda_CdMOBP/Mazda_CdMOBP/Info_Mazda_CdMOBP_edited.xlsx'
# Read the Excel file into a DataFrame
dataframe = pd.read_excel(file_path, sheet_name='Explain_DV_and_Const.')
# Display the DataFrame to ensure it has been read correctly
bounds = dataframe.values[1:, 1:3]
bounds_tensor = torch.tensor(bounds, dtype=torch.float32)
# print(bounds_tensor.shape)
range_bounds = bounds_tensor[:,1] - bounds_tensor[:,0]
scaled_samples = init_samples * range_bounds + bounds_tensor[:,0]
# print(scaled_samples)
# Convert the torch tensor to a numpy array
data_numpy_back = scaled_samples.numpy()
# Create a pandas DataFrame from the numpy array
dataframe_back = pd.DataFrame(data_numpy_back)
# Write the DataFrame to a text file with space-separated values
output_file_path = '/home/turbo/rosenyu/Bank_High_DIM/Mazda_CdMOBP/Mazda_CdMOBP/rosen_sample_t2/pop_vars_eval.txt'
dataframe_back.to_csv(output_file_path, sep='\t', header=False, index=False)
#####################
#####################
#####################
# Run Bash file
#####################
# Change the current working directory
os.chdir('/home/turbo/rosenyu/Bank_High_DIM/Mazda_CdMOBP/Mazda_CdMOBP/rosen_sample_t2')
# Get the current permissions of the file
current_permissions = os.stat(os.getcwd()).st_mode
# Add execute permissions for the owner, group, and others
new_permissions = current_permissions | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
# Apply the new permissions
os.chmod(os.getcwd(), new_permissions)
# Script name
script_name = 'run.sh'
# Run the bash script in the background
process = subprocess.Popen(['bash', script_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True)
process.wait()
# Optional: capture the output and error messages
stdout, stderr = process.communicate()
os.chdir('/home/turbo/rosenyu/Bank_High_DIM/')
# print(os.getcwd())
#####################
#####################
#####################
# Read in objective and constraints
#####################
# Read the data from the file into a pandas DataFrame
file_path = '/home/turbo/rosenyu/Bank_High_DIM/Mazda_CdMOBP/Mazda_CdMOBP/rosen_sample_t2/pop_objs_eval.txt'
objs_dataframe = pd.read_csv(file_path, delim_whitespace=True, header=None)
# Convert the DataFrame to a numpy array
objs_data_numpy = objs_dataframe.values
# Convert the numpy array to a torch tensor
objs_data_tensor = torch.tensor(objs_data_numpy, dtype=torch.float32)
objs_data_tensor = objs_data_tensor[:,0].reshape(objs_data_tensor.shape[0],1)
# Read the data from the file into a pandas DataFrame
file_path = '/home/turbo/rosenyu/Bank_High_DIM/Mazda_CdMOBP/Mazda_CdMOBP/rosen_sample_t2/pop_cons_eval.txt'
cons_dataframe = pd.read_csv(file_path, delim_whitespace=True, header=None)
# Convert the DataFrame to a numpy array
cons_data_numpy = cons_dataframe.values
# Convert the numpy array to a torch tensor
cons_data_tensor = torch.tensor(cons_data_numpy, dtype=torch.float32)
# print(objs_data_tensor)
# print(cons_data_tensor)
return cons_data_tensor, -objs_data_tensor
def Mazda_softpen(init_samples):
##########################################
# Scaling
##########################################
# Define the path to your Excel file
file_path = '/home/turbo/rosenyu/Bank_High_DIM/Mazda_CdMOBP/Mazda_CdMOBP/Info_Mazda_CdMOBP_edited.xlsx'
# Read the Excel file into a DataFrame
dataframe = pd.read_excel(file_path, sheet_name='Explain_DV_and_Const.')
# Display the DataFrame to ensure it has been read correctly
bounds = dataframe.values[1:, 1:3]
bounds_tensor = torch.tensor(bounds, dtype=torch.float32)
# print(bounds_tensor.shape)
range_bounds = bounds_tensor[:,1] - bounds_tensor[:,0]
scaled_samples = init_samples * range_bounds + bounds_tensor[:,0]
# print(scaled_samples)
# Convert the torch tensor to a numpy array
data_numpy_back = scaled_samples.numpy()
# Create a pandas DataFrame from the numpy array
dataframe_back = pd.DataFrame(data_numpy_back)
# Write the DataFrame to a text file with space-separated values
output_file_path = '/home/turbo/rosenyu/Bank_High_DIM/Mazda_CdMOBP/Mazda_CdMOBP/rosen_sample_t2/pop_vars_eval.txt'
dataframe_back.to_csv(output_file_path, sep='\t', header=False, index=False)
#####################
#####################
#####################
# Run Bash file
#####################
# Change the current working directory
os.chdir('/home/turbo/rosenyu/Bank_High_DIM/Mazda_CdMOBP/Mazda_CdMOBP/rosen_sample_t2')
# Get the current permissions of the file
current_permissions = os.stat(os.getcwd()).st_mode
# Add execute permissions for the owner, group, and others
new_permissions = current_permissions | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
# Apply the new permissions
os.chmod(os.getcwd(), new_permissions)
# Script name
script_name = 'run.sh'
# Run the bash script in the background
process = subprocess.Popen(['bash', script_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True)
process.wait()
# Optional: capture the output and error messages
stdout, stderr = process.communicate()
os.chdir('/home/turbo/rosenyu/Bank_High_DIM/')
# print(os.getcwd())
#####################
#####################
#####################
# Read in objective and constraints
#####################
# Read the data from the file into a pandas DataFrame
file_path = '/home/turbo/rosenyu/Bank_High_DIM/Mazda_CdMOBP/Mazda_CdMOBP/rosen_sample_t2/pop_objs_eval.txt'
objs_dataframe = pd.read_csv(file_path, delim_whitespace=True, header=None)
# Convert the DataFrame to a numpy array
objs_data_numpy = objs_dataframe.values
# Convert the numpy array to a torch tensor
objs_data_tensor = torch.tensor(objs_data_numpy, dtype=torch.float32)
objs_data_tensor = objs_data_tensor[:,0].reshape(objs_data_tensor.shape[0],1)
# Read the data from the file into a pandas DataFrame
file_path = '/home/turbo/rosenyu/Bank_High_DIM/Mazda_CdMOBP/Mazda_CdMOBP/rosen_sample_t2/pop_cons_eval.txt'
cons_dataframe = pd.read_csv(file_path, delim_whitespace=True, header=None)
# Convert the DataFrame to a numpy array
cons_data_numpy = cons_dataframe.values
# Convert the numpy array to a torch tensor
cons_data_tensor = torch.tensor(cons_data_numpy, dtype=torch.float32)
cost = cons_data_tensor
cost[cost<0] = 0
cost = cost.sum(dim=1).reshape(cost.shape[0], 1)
objs_data_tensor = objs_data_tensor + cost
# print(objs_data_tensor)
# print(cons_data_tensor)
return cons_data_tensor, -objs_data_tensor
|