Skip to content

Commit

Permalink
update warmstart
Browse files Browse the repository at this point in the history
  • Loading branch information
ZedongPeng committed Nov 2, 2023
1 parent c16d4b7 commit 973c928
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions pyomo/contrib/mindtpy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
from pyomo.solvers.plugins.solvers.gurobi_persistent import GurobiPersistent
import math
from itertools import permutations, product
import pickle
import pickle
import numpy as np

pyomo_nlp = attempt_import('pyomo.contrib.pynumero.interfaces.pyomo_nlp')[0]
numpy = attempt_import('numpy')[0]
Expand Down Expand Up @@ -971,6 +972,8 @@ def generate_norm_constraint(fp_nlp_model, mip_model, config):
def customized_initialize(mod):
with open("rotary_bed_unit_FIM", 'rb') as f:
unit_fim_list = pickle.load(f)

new_fim = np.zeros((5,5))

### compute FIM
def eval_fim2(m, a, b):
Expand All @@ -985,40 +988,39 @@ def eval_fim2(m, a, b):
for i in m.n_responses:
for j in m.n_responses:
if j>=i:
summi += m.cov_y[i,j]*unit_fim_list[i*m.num_measure_dynamic_flatten+j][a][b]
summi += m.cov_y[i,j].value*unit_fim_list[i*m.num_measure_dynamic_flatten+j][a][b]
else:
summi += m.cov_y[j,i]*unit_fim_list[i*m.num_measure_dynamic_flatten+j][a][b]


summi += m.cov_y[j,i].value*unit_fim_list[i*m.num_measure_dynamic_flatten+j][a][b]
return summi
else:
summi = 0
for i in m.n_responses:
for j in m.n_responses:
if j>=i:
summi += m.cov_y[i,j]*unit_fim_list[i*m.num_measure_dynamic_flatten+j][b][a]
summi += m.cov_y[i,j].value*unit_fim_list[i*m.num_measure_dynamic_flatten+j][b][a]
else:
summi += m.cov_y[j,i]*unit_fim_list[i*m.num_measure_dynamic_flatten+j][b][a]


summi += m.cov_y[j,i].value*unit_fim_list[i*m.num_measure_dynamic_flatten+j][b][a]
return summi

#mod.dynamic_initial_FIM = pyo.Expression(mod.DimFIM_half, rule=eval_fim2)


for a in mod.DimFIM:
for b in mod.DimFIM:
if a<=b:
dynamic_initial_element = eval_fim2(mod, a,b)
mod.TotalFIM[a,b].value = dynamic_initial_element
#new_fim[a,b] = dynamic_initial_element
new_fim[a,b] = dynamic_initial_element
new_fim[b,a] = dynamic_initial_element

### update greybox
#grey_box_name = "ele_"+str(a)+"_"+str(b)
#mod.my_block.egb.inputs[grey_box_name].value = dynamic_initial_element
grey_box_name = "ele_"+str(a)+"_"+str(b)
mod.my_block.egb.inputs[grey_box_name].value = dynamic_initial_element

# initialize determinant
_, det = np.linalg.slogdet(new_fim)
mod.my_block.egb.outputs["log_det"] = det

# manual and dynamic install initial
def total_dynamic_exp(m):
return sum(m.cov_y[i,i] for i in range(m.n_static_measurements, m.num_measure_dynamic_flatten))
return sum(m.cov_y[i,i].value for i in range(m.n_static_measurements, m.num_measure_dynamic_flatten))

total_dynamic_initial = total_dynamic_exp(mod)
mod.total_number_dynamic_measurements.value = total_dynamic_initial
Expand All @@ -1028,10 +1030,9 @@ def cost_compute(m):
"""Compute cost
cost = static-cost measurement cost + dynamic-cost measurement installation cost + dynamic-cost meausrement timepoint cost
"""
return sum(m.cov_y[i,i]*m.cost_list[i] for i in m.n_responses)+sum(m.if_install_dynamic[j]*m.dynamic_install_cost[j-m.n_static_measurements] for j in m.DimDynamic)
return sum(m.cov_y[i,i].value*m.cost_list[i] for i in m.n_responses)+sum(m.if_install_dynamic[j].value*m.dynamic_install_cost[j-m.n_static_measurements] for j in m.DimDynamic)

cost_init = cost_compute(mod)
mod.cost.value = cost_init

return mod

0 comments on commit 973c928

Please sign in to comment.