Example #1
0
from SROMPy.target import SampleRandomVector
'''
Ex3 - unimodal 3D
Script to generate PW constant SROM approximation to EOL and compare it with the
monte carlo solution (w/ surrogate model)
'''

mc_eol_file = "mc_data/eol_samples_MC.txt"

srom_size = 20

srom_eol_file = "srom_data/srom_eol_m" + str(srom_size) + ".txt"
srom_input_file = "srom_data/srom_m" + str(srom_size) + ".txt"

# Get MC EOL samples.
MC_eols = np.genfromtxt(mc_eol_file)

# Get SROM EOL samples & probabilities from input srom.
srom_end_of_life_data = np.genfromtxt(srom_eol_file)

# Probabilities in last column.
srom_probabilities = np.genfromtxt(srom_input_file)[:, -1]

# Make MC random variable & SROM to compare.
end_of_life_srom = SROM(srom_size, dim=1)
end_of_life_srom.set_params(srom_end_of_life_data, srom_probabilities)
end_of_life_monte_carlo = SampleRandomVector(MC_eols)

pp = Postprocessor(end_of_life_srom, end_of_life_monte_carlo)
pp.compare_cdfs(variable_names=["EOL"])
Example #2
0
MC_eols = np.genfromtxt(mc_eol_file)

#Get SROM EOL samples, FD samples and input SROM from file
srom_eols = np.genfromtxt(srom_eol_file)
srom_fd_eols = np.genfromtxt(srom_fd_eol_file)
input_srom = SROM(sromsize, dim)
input_srom.load_params(srom_input_file)

#Get FD step sizes from file (the same for all samples, just pull the first)
#Step sizes chosen as approximately 2% of the median sample value of inputs
stepsizes = [0.0065, 0.083, 0.025]

#Calculate gradient from FiniteDifference class:
gradient = FD.compute_gradient(srom_eols, srom_fd_eols, stepsizes)

#Create SROM surrogate, sample, and create random variable solution
surrogate_PWL = SROMSurrogate(input_srom, srom_eols, gradient)
srom_eol_samples = surrogate_PWL.sample(MC_inputs)
solution_PWL = SampleRandomVector(srom_eol_samples)

#Store EOL samples for plotting later:
eolfile = "srom_data/srom_eol_samples_m" + str(sromsize) + ".txt"
#np.savetxt(eolfile, srom_eol_samples)  #NOTE - avoid overwriting paper data

#Make MC random variable solution
eol_mc = SampleRandomVector(MC_eols)

#COmpare solutions
pp = Postprocessor(solution_PWL, eol_mc)
pp.compare_CDFs()
Example #3
0
from SROMPy.postprocess import Postprocessor
from SROMPy.srom import SROM, SROMSurrogate
from SROMPy.target import SampleRandomVector
'''
Generate SROM to model input distribution (samples)
'''

# Specify input/output files and SROM optimization parameters.
dim = 3
srom_size = 20
monte_carlo_end_of_life_input_filename = "mc_data/input_samples_MC.txt"
monte_carlo_end_of_life_sample_filename = "mc_data/eol_samples_MC.txt"

# Define target random variable from samples.
monte_carlo_samples = np.genfromtxt(monte_carlo_end_of_life_sample_filename)
target = SampleRandomVector(monte_carlo_samples)

# Define SROM, determine optimal parameters, store parameters.
input_srom = SROM(srom_size, dim)
input_srom.optimize(target, weights=[1, 1, 1], error="SSE")

# Compare the CDFs.
pp = Postprocessor(input_srom, target)
pp.compare_cdfs(save_figure=False)

# Run the model for each input SROM sample:
srom_end_of_life_data = np.zeros(srom_size)
(srom_samples, srom_probabilities) = input_srom.get_params()
for i, sample in enumerate(srom_samples):
    srom_end_of_life_data[i] = model.evaluate(sample)
Example #4
0
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import numpy
from os import path
from SROMPy.postprocess import Postprocessor
from SROMPy.srom import SROM, SROMSurrogate
from SROMPy.target import SampleRandomVector

#Define target random vector from samples
monte_carlo_input_samples_filename = path.join("mc_data",
                                               "input_samples_MC.txt")
monte_carlo_input_samples = numpy.genfromtxt(
    monte_carlo_input_samples_filename)
target_vector = SampleRandomVector(monte_carlo_input_samples)

