Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Calo Opt_Nikhil
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package registry
Operate
Terraform modules
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Krishna Krishna Nikhil
Calo Opt_Nikhil
Commits
8196821d
Commit
8196821d
authored
10 months ago
by
Krishna Krishna Nikhil
Browse files
Options
Downloads
Patches
Plain Diff
Update optimizer.py
parent
c14d6326
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
modules/optimizer.py
+62
-10
62 additions, 10 deletions
modules/optimizer.py
with
62 additions
and
10 deletions
modules/optimizer.py
+
62
−
10
View file @
8196821d
...
...
@@ -34,7 +34,6 @@ class Optimizer(object):
self
.
to
(
self
.
device
)
def
to
(
self
,
device
):
self
.
device
=
device
self
.
surrogate_model
.
to
(
device
)
...
...
@@ -42,27 +41,80 @@ class Optimizer(object):
def
other_constraints
(
self
,
dataset
):
# keep the size of the detector within 3m
raw_detector_parameters
=
self
.
detector_parameters
raw_detector_parameters
=
self
.
detector_parameters
# this creates a loss that increases starting from 3 meters using relu
detector_length
=
torch
.
sum
(
raw_detector_parameters
)
detector_length
=
torch
.
sum
(
raw_detector_parameters
[:
6
])
# changed this here so as to not include material choice in this
thickness_parameters
=
self
.
detector_parameters
[
0
:
6
]
material_parameters
=
self
.
detector_parameters
[
6
:
12
]
abs_parameters
=
self
.
detector_parameters
[
6
:
9
]
scint_parameters
=
self
.
detector_parameters
[
9
:
12
]
# cost_dic = {"G4_POLYSTYRENE": 0.001
# ,"G4_PLASTIC_SC_VINYLTOLUENE": 0.001
# ,"G4_BGO": 666.66
# ,"G4_LSO": 5000
# ,"G4_LYSO": 75000
# ,"G4_CESIUM_IODIDE": 0.001
# ,"G4_Pb": 25
# ,"G4_Fe": 4.166
# ,"G4_W": 500
# ,"G4_Cu": 66.66
# ,"G4_BRASS": 16.66
# ,"G4_Si": 8330000
# ,"G4_PbWO4": 2500 }
if
self
.
constraints
is
not
None
:
if
'
length
'
in
self
.
constraints
:
detector_length
=
torch
.
sum
(
raw_detector_parameters
)
detector_length
=
torch
.
sum
(
raw_detector_parameters
[:
6
])
# changed this here so as to not include material choice in this
total_length_loss
=
torch
.
mean
(
100.
*
torch
.
nn
.
ReLU
()(
detector_length
-
self
.
constraints
[
'
length
'
])
**
2
)
#constrain it to 25cm
if
'
lower
'
in
self
.
constraints
:
lbound
=
(
torch
.
ones
(
12
)
*
self
.
constraints
[
'
lower
'
]).
to
(
self
.
device
)
lower_loss
=
torch
.
mean
(
1000.
*
torch
.
nn
.
ReLU
()(
lbound
-
raw_detector_parameters
)
**
2
)
if
'
upper
'
in
self
.
constraints
:
ubound
=
(
torch
.
ones
(
6
)
*
self
.
constraints
[
'
upper
'
]).
to
(
self
.
device
)
upper_loss
=
torch
.
mean
(
100.
*
torch
.
nn
.
ReLU
()(
material_parameters
-
ubound
)
**
2
)
if
'
diff
'
in
self
.
constraints
:
abs_maxdiff
=
max
(
abs_parameters
)
-
min
(
abs_parameters
)
scint_maxdiff
=
max
(
scint_parameters
)
-
min
(
scint_parameters
)
diff_loss
=
torch
.
mean
(
100.
*
torch
.
nn
.
ReLU
()(
abs_maxdiff
-
self
.
constraints
[
'
diff
'
])
**
2
)
diff_loss
+=
torch
.
mean
(
100.
*
torch
.
nn
.
ReLU
()(
scint_maxdiff
-
self
.
constraints
[
'
diff
'
])
**
2
)
if
'
cost
'
in
self
.
constraints
:
abs_mask
=
(
abs_parameters
>=
0.5
)
cost_abs
=
torch
.
where
(
abs_mask
,
25
,
4.166
)
scint_mask
=
(
scint_parameters
>=
0.5
)
cost_scint
=
torch
.
where
(
scint_mask
,
2500
,
0.001
)
combined
=
torch
.
cat
((
cost_abs
,
cost_scint
),
dim
=
0
)
combined_cost
=
combined
.
to
(
self
.
device
)
# bounded_thickness = torch.tensor(thickness_parameters)
# bounded_thickness = torch.where(bounded_thickness > 0, bounded_thickness, 0)
cost
=
torch
.
sum
(
combined_cost
*
thickness_parameters
)
cost_loss
=
torch
.
mean
(
50.
*
torch
.
nn
.
ReLU
()(
cost
-
self
.
constraints
[
'
cost
'
])
**
2
)
# now keep parameters such that within the box size of the generator, there are always some positive values even if the
# central parameters are negative. Both box size and raw_detector_parameters are in non-normalised space, so this is straight forward
# the generator will have to provide the box size
# this will avoid mode collapse
box
=
self
.
cu_box
# the box has same ordering as raw_detector_parameters
lower_para_bound
=
-
box
/
1.1
bloss
=
torch
.
mean
(
100.
*
torch
.
nn
.
ReLU
()(
lower_para_bound
-
raw_detector_parameters
)
**
2
)
# box = self.cu_box # the box has same ordering as raw_detector_parameters
# lower_para_bound = -box/1.1
# bloss = torch.mean(100.*torch.nn.ReLU()(lower_para_bound - raw_detector_parameters)**2)
# + bloss
return
total_length_loss
+
b
loss
return
total_length_loss
+
lower_loss
+
upper_loss
+
diff_loss
+
cost_
loss
def
clamp_parameters
(
self
):
return
#for now
return
self
.
detector_parameters
=
self
.
detector_parameters
.
clamp
(
1e-3
)
#DEBUG, NE
def
adjust_generator_covariance
(
self
,
direction
,
min_scale
=
2.0
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment