Simplex Optimizer Start Point

Moderators: cyao, michael_borland

Post Reply
Brendan
Posts: 2
Joined: 26 Oct 2017, 16:18

Simplex Optimizer Start Point

Post by Brendan » 26 Oct 2017, 16:54

Hi,

Short question: Is there a way to change K1 for a quad using the -macro function? Or change the initial value used (in optimization_variable I suppose) when performing simplex optimization?

Long question:
I'm trying to optimize a set of four quads. A simple problem of take in twiss parameters and match them to some desired output set of twiss parameters. I am finding that depending on the settings of the quad K1 values in the lattice file the simplex optimizer will either find a really good match (FDFD, optimization function has value 2.16018011905043e-11) or really bad match (FFFF, optimization function has value 1.129994256216170e-01). The latter result seems immune to increasing n_passes, n_evaluations or n_restarts. It appears elegant is using the K1 values in the .lte file to start the search. What I'd like to be able to do is use a macro to iterate over setting the start points when starting the simplex optimizer. Is this possible? The documentation for optimize_variable doesn't seem to have that option:

Code: Select all

&optimization_variable
STRING name = NULL;
STRING item = NULL;
double lower_limit = 0;
double upper_limit = 0;
double step_size = 1;
long disable = 0;
long force_inside = 0;
&end


Other options I've tried:
include_simplex_1d_scans = 1,
simplex_pass_range_factor = 0.001, 0.01, 0.1, 1, 10
simplex_divisor = 0.01, 0.1, 1, 10
restart_worst_term_factor = 2, 5, 10
and I can't get it to change the signs on the quads appropriately. I've tried randomwalk and grid but it will be much faster to go through all 16 permutations of the possible start values than it would to wait for those guys to search - if I could get them to the right answer anyway. I haven't spent too much time on them and I really like the -macro function so I was hoping to find a solution using it.

.lte file

Code: Select all

! 4 Quads, no LSC stuff, for inital optimization work.

L1: DRIF,L=14.5843
L2: DRIF,L=6.39492
L3: DRIF,L=8.14492
L4: DRIF,L=4.14492
L5: DRIF,L=7.07246

QUM1: QUAD,L= 1.580000000E-01,K1=0.5 ! change me to -0.5 for good opt.
QUM2: QUAD,L= 1.580000000E-01,K1=0.5
QUM3: QUAD,L= 1.580000000E-01,K1=0.5 ! change me to -0.5 for good opt.
QUM4: QUAD,L= 1.580000000E-01,K1=0.5


UndMatch: LINE = (L1, QUM1, QUM1, L2, QUM2, QUM2, &
  L3, QUM3, QUM3, L4, QUM4, QUM4, L5)

.ele file:

Code: Select all

! A simple 4 quad match


! ----------------------------------
! Setup the optimizer
! ----------------------------------

&run_setup
        lattice = simple_4_quad.lte,
        default_order = 1,
        use_beamline = UndMatch,
        p_central_mev = 8000,
        final = %s.fin,
&end

&twiss_output
        matched = 0,
        beta_x = 30.7723, alpha_x = -0.8882 ,
        beta_y = 63.39, alpha_y = 1.1676,
        output_at_each_step=0,
&end

&run_control
        n_steps = 1
&end


&optimization_setup
	mode = "minimize", 
	method = "simplex",
    target = 1e-10,
    tolerance = 1e-16, 
    n_passes = 5,
    n_evaluations = 500, 
    log_file = /dev/tty, 
    n_restarts = 10,
    verbose = 0, 
    output_sparsing_factor = 10,
&end


&optimization_term
	term = "betax 34.35574 - sqr"
&end

&optimization_term
	term = "alphax 1.432320 - sqr"
&end

&optimization_term
	term = "betay 30.15917 - sqr"
&end

&optimization_term
	term = "alphay -0.9522622 - sqr"
&end

&optimization_variable
        name = QUM1, item=K1, lower_limit=-1.1859, upper_limit=1.1859, step_size = 0.001 
&end
&optimization_variable
        name = QUM2, item=K1, lower_limit=-1.1859, upper_limit=1.1859, step_size = 0.001 
&end
&optimization_variable
        name = QUM3, item=K1, lower_limit=-1.1859, upper_limit=1.1859, step_size = 0.001 