#Define SROM and determine optimal parameters
srom_size = 20
input_srom = SROM(size=srom_size, dim=3)
input_srom.optimize(target_vector)

#Compare the input CDFs (produces Figure 6)
post_processor = Postprocessor(input_srom, target_vector)
post_processor.compare_CDFs(variablenames=[r'$y_{0}$', r'log$C$', r'$n$'])

#Run the model for each input SROM sample:
srom_results = numpy.zeros(srom_size)
(srom_samples, srom_probs) = input_srom.get_params()

# TODO: define model here.
Example #5
0
def sample_random_vector():

    np.random.seed(1)
    random_vector = np.random.rand(10)
    return SampleRandomVector(random_vector)
Example #6
0
from SROMPy.postprocess import Postprocessor
from SROMPy.srom import SROM, SROMSurrogate
from SROMPy.target import SampleRandomVector
'''
Generate SROM to model input distribution (samples)
'''

#Specify input/output files and SROM optimization parameters
dim = 3
srom_size = 20
mc_input_file = "mc_data/input_samples_MC.txt"
mc_eol_file = "mc_data/eol_samples_MC.txt"

#Define target random variable from samples
MCsamples = np.genfromtxt(samplesfile)
target = SampleRandomVector(MCsamples)

#Define SROM, determine optimal parameters, store parameters
input_srom = SROM(srom_size, dim)
input_srom.optimize(target, weights=[1, 1, 1], error="SSE")

#Compare the CDFs
pp = Postprocessor(srom, target)
pp.compare_CDFs(saveFig=False)

#Run the model for each input SROM sample:
srom_eols = np.zeros(srom_size)
(srom_samples, srom_probs) = input_srom.get_params()
for i, sample in enumerate(srom_samples):
    srom_eols[i] = model.evaluate(sample)
Example #7
0
variables = [r'$y_{0}$', r'log$C$', r'$n$']
cdf_y_label = True  # Label y axis as "CDF"
plot_dir = "plots"
plot_suffix = "SROM_input_CDF_m"
for m in srom_sizes:
    plot_suffix += "_" + str(m)

# x_tick labels for each variable for clarity.
y0_ticks = ['', '0.245', '', '0.255', '', '0.265', '', '0.275']
log_c_ticks = ['', '-8.8', '', '-8.4', '', '-8.0', '', '-7.6']
n_ticks = ['1.0', '', '1.5', '', '2.0', '', '2.5', '', '3.0']
x_ticks = [y0_ticks, log_c_ticks, n_ticks]

# Load / initialize target random variable from samples:
samples = np.genfromtxt(target_samples)
target = SampleRandomVector(samples)

# Set x limits for each variable based on target:
x_limits = []
for i in range(target._dim):
    limits = [np.min(samples[:, i]), np.max(samples[:, i])]
    x_limits.append(limits)

# Build up srom_size-to-SROM object map for plotting routine.
sroms = OrderedDict()

for srom_size in srom_sizes:

    # Generate SROM from file:
    srom = SROM(srom_size, target._dim)
    srom_filename = "srom_m" + str(srom_size) + ".txt"
Example #8
0
# Initialize model,
model = SpringMass1D(m, state0, t_grid)

# ----------Monte Carlo------------------

# Generate stiffness input samples for Monte Carlo.
num_samples = 5000
stiffness_samples = stiffness_random_variable.draw_random_sample(num_samples)

# Calculate maximum displacement samples using MC simulation.
displacement_samples = np.zeros(num_samples)
for i, stiff in enumerate(stiffness_samples):
    displacement_samples[i] = model.get_max_disp(stiff)

# Get Monte carlo solution as a sample-based random variable:
monte_carlo_solution = SampleRandomVector(displacement_samples)

# -------------SROM-----------------------

# Generate SROM for random stiffness.
srom_size = 10
dim = 1
input_srom = SROM(srom_size, dim)
input_srom.optimize(displacement_samples)

# Compare SROM vs target stiffness distribution:
pp_input = Postprocessor(input_srom, displacement_samples)
pp_input.compare_cdfs()

# Run model to get max displacement for each SROM stiffness sample.
srom_displacements = np.zeros(srom_size)
Example #9
0
#----------Monte Carlo------------------

#Generate stiffness input samples for Monte Carlo
num_samples = 5000

k_samples = stiffness_rv.draw_random_sample(num_samples)
m_samples = mass_rv.draw_random_sample(num_samples)

#Calculate maximum displacement samples using MC simulation
disp_samples = np.zeros(num_samples)
for i in range(num_samples):
    disp_samples[i] = model.get_max_disp(k_samples[i], m_samples[i])

#Get Monte carlo solution as a sample-based random variable:
mc_solution = SampleRandomVector(disp_samples)

#-------------SROM-----------------------

#generate SROM for random vector of stiffness & mass
sromsize = 25
dim = 2

#Assume we only have access to samples in this example and want SROM from them:
km_samples = np.array([k_samples, m_samples]).T
km_random_vector = SampleRandomVector(km_samples)

srom = SROM(sromsize, dim)
srom.optimize(km_random_vector)
(samples, probs) = srom.get_params()
Example #10
0
#Initialize model
model = SpringMass_1D(m, state0, t_grid)

#----------Monte Carlo------------------

#Generate stiffness input samples for Monte Carlo
num_samples = 5000
stiffness_samples = stiffness_rv.draw_random_sample(num_samples)

#Calculate maximum displacement samples using MC simulation
disp_samples = np.zeros(num_samples)
for i, stiff in enumerate(stiffness_samples):
    disp_samples[i] = model.get_max_disp(stiff)

#Get Monte carlo solution as a sample-based random variable:
mc_solution = SampleRandomVector(disp_samples)

#-------------SROM-----------------------

#generate SROM for random stiffness
sromsize = 10
dim = 1
input_srom = SROM(sromsize, dim)
input_srom.optimize(stiffness_rv)

#Compare SROM vs target stiffness distribution:
pp_input = Postprocessor(input_srom, stiffness_rv)
pp_input.compare_CDFs()

#Run model to get max disp for each SROM stiffness sample
srom_disps = np.zeros(sromsize)
Example #11
0
from SROMPy.postprocess import Postprocessor
from SROMPy.srom import SROM
from SROMPy.target import SampleRandomVector

'''
Generate SROM to model input distribution (samples)
'''

# Specify input/output files and SROM optimization parameters.
dim = 3
srom_size = 20
samples_file = "mc_data/input_samples_MC.txt"
outfile = "srom_data/srom_m" + str(srom_size) + ".txt"

# Define target random variable from samples
mc_samples = np.genfromtxt(samples_file)
target = SampleRandomVector(mc_samples)

# Define SROM, determine optimal parameters, store parameters
srom = SROM(srom_size, dim)
srom.optimize(target, weights=[1, 1, 1], error="SSE", num_test_samples=100)

# NOTE - commented out to not overwrite paper data files:
# srom.save_params(outfile)

# Check out the CDFs.
pp = Postprocessor(srom, target)
pp.compare_cdfs(variable_names=[r'$y_{0}$', r'log$C$', r'$n$'])

    ''
]]

x_axis_padding = 5
axis_font_size = 24
label_font_size = 20
legend_font_size = 20
cdf_y_label = True  # Label y axis as "CDF".
plot_dir = "plots"
plot_suffix = "SROM_pwlin_eol_CDF_m"
for m in srom_sizes:
    plot_suffix += "_" + str(m)

# Load / initialize target random variable from samples:
samples = np.genfromtxt(target_samples)
target = SampleRandomVector(samples)

# Build up srom_size-to-SROM object map for plotting routine
sroms = OrderedDict()

for srom_size in srom_sizes:

    # Get EOL SROM Surrogate samples to make SampleRV representation of CDF
    end_of_life_sample_filename = "srom_eol_samples_m" + str(
        srom_size) + ".txt"
    end_of_life_sample_filename = os.path.join(srom_dir,
                                               end_of_life_sample_filename)
    end_of_life_samples = np.genfromtxt(end_of_life_sample_filename)

    sroms[srom_size] = SampleRandomVector(end_of_life_samples)
Example #13
0
from SROMPy.postprocess import Postprocessor
from SROMPy.srom import SROM
from SROMPy.target import SampleRandomVector
'''
Script to generate piecewise constant SROM approximation to EOL and compare it 
with the Monte Carlo solution - step 3. Uses the stored EOL model outputs 
from step 2 and the stored input SROM from step 1.
'''

mc_eol_file = "mc_data/eol_samples_MC.txt"

sromsize = 20

srom_eol_file = "srom_data/srom_eol_m" + str(sromsize) + ".txt"
srom_input_file = "srom_data/srom_m" + str(sromsize) + ".txt"

#Get MC EOL samples
MC_eols = np.genfromtxt(mc_eol_file)

