Spaces:
Sleeping
Sleeping
File size: 3,279 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 |
import os
import subprocess
import sys
import tempfile
from pathlib import Path
from platform import machine
import numpy as np
import torch
import stat
def MOPTA08_Car_single(x):
# 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)
sysarch = 64 if sys.maxsize > 2 ** 32 else 32
# machine = machine().lower()
# if machine == "armv7l":
# assert sysarch == 32, "Not supported"
# mopta_exectutable = "mopta08_armhf.bin"
# elif machine == "x86_64":
# assert sysarch == 64, "Not supported"
# mopta_exectutable = "mopta08_elf64.bin"
# elif machine == "i386":
# assert sysarch == 32, "Not supported"
# mopta_exectutable = "mopta08_elf32.bin"
# else:
# raise RuntimeError("Machine with this architecture is not supported")
machine = "x86_64"
mopta_exectutable = "mopta08_elf64.bin"
mopta_full_path = os.path.join(
"mopta08", mopta_exectutable
)
# print(mopta_full_path)
directory_file_descriptor = tempfile.TemporaryDirectory()
# directory_name = directory_file_descriptor.name
directory_name = Path(__file__).parent
##########################################################################################
# Input here
# if x == None:
# x = np.random.rand(124)
# print(x.shape)
##########################################################################################
with open(os.path.join(directory_name, "input.txt"), "w+") as tmp_file:
for _x in x:
tmp_file.write(f"{_x}\n")
popen = subprocess.Popen(
mopta_full_path,
# '#!/home/rosen/Dropbox (MIT)/Rosen_DeC0De/MOPTA_Test/mopta08/mopta08_elf64.bin',
stdout=subprocess.PIPE,
cwd=directory_name,
shell=True,
)
popen.wait()
with open(os.path.join(directory_name, "output.txt"), "r") as tmp_file:
output = (
tmp_file
.read()
.split("\n")
)
# print(output)
# print(x)
# print(output)
output = [x.strip() for x in output]
output = np.array([float(x) for x in output if len(x) > 0])
value = output[0]
constraints = output[1:]
# print(value, constraints)
return constraints, value
def MOPTA08_Car(X):
GX = torch.zeros(X.shape[0], 68)
FX = torch.zeros(X.shape[0], 1)
for ii in range(X.shape[0]):
input_x = X[ii,:].numpy()
gx, fx = MOPTA08_Car_single(input_x)
GX[ii,:] = torch.from_numpy(gx)
FX[ii,:] = -fx
return GX, FX
def MOPTA08_Car_softpen(X):
GX = torch.zeros(X.shape[0], 68)
FX = torch.zeros(X.shape[0], 1)
for ii in range(X.shape[0]):
input_x = X[ii,:].numpy()
gx, fx = MOPTA08_Car_single(input_x)
GX[ii,:] = torch.from_numpy(gx)
FX[ii,:] = fx
cost = GX
cost[cost<0] = 0
cost = cost.sum(dim=1).reshape(cost.shape[0], 1)
FX = FX + cost
return GX, -FX
|