JuMP - Update constraint with matrix coefficient

Hi everyone :grinning:

I’m trying to iteratively solve an optimisation problem in which most of the elements (e.g. objective function and constraints) remain the same over the iterations, but the constraint below needs to be updated with recent data,

@constraint(model, con, [Up; Uf] .== H*alpha)

where Uf and alpha are a 32- and a 40-elements variables, H is a 40x40 matrix and Up is an 8-element vector.

The problem that I’m solving requires that I update con to change the values of Hu and Up. Currently I’m simply deleting the constraint and adding it again with the updated values, but I imagine this isn’t efficient. I tried updating H using the approach suggested in the JuMP manual (as shown in the MWE below), but with no success.

What is the best approach to deal with this kind of constraint? The approach I’m currently using works, but I don’t believe deleting and recreating constraints is efficient. For context, I’m recent to Julia and I’ve been using matlab for years.

using JuMP, Ipopt

# Define data

H = [0.8285911616142245 0.9694396491365609 0.996852329094453 0.9474374188905399 0.8495658863289253 0.8150567337579983 0.8260950501765646 0.7772990625211669 0.7973177427014393 0.9945923412587303 0.8344606810190933 0.931870019332437 0.9071740895323583 0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598;

0.9539970200614036 0.8594167420580591 0.8729573584147194 0.8525658050195425 0.8168659134775795 0.8886771634228199 0.8289103960032675 0.9564270324691854 0.8612823481061153 0.7654838670166808 0.9180474580264514 0.7961762292295225 0.8687389629392916 0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411;

0.9694396491365609 0.996852329094453 0.9474374188905399 0.8495658863289253 0.8150567337579983 0.8260950501765646 0.7772990625211669 0.7973177427014393 0.9945923412587303 0.8344606810190933 0.931870019332437 0.9071740895323583 0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996;

0.8594167420580591 0.8729573584147194 0.8525658050195425 0.8168659134775795 0.8886771634228199 0.8289103960032675 0.9564270324691854 0.8612823481061153 0.7654838670166808 0.9180474580264514 0.7961762292295225 0.8687389629392916 0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509;

0.996852329094453 0.9474374188905399 0.8495658863289253 0.8150567337579983 0.8260950501765646 0.7772990625211669 0.7973177427014393 0.9945923412587303 0.8344606810190933 0.931870019332437 0.9071740895323583 0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045;

0.8729573584147194 0.8525658050195425 0.8168659134775795 0.8886771634228199 0.8289103960032675 0.9564270324691854 0.8612823481061153 0.7654838670166808 0.9180474580264514 0.7961762292295225 0.8687389629392916 0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532;

0.9474374188905399 0.8495658863289253 0.8150567337579983 0.8260950501765646 0.7772990625211669 0.7973177427014393 0.9945923412587303 0.8344606810190933 0.931870019332437 0.9071740895323583 0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862;

0.8525658050195425 0.8168659134775795 0.8886771634228199 0.8289103960032675 0.9564270324691854 0.8612823481061153 0.7654838670166808 0.9180474580264514 0.7961762292295225 0.8687389629392916 0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151;

0.8495658863289253 0.8150567337579983 0.8260950501765646 0.7772990625211669 0.7973177427014393 0.9945923412587303 0.8344606810190933 0.931870019332437 0.9071740895323583 0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943;

0.8168659134775795 0.8886771634228199 0.8289103960032675 0.9564270324691854 0.8612823481061153 0.7654838670166808 0.9180474580264514 0.7961762292295225 0.8687389629392916 0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129;

0.8150567337579983 0.8260950501765646 0.7772990625211669 0.7973177427014393 0.9945923412587303 0.8344606810190933 0.931870019332437 0.9071740895323583 0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253;

0.8886771634228199 0.8289103960032675 0.9564270324691854 0.8612823481061153 0.7654838670166808 0.9180474580264514 0.7961762292295225 0.8687389629392916 0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947;

0.8260950501765646 0.7772990625211669 0.7973177427014393 0.9945923412587303 0.8344606810190933 0.931870019332437 0.9071740895323583 0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779;

0.8289103960032675 0.9564270324691854 0.8612823481061153 0.7654838670166808 0.9180474580264514 0.7961762292295225 0.8687389629392916 0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632;

0.7772990625211669 0.7973177427014393 0.9945923412587303 0.8344606810190933 0.931870019332437 0.9071740895323583 0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779 0.8695763201938923;

0.9564270324691854 0.8612823481061153 0.7654838670166808 0.9180474580264514 0.7961762292295225 0.8687389629392916 0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632 0.9161052325816541;

0.7973177427014393 0.9945923412587303 0.8344606810190933 0.931870019332437 0.9071740895323583 0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779 0.8695763201938923 0.8659216865657627;

0.8612823481061153 0.7654838670166808 0.9180474580264514 0.7961762292295225 0.8687389629392916 0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632 0.9161052325816541 0.8852443550169791;

0.9945923412587303 0.8344606810190933 0.931870019332437 0.9071740895323583 0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779 0.8695763201938923 0.8659216865657627 0.868862755449324;

0.7654838670166808 0.9180474580264514 0.7961762292295225 0.8687389629392916 0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632 0.9161052325816541 0.8852443550169791 0.8931468436763589;

0.8344606810190933 0.931870019332437 0.9071740895323583 0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779 0.8695763201938923 0.8659216865657627 0.868862755449324 0.9883561956306676;

0.9180474580264514 0.7961762292295225 0.8687389629392916 0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632 0.9161052325816541 0.8852443550169791 0.8931468436763589 0.8781365642130943;

0.931870019332437 0.9071740895323583 0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779 0.8695763201938923 0.8659216865657627 0.868862755449324 0.9883561956306676 0.7952361023128759;

0.7961762292295225 0.8687389629392916 0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632 0.9161052325816541 0.8852443550169791 0.8931468436763589 0.8781365642130943 0.8548932902085405;

0.9071740895323583 0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779 0.8695763201938923 0.8659216865657627 0.868862755449324 0.9883561956306676 0.7952361023128759 0.7660429807540107;

0.8687389629392916 0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632 0.9161052325816541 0.8852443550169791 0.8931468436763589 0.8781365642130943 0.8548932902085405 0.8944470652932451;

0.9416046739798282 0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779 0.8695763201938923 0.8659216865657627 0.868862755449324 0.9883561956306676 0.7952361023128759 0.7660429807540107 0.77896651943102;

0.8365432531760428 0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632 0.9161052325816541 0.8852443550169791 0.8931468436763589 0.8781365642130943 0.8548932902085405 0.8944470652932451 0.7883231727220852;

0.9740618842487063 0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779 0.8695763201938923 0.8659216865657627 0.868862755449324 0.9883561956306676 0.7952361023128759 0.7660429807540107 0.77896651943102 0.8369433798664419;

0.9012583240971035 0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632 0.9161052325816541 0.8852443550169791 0.8931468436763589 0.8781365642130943 0.8548932902085405 0.8944470652932451 0.7883231727220852 0.9042818935189274;

0.9139442278758516 0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779 0.8695763201938923 0.8659216865657627 0.868862755449324 0.9883561956306676 0.7952361023128759 0.7660429807540107 0.77896651943102 0.8369433798664419 0.8953794455524079;

0.8200754543441915 0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632 0.9161052325816541 0.8852443550169791 0.8931468436763589 0.8781365642130943 0.8548932902085405 0.8944470652932451 0.7883231727220852 0.9042818935189274 0.9868174276620444;

0.8663661450350677 0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779 0.8695763201938923 0.8659216865657627 0.868862755449324 0.9883561956306676 0.7952361023128759 0.7660429807540107 0.77896651943102 0.8369433798664419 0.8953794455524079 0.9925449686431893;

0.9182587236776176 0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632 0.9161052325816541 0.8852443550169791 0.8931468436763589 0.8781365642130943 0.8548932902085405 0.8944470652932451 0.7883231727220852 0.9042818935189274 0.9868174276620444 0.8140023455985446;

0.9925961858840693 0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779 0.8695763201938923 0.8659216865657627 0.868862755449324 0.9883561956306676 0.7952361023128759 0.7660429807540107 0.77896651943102 0.8369433798664419 0.8953794455524079 0.9925449686431893 0.7824420769757024;

0.8574623560327377 0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632 0.9161052325816541 0.8852443550169791 0.8931468436763589 0.8781365642130943 0.8548932902085405 0.8944470652932451 0.7883231727220852 0.9042818935189274 0.9868174276620444 0.8140023455985446 0.8579485145032006;

0.9104695506113016 0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779 0.8695763201938923 0.8659216865657627 0.868862755449324 0.9883561956306676 0.7952361023128759 0.7660429807540107 0.77896651943102 0.8369433798664419 0.8953794455524079 0.9925449686431893 0.7824420769757024 0.8439499340776127;

0.8630148473758418 0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632 0.9161052325816541 0.8852443550169791 0.8931468436763589 0.8781365642130943 0.8548932902085405 0.8944470652932451 0.7883231727220852 0.9042818935189274 0.9868174276620444 0.8140023455985446 0.8579485145032006 0.7678354128386425;

0.955626341337954 0.8905203035732796 0.9236729891592618 0.9231337398310591 0.8601964717750347 0.8940804578347381 0.8564549107390229 0.9370433932915307 0.9531399258646586 0.9129130002223945 0.8584933163821729 0.7794211846795046 0.9884906960557989 0.9810616877909453 0.8880263785776568 0.9364794022483661 0.8836993987284949 0.8963165954788619 0.8267954109061333 0.8914087646394822 0.7544235191129598 0.7795282536457996 0.794756164381045 0.7677226906095862 0.7604051785110943 0.9990283662633253 0.9915596837444779 0.8695763201938923 0.8659216865657627 0.868862755449324 0.9883561956306676 0.7952361023128759 0.7660429807540107 0.77896651943102 0.8369433798664419 0.8953794455524079 0.9925449686431893 0.7824420769757024 0.8439499340776127 0.8834607353171732;

0.9226412263866202 0.829190286369325 0.8582247457091453 0.7850996141145684 0.9210659955993652 0.8149424805471573 0.9936706127423657 0.9817343180288739 0.8098102709120388 0.9619507253979593 0.8056733382682834 0.8597996130713885 0.8836904797696481 0.9788983402943187 0.7624329108667968 0.7918710283473316 0.9112509606146643 0.8488369569819281 0.8311318732274775 0.8245328237769585 0.9299469955146411 0.8763313557580509 0.8497012392917532 0.9658652611753151 0.7866559037938129 0.9285886543912947 0.9854275534977632 0.9161052325816541 0.8852443550169791 0.8931468436763589 0.8781365642130943 0.8548932902085405 0.8944470652932451 0.7883231727220852 0.9042818935189274 0.9868174276620444 0.8140023455985446 0.8579485145032006 0.7678354128386425 0.8624293100949555]