&end
&optimization_variable
        name = QUM4, item=K1, lower_limit=-1.1859, upper_limit=1.1859, step_size = 0.001 
&end

&bunched_beam
        n_particles_per_bunch = 1
&end
 
&optimize summarize_setup=0 
&end

&save_lattice 
	filename = %s.new 
&end

! ----------------------------------
! Run the guess beam through the (potentially) optimized lattice
! ----------------------------------
&run_setup
        lattice = simple_4_quad.new,
        default_order = 1,
        use_beamline = UndMatch,
        p_central_mev = 8000,
        parameters = %s.param
        magnets = %s.mag
&end

&twiss_output
        matched = 0,
        beta_x = 30.7723, alpha_x = -0.8882 ,
        beta_y = 63.39, alpha_y = 1.1676,
        statistics = 1,
        filename = %s.twi
&end

&run_control
        n_steps = 1
&end

! ----------------------------------
! Run elegant and...other stuff?
! ----------------------------------

&bunched_beam &end

&track &end

&stop &end


michael_borland
Posts: 1959
Joined: 19 May 2008, 09:33
Location: Argonne National Laboratory
Contact:

Re: Simplex Optimizer Start Point

Post by michael_borland » 26 Oct 2017, 17:09

Brendan,

The answer to the simple question is: you can use the alter_elements command.

On the commandline, use

Code: Select all

elegant input.ele -macro=newK1=42.0
In the .ele file, use

Code: Select all

&alter_elements name=Q1, item=K1, value=<newK1> &end
just after your run_setup command.

Let me know if this doesn't solve your problem.

--Michael

Brendan
Posts: 2
Joined: 26 Oct 2017, 16:18

Re: Simplex Optimizer Start Point

Post by Brendan » 31 Oct 2017, 11:21

Hi Michael,

That did the trick. Thank you!

Brendan

Biaobin
Posts: 29
Joined: 17 Feb 2017, 09:03

Re: Simplex Optimizer Start Point

Post by Biaobin » 22 Jan 2018, 19:27

Hi Michael,

As simplex method will depend on the start points, so I want to generate the random start values using several alter_elements combined with rpnl command:

Code: Select all

&alter_elements name=Q1H, item=K1, value={rpnl "rnd -1 * rnd + 20 *"} &end
&alter_elements name=Q2H, item=K1, value={rpnl "rnd -1 * rnd + 20 *"} &end
&alter_elements name=Q3H, item=K1, value={rpnl "rnd -1 * rnd + 20 *"} &end
&alter_elements name=Q4H, item=K1, value={rpnl "rnd -1 * rnd + 20 *"} &end
but it occurs to all the random numbers are exactly the same. Is there any possible method to prevent this ?

The test file modified from betaMatching are attached.

Best,
Biaobin
Attachments
one.lte
(426 Bytes) Downloaded 246 times
one.ele
(1.92 KiB) Downloaded 241 times

michael_borland
Posts: 1959
Joined: 19 May 2008, 09:33
Location: Argonne National Laboratory
Contact:

Re: Simplex Optimizer Start Point

Post by michael_borland » 23 Jan 2018, 09:30

Biaobin,

You can accomplish this using

Code: Select all

&alter_elements name=Q1H, item=K1, value="(rnd -1 * rnd + 20 *)", verbose=1 &end
&alter_elements name=Q2H, item=K1, value="(rnd -1 * rnd + 20 *)", verbose=1 &end
&alter_elements name=Q3H, item=K1, value="(rnd -1 * rnd + 20 *)", verbose=1 &end
&alter_elements name=Q4H, item=K1, value="(rnd -1 * rnd + 20 *)", verbose=1 &end
The difference is that using the {} syntax, each rpnl command is executed in subshell. Since the commands are so fast, the clock-based seed of the random number generator is identical in all cases. Using the () syntax executes the rpn expressions in the main process, using the primary random number generator in elegant.

--Michael

Biaobin
Posts: 29
Joined: 17 Feb 2017, 09:03

Re: Simplex Optimizer Start Point

Post by Biaobin » 23 Jan 2018, 14:25

Hi Michael,

Thanks a lot, it works.

Best,
Biaobin

Post Reply