#Get SROM EOL samples & probabilities from input srom
srom_eols = np.genfromtxt(srom_eol_file)
srom_probs = np.genfromtxt(srom_input_file)[:, -1]  #probs in last column

#Make MC random variable & SROM to compare
eol_srom = SROM(sromsize, dim=1)
eol_srom.set_params(srom_eols, srom_probs)
eol_mc = SampleRandomVector(MC_eols)

pp = Postprocessor(eol_srom, eol_mc)
pp.compare_CDFs(variablenames=["EOL"])
Example #14
0
# Get SROM EOL samples, FD samples and input SROM from file.
srom_end_of_life_data = np.genfromtxt(srom_end_of_life_filename)
srom_fd_end_of_life_data = np.genfromtxt(srom_fd_end_of_life_filename)
input_srom = SROM(srom_size, dim)
input_srom.load_params(srom_input_file)

# Get FD step sizes from file (the same for all samples, just pull the first)
# Step sizes chosen as approximately 2% of the median sample value of inputs
step_sizes = [0.0065, 0.083, 0.025]

# Calculate gradient from FiniteDifference class:
gradient = FD.compute_gradient(srom_end_of_life_data, srom_fd_end_of_life_data,
                               step_sizes)

# Create SROM surrogate, sample, and create random variable solution.
surrogate_PWL = SROMSurrogate(input_srom, srom_end_of_life_data, gradient)
srom_end_of_life_samples = surrogate_PWL.sample(monte_carlo_inputs)
solution_PWL = SampleRandomVector(srom_end_of_life_samples)

# Store EOL samples for plotting later:
end_of_life_filename = "srom_data/srom_eol_samples_m" + str(srom_size) + ".txt"
# np.savetxt(end_of_life_filename, srom_eol_samples)
# NOTE - avoid overwriting paper data

# Make MC random variable solution.
end_of_life_monte_carlo = SampleRandomVector(monte_carlo_end_of_life_data)

# Compare solutions.
pp = Postprocessor(solution_PWL, end_of_life_monte_carlo)
pp.compare_cdfs()
from SROMPy.target import SampleRandomVector
'''
Script to generate piecewise constant SROM approximation to EOL and compare it 
with the Monte Carlo solution - step 3. Uses the stored EOL model outputs 
from step 2 and the stored input SROM from step 1.
'''

monte_carlo_end_of_life_filename = "mc_data/eol_samples_MC.txt"

srom_size = 20

srom_end_of_life_filename = "srom_data/srom_eol_m" + str(srom_size) + ".txt"
srom_input_file = "srom_data/srom_m" + str(srom_size) + ".txt"

# Get MC EOL samples.
monte_carlo_end_of_life_data = np.genfromtxt(monte_carlo_end_of_life_filename)

# Get SROM EOL samples & probabilities from input srom.
srom_end_of_life_data = np.genfromtxt(srom_end_of_life_filename)

# Probabilities in last column.
srom_probabilities = np.genfromtxt(srom_input_file)[:, -1]

# Make MC random variable & SROM to compare.
end_of_life_srom = SROM(srom_size, dim=1)
end_of_life_srom.set_params(srom_end_of_life_data, srom_probabilities)
end_of_life_mc = SampleRandomVector(monte_carlo_end_of_life_data)

pp = Postprocessor(end_of_life_srom, end_of_life_mc)
pp.compare_cdfs(variable_names=["EOL"])
Example #16
0
    ''
]]

xaxispadding = 5
axisfontsize = 24
labelfontsize = 20
legendfontsize = 20
cdfylabel = True  #Label y axis as "CDF"
plot_dir = "plots"
plot_suffix = "SROM_pwlin_eol_CDF_m"
for m in sromsizes:
    plot_suffix += "_" + str(m)

#Load / initialize target random variable from samples:
samples = np.genfromtxt(targetsamples)
target = SampleRandomVector(samples)

#Build up sromsize-to-SROM object map for plotting routine
sroms = OrderedDict()

for sromsize in sromsizes:

    #Get EOL SROM Surrogate samples to make SampleRV representation of CDF
    eolsamplefile = "srom_eol_samples_m" + str(sromsize) + ".txt"
    eolsamplefile = os.path.join(srom_dir, eolsamplefile)
    eolsamples = np.genfromtxt(eolsamplefile)

    sroms[sromsize] = SampleRandomVector(eolsamples)

Postprocessor.compare_srom_CDFs(sroms,
                                target,