Up = [0.9311195438007046; 0.9526382891718077; 0.9895185275976004; 0.7671660605112687; 0.758144332973139; 0.8699801876312837; 0.921090625605257; 0.9981662594972459]

# Cretae model

model = Model(Ipopt.Optimizer; add_bridges = false)

@variable(model, Uf[1:32])

@variable(model,alpha[1:40])

@constraint(model, con, [Up; Uf] .== H*alpha)

# Update H

H = 2*H

set_normalized_coefficient(con, alpha, H)

Hi @luccafaro, welcome to the forum :smile:

A few comments.

About set_normalized_coefficient

The first is that set_normalized_coefficient(con, alpha, H) doesn’t work because con is a Vector of constraints. In general, if you encounter a MethodError in Julia because you are trying to apply a scalar operation over a vector:

julia> set_normalized_coefficient(con, alpha, H)
ERROR: MethodError: no method matching set_normalized_coefficient(::Vector{ConstraintRef{…}}, ::Vector{VariableRef}, ::Matrix{Float64})

you need to use Julia’s broadcasting:

set_normalized_coefficient.(con, alpha, H)

Note the .

However, this isn’t quite correct, because of the normalized part of the function name. JuMP will normalize all constraints by moving the variable terms to the left-hand side. Thus, your normalized constraint is really:

