SHARE
TWEET

Untitled

gab709 Jan 21st, 2020 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import gurobipy as gp
  3. from gurobipy import GRB
  4. from pynverse import inversefunc
  5. import copy
  6.  
  7. def f(wi):
  8.     y = wi**2
  9.     return y
  10.  
  11. def g(wi):
  12.     y = 2*wi
  13.     return y
  14.  
  15.  
  16. model = gp.Model("qp")
  17. model.setParam("PSDTol", float('Inf'))
  18.  
  19. w = model.addVar(lb=-5, ub=5, name="w", vtype=GRB.CONTINUOUS)
  20. v = model.addVar(lb=-5, ub=5, name="v", vtype=GRB.CONTINUOUS)
  21.  
  22. model.setObjective(v,GRB.MINIMIZE)#si deve minimizzare v
  23.  
  24. wi=-1
  25. for i in range(20):
  26.     #calcolo f(w) e g(w) con i pesi iniziali e poi quelli aggiornati
  27.     fwi=f(wi)
  28.     gwi=g(wi)
  29.  
  30.     #aggiunta vincolo quindi aggiornamento dei bundle
  31.     contr =  v >= gwi*(w-wi)+fwi
  32.     model.addConstr(contr, str(i))
  33.    
  34.     model.optimize()
  35.  
  36.     for v in model.getVars():
  37.         #print variabili
  38.         print('%s %g' % (v.varName, v.x))
  39.         #aggiornamento pesi
  40.         if(v.varName=="w"):
  41.             wi=v.x
  42.     #print funzione
  43.     print('Obj: %g' % model.getObjective().getValue())
  44.  
  45. #wi punto di minimo trovato nel bundle
  46. min_val=f(wi)
  47. print("Valore di minimo finale")
  48. print(min_val)
  49.  
  50.  
  51.  
  52.  
  53.  
  54. #######################################################################################
  55. '''PRIMA PROVA
  56. wi1=-1
  57.  
  58. fwi1=f(wi1)
  59. gwi1=g(wi1)
  60.  
  61. partcontr =  v >= gwi1*(w-wi1)+fwi1
  62.    
  63. model.addConstr(partcontr, "sa")
  64.  
  65. wi2=5
  66.  
  67. fwi2=f(wi2)
  68. gwi2=g(wi2)
  69.  
  70. partcontr =  v >= gwi2*(w-wi2)+fwi2
  71.    
  72. model.addConstr(partcontr, "s")
  73.  
  74. model.optimize()
  75.  
  76. for v in model.getVars():
  77.     print('%s %g' % (v.varName, v.w))
  78.  
  79. print('Obj: %g' % model.getObjective().getValue())
  80. '''
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top