SHARE
TWEET

Untitled

gab709 Jan 22nd, 2020 81 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(w):
  8.     y = w[0]**2+w[1]**2
  9.     return y
  10.  
  11. def g(w):
  12.     y = 2*w
  13.     return y
  14.  
  15.  
  16. model = gp.Model("qp")
  17. model.setParam("PSDTol", float('Inf'))
  18.  
  19. w1 = model.addVar(lb=-5, ub=5, name="w1", vtype=GRB.CONTINUOUS)#2 pesi
  20. w2 = model.addVar(lb=-5, ub=5, name="w2", vtype=GRB.CONTINUOUS)
  21. w=np.array([w1,w2])
  22.  
  23.  
  24. v = model.addVar(lb=-5, ub=5, name="v", vtype=GRB.CONTINUOUS)# errore DEVE essere uno
  25.  
  26. model.setObjective(v,GRB.MINIMIZE)#si deve minimizzare v
  27.  
  28. wi=np.array([-1,-1])
  29. for i in range(20):
  30.     #calcolo f(w) e g(w) con i pesi iniziali e poi quelli aggiornati
  31.     fwi=f(wi)
  32.     gwi=g(wi)
  33.  
  34.     #aggiunta vincolo quindi aggiornamento dei bundle
  35.     contr =  v >= np.dot(gwi, (w-wi))+fwi
  36.     model.addConstr(contr, str(i))
  37.    
  38.     model.optimize()
  39.  
  40.     #print soluzioni
  41.     for v in model.getVars():
  42.         #print variabili
  43.         print('%s %g' % (v.varName, v.x))
  44.  
  45.     #aggiornamento pesi
  46.     wi=np.array([model.getVarByName("w1").x, model.getVarByName("w2").x])
  47.  
  48.     #print funzione
  49.     print('Obj: %g' % model.getObjective().getValue())
  50.  
  51. #wi punto di minimo trovato nel bundle
  52. min_val=f(wi)
  53. print("Valore di minimo finale")
  54. print(min_val)
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