@constraint(model, con, [Up; Uf] .- H * alpha .== 0)

so you need to do

set_normalized_coefficient.(con, alpha, -H)

However, this call is a bit complicated, because you’re broadcasting over a vector of constraints (con) and a vector of variables (alpha) and a matrix of numbers (H).

Because you need con down the rows of H and alpha across the columns of H, you need to transpose the alpha vector so that it is a row. That gives:

set_normalized_coefficient.(con, alpha', -H)

Using broadcasting with a mix of shapes can be tricky to get correct. Note that Julia is not MATLAB, so loops are not slower than vectorized code. You could just write instead:

for i in eachindex(con)
    for j in eachindex(alpha)
        set_normalized_coefficient(con[i], alpha[j], -H[i, j])
    end
end

Using parameters

However, there’s a much easier (and in this case, more efficient way): use Nonlinear Modeling · JuMP

model = Model(Ipopt.Optimizer)
@variable(model, Uf[1:32])
@variable(model, alpha[1:40])
@variable(model, H_param[i in 1:40, j in 1:40] in Parameter(H[i, j]))
@constraint(model, con, [Up; Uf] .== H_param * alpha)
optimize!(model)
@assert is_solved_and_feasible(model)
set_parameter_value.(H_param, 2 * H)
optimize!(model)
@assert is_solved_and_feasible(model)
4 Likes

Thank you for you (incredibly fast) response! I just implemented your suggestion and it was exactly what I needed. I was able to significantly reduce the memory usage of my program. Your explanations were also very helpful and truly appreciated :grinning:

2 Likes