I have a function called " example1". I would like to evaluate it accross 9 grids until the ratio of the current value over the previous value is between 0.98 and 1.05. Please look the code below:

%%9 dimensional grid

grid1D=[-10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10];

x=[ 1, 2 3, 4, 5 ,6 ,7 ,8, 9];

arguments_2=num2cell(x);

for k=1:9

arguments_2{k}=grid1D(k,:);

end

arguments=num2cell(x);

for k=1:8

if (j >=0.98) & (j <=1.05)

break

else

arguments{k}=arguments_2{k};

for i=1:21

arguments{k}=arguments_2{k}(i)

h(i,:)=example1(arguments{:})

end

[j1, idx]=min(h)

arguments{k}=grid1D(idx)

for i=1:21

arguments{k+1}=arguments_2{k+1}(i)

h(i,:)=example1(arguments{:})

end

[j2, idx]=min(h);

arguments{k+1}=grid1D(idx);

j=j2/j1

end

end

The function "example1" takes approximately 12 hours to run thats why I would like to use parfor. Now I dont know in which loop it is the most efficient to place it in my code above? Any comment would be very helpful. Thanks a lot

Best

S

"Saad " <saad.badaoui07@imperial.ac.uk> wrote in message

news:j35khr$gem$1@newscl01ah.mathworks.com...

> Dear All,

>

> I have a function called " example1". I would like to evaluate it accross

> 9 grids until the ratio of the current value over the previous value is

> between 0.98 and 1.05. Please look the code below:

*snip*

There's no need to repeat yourself; please try to keep all discussion of one

problem in one thread in the future. Thanks.

http://www.mathworks.com/matlabcentral/newsreader/view_thread/311906

Anyway, your statement "the ratio of the current value over the previous

value" suggests to me that the result of your computations is going to

depend on the order of the iterate values for which the loop body is

evaluated. That is incompatible with PARFOR:

http://www.mathworks.com/help/toolbox/distcomp/brb2x2l-1.html#brdqg4q-1

"You cannot use a parfor-loop when an iteration in your loop depends on the

results of other iterations. Each iteration must be independent of all

others."

Why are you trying to brute-force your function? You're going to need to

evaluate it for each of 21^9 points -- that's a LOT of function evaluations

if your function is not vectorized! I _think_ if you were to create custom

creation, mutation, and crossover functions you may be able to use Global

Optimization Toolbox to solve your problem.

http://www.mathworks.com/help/toolbox/gads/f6174dfi10.html#f14223

Alternately, if you clearly describe IN WORDS (NOT equations or code) what

exactly you're trying to do someone may be able to offer a suggestion as to

how to do what you want without brute-force.

--

Steve Lord

slord@mathworks.com

To contact Technical Support use the Contact Us link on

http://www.mathworks.com

> for k=1:8

>

> if (j >=0.98) & (j <=1.05)

=================

Are you sure it makes sense to apply this stopping criterion for every individual k? It's certainly not a good way in general to see if you've found a minimim. For example, if I apply your technique to the minimization of

f(x)=x(1)^2 + x(2)^2 + x(3)^2

starting at x=[0 0 1], then I will get j=1 right from loop iteration k=2 and the algorithm will stop without ever processing x(3).

> arguments{k}=arguments_2{k};

================

This command plays no role in your code and should probably be deleted.

You immediately overwrite

arguments{k} with arguments_2{k}(i) in later lines of code.

> arguments{k}=grid1D(idx)

==============

Shouldn't this be

arguments{k}=grid1D(k,idx);

>

> for i=1:21

>

> arguments{k}=arguments_2{k}(i)

>

> h(i,:)=example1(arguments{:})

>

>

> end

>

> [j1, idx]=min(h)

==========================

It doesn't really make sense that you run this loop twice for every k in order to get both j1 and j2. When the for-loop over k increments and k becomes k+1, your computation of j1 will be identical to your most recent computation of j2.

> The function "example1" takes approximately 12 hours to run thats why I would like to use parfor.

===================

As Steve said, nothing above looks parallelizable. However, 12 hours is a really long time for a function evaluation. Why don't you show us what's inside example1 and we can see if maybe PARFOR can be used to accelerate example1 itself?

> A few miscellaneous remarks:

>

>

> > for k=1:8

> >

> > if (j >=0.98) & (j <=1.05)

> =================

>

> Are you sure it makes sense to apply this stopping criterion for every individual k? It's certainly not a good way in general to see if you've found a minimim. For example, if I apply your technique to the minimization of

>

> f(x)=x(1)^2 + x(2)^2 + x(3)^2

>

> starting at x=[0 0 1], then I will get j=1 right from loop iteration k=2 and the algorithm will stop without ever processing x(3).

>

>

>

>

> > arguments{k}=arguments_2{k};

> ================

>

> This command plays no role in your code and should probably be deleted.

> You immediately overwrite

> arguments{k} with arguments_2{k}(i) in later lines of code.

>

>

>

> > arguments{k}=grid1D(idx)

> ==============

>

> Shouldn't this be

>

> arguments{k}=grid1D(k,idx);

>

>

>

> >

> > for i=1:21

> >

> > arguments{k}=arguments_2{k}(i)

> >

> > h(i,:)=example1(arguments{:})

> >

> >

> > end

> >

> > [j1, idx]=min(h)

> ==========================

>

> It doesn't really make sense that you run this loop twice for every k in order to get both j1 and j2. When the for-loop over k increments and k becomes k+1, your computation of j1 will be identical to your most recent computation of j2.

>

>

>

>

>

>

> > The function "example1" takes approximately 12 hours to run thats why I would like to use parfor.

> ===================

>

> As Steve said, nothing above looks parallelizable. However, 12 hours is a really long time for a function evaluation. Why don't you show us what's inside example1 and we can see if maybe PARFOR can be used to accelerate example1 itself?

Hi Matt and Steven

Apologies for the repetition it wont happen next time. Unfortunately the function example1 is really long and depends on other functions thats why I didnt paste it. To run the function takes up to 12 hours (generally between 5 and 12h max). Another bad news is that the function cannot be vectorized.

Here is what I am doing:

1) I am setting up a 9-D grid: grid1D is a cell.

grid1D=[-10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10]; %% this is just an example. In my study, I will just take 6 values per grid otherwise the number of evaluation will be very high.

2) the function "example1" is function of 9 parameters. So the aim of this exercise is to evaluate "example1" by changing the first parameter (iterating accross grid1D{1}) and keeping the other parameters fixed. Once this operation is done, take the parameter of grid1D{1} that gives the smallest value of "example1" and plug it into the function and move to other grid (grid1D{2}). Iterate accross all the values of this grid and keep the parameter that minimize the function "example1"....I keep doing this until "if" condition is fullfilled.

Now all I am doing is evaluating the same function by changing the parameters (in each grid1D). so In theory this is parallelizable no? Maybe the way I wrote the code above is confusing ? (sorry about that)

Matt, I am not sure but I think the reason why your function quickly fulfilled the "if" condition is because the function that you took as example is quite simple which is not the case for "example1". But I agree it is risky the iteration is risky it might not evaluate all the grids.

3) so basically in my code I am iterating accross the Grids and inside each grid I am iterating accross the values of the grid

As you mentioned PARFOR is not suitable when one iteration is dependent on the others but there is a still a room to use PARFOR in my case no? knowing that all I do is to evaluate the function at different values?

4) Worse case scenario, do you think I can do it manually? I can run the inner loop with PARFOR for grid1{1} ( I iterate accross grid1d{1} )

%%%%Inner loop%%%%

parfor i=1:21

arguments{k}=arguments_2{k}(i)

h(i,:)=example1(arguments{:})

end

[j1, idx]=min(h)

arguments{k}=grid1D(idx)

Store the best parameter and plug it in the function " example1" and then move to grid1D{2} run with parfor again and keep going until the function doesnt change (much)...it might be a silly way but at least it will enable me to use PARFOR to speed up the computation...What do you think?

Matt I also agree with you it might not be an intelligent way to minimize the function but Iam just replication a paper to retrieve the parameters.

Sorry for this long email and thanks a lot for any valuable advice you could give me.

Best Regards

S

"Saad " <saad.badaoui07@imperial.ac.uk> wrote in message

news:j36e8h$f8b$1@newscl01ah.mathworks.com...

> "Matt J" wrote in message <j360ia$rd6$1@newscl01ah.mathworks.com>...

*snip*

> Hi Matt and Steven

>

> Apologies for the repetition it wont happen next time. Unfortunately the

> function example1 is really long and depends on other functions thats why

> I didnt paste it. To run the function takes up to 12 hours (generally

> between 5 and 12h max). Another bad news is that the function cannot be

> vectorized.

How long does ONE execution of your function take?

Have you run one execution through the Profiler to identify where the

bottlenecks are located?

How sure are you that it is impossible to vectorize your function? Is it

simply more difficult to vectorize than you want to do at the moment?

> Here is what I am doing:

>

> 1) I am setting up a 9-D grid: grid1D is a cell.

> grid1D=[-10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10;

> -10:1:10; -10:1:10]; %% this is just an example. In my study, I will just

> take 6 values per grid otherwise the number of evaluation will be very

> high.

Why?

> 2) the function "example1" is function of 9 parameters. So the aim of

> this exercise is to evaluate "example1" by changing the first parameter

> (iterating accross grid1D{1}) and keeping the other parameters fixed. Once

> this operation is done, take the parameter of grid1D{1} that gives the

> smallest value of "example1" and plug it into the function and move to

> other grid (grid1D{2}). Iterate accross all the values of this grid and

> keep the parameter that minimize the function "example1"....I keep doing

> this until "if" condition is fullfilled.

So you don't actually need to evaluate on all 21^9 elements of your 9-D

grid. Instead you'll do something like evaluating at (switching to 4D to

save me some typing)

part 1:

[-10 -10 -10 -10]

[-9 -10 -10 -10]

[-8 -10 -10 -10]

% etc

[10 -10 -10 -10]

and decide for which x the value of the function evaluated at

[x -10 -10 -10] is minimized, then repeat with:

part 2:

% we already know [x -10 -10 -10] from part 1

[x -9 -10 -10]

[x -8 -10 -10]

[x -7 -10 -10]

% etc

[x 10 -10 -10]

part 3:

[x y -9 -10]

% etc

Is this a correct assessment of what you're trying to do?

> Now all I am doing is evaluating the same function by changing the

> parameters (in each grid1D). so In theory this is parallelizable no? Maybe

> the way I wrote the code above is confusing ? (sorry about that)

The evaluation may be parallelizable, but since part 2 depends on the

results of part 1, part 1 must be completed before you can start part 2 and

so on down the line. Thus you could parallelize this in chunks of 20 or 21

functions evaluations being done in parallel; depending on how involved your

function is, the benefit from parallelizing may be good or may be outweighed

by the parallel setup cost.

*snip*

> Matt I also agree with you it might not be an intelligent way to minimize

> the function but Iam just replication a paper to retrieve the parameters.

How did the author of the paper retrieve those parameters -- did they do it

in this same pseudo brute-force way?

--

Steve Lord

slord@mathworks.com

To contact Technical Support use the Contact Us link on

http://www.mathworks.com

>

>

> "Saad " <saad.badaoui07@imperial.ac.uk> wrote in message

> news:j36e8h$f8b$1@newscl01ah.mathworks.com...

> > "Matt J" wrote in message <j360ia$rd6$1@newscl01ah.mathworks.com>...

>

> *snip*

>

> > Hi Matt and Steven

> >

> > Apologies for the repetition it wont happen next time. Unfortunately the

> > function example1 is really long and depends on other functions thats why

> > I didnt paste it. To run the function takes up to 12 hours (generally

> > between 5 and 12h max). Another bad news is that the function cannot be

> > vectorized.

>

> How long does ONE execution of your function take?

> Have you run one execution through the Profiler to identify where the

> bottlenecks are located?

> How sure are you that it is impossible to vectorize your function? Is it

> simply more difficult to vectorize than you want to do at the moment?

>

To be honest, I am not very confident about the fact the function "example1" cannot be vectorized, it is just that the function itself is a sequence of other long functions ( and Iam not sure how to vectorize them at the moment).

Yes I run one execution through Profiler and the speed problem is coming from an optimization (of 5 parameters). because each time, fminsearch iterates to evaluate the function that I would like to minimize (say function "example2") it has to read another functions in the file because their outputs are necessary to compute the output of "example2".

> > Here is what I am doing:

> >

> > 1) I am setting up a 9-D grid: grid1D is a cell.

> > grid1D=[-10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10; -10:1:10;

> > -10:1:10; -10:1:10]; %% this is just an example. In my study, I will just

> > take 6 values per grid otherwise the number of evaluation will be very

> > high.

>

> Why?

I would like to start with max 10 values for each grid because 21 evaluations for each grid might be a bit too much.

>

> > 2) the function "example1" is function of 9 parameters. So the aim of

> > this exercise is to evaluate "example1" by changing the first parameter

> > (iterating accross grid1D{1}) and keeping the other parameters fixed. Once

> > this operation is done, take the parameter of grid1D{1} that gives the

> > smallest value of "example1" and plug it into the function and move to

> > other grid (grid1D{2}). Iterate accross all the values of this grid and

> > keep the parameter that minimize the function "example1"....I keep doing

> > this until "if" condition is fullfilled.

>

> So you don't actually need to evaluate on all 21^9 elements of your 9-D

> grid. Instead you'll do something like evaluating at (switching to 4D to

> save me some typing)

>

> part 1:

> [-10 -10 -10 -10]

> [-9 -10 -10 -10]

> [-8 -10 -10 -10]

> % etc

> [10 -10 -10 -10]

>

> and decide for which x the value of the function evaluated at

> [x -10 -10 -10] is minimized, then repeat with:

>

> part 2:

> % we already know [x -10 -10 -10] from part 1

> [x -9 -10 -10]

> [x -8 -10 -10]

> [x -7 -10 -10]

> % etc

> [x 10 -10 -10]

>

> part 3:

> [x y -9 -10]

> % etc

>

> Is this a correct assessment of what you're trying to do?

>

Yes thats exactly what I am trying to do.

> > Now all I am doing is evaluating the same function by changing the

> > parameters (in each grid1D). so In theory this is parallelizable no? Maybe

> > the way I wrote the code above is confusing ? (sorry about that)

>

> The evaluation may be parallelizable, but since part 2 depends on the

> results of part 1, part 1 must be completed before you can start part 2 and

> so on down the line. Thus you could parallelize this in chunks of 20 or 21

> functions evaluations being done in parallel; depending on how involved your

> function is, the benefit from parallelizing may be good or may be outweighed

> by the parallel setup cost.

>

I agree that I need to wait until the first iteration is finished before running the second one. Now if I run the first loop of the first grid in this way:

for i=1:21 %% iteration accross the value of the first grid

arguments{k}=arguments_2{k}(i)

h(i,:)=example1(arguments{:})

end

[j1, idx]=min(h)

.....

It says that the variables has a classification problems. I dont think I parallelized the loop in a good way. I was wondering if you could suggest a code that could be help parfor to run? because since I am doing it grid by grid now I probably have to delete "arguments{k}=arguments_2{k}(i)" ? Thank you very much for your comment on this.

> *snip*

>

> > Matt I also agree with you it might not be an intelligent way to minimize

> > the function but Iam just replication a paper to retrieve the parameters.

>

> How did the author of the paper retrieve those parameters -- did they do it

> in this same pseudo brute-force way?

Yes the author did it exaclty the same way (Grid Search Method).

Kind Regards

Saad

>

> --

> Steve Lord

> slord@mathworks.com

> To contact Technical Support use the Contact Us link on

> http://www.mathworks.com

You can think of your watch list as threads that you have bookmarked.

You can add tags, authors, threads, and even search results to your watch list. This way you can easily keep track of topics that you're interested in. To view your watch list, click on the "My Newsreader" link.

To add items to your watch list, click the "add to watch list" link at the bottom of any page.

To add search criteria to your watch list, search for the desired term in the search box. Click on the "Add this search to my watch list" link on the search results page.

You can also add a tag to your watch list by searching for the tag with the directive "tag:tag_name" where tag_name is the name of the tag you would like to watch.

To add an author to your watch list, go to the author's profile page and click on the "Add this author to my watch list" link at the top of the page. You can also add an author to your watch list by going to a thread that the author has posted to and clicking on the "Add this author to my watch list" link. You will be notified whenever the author makes a post.

To add a thread to your watch list, go to the thread page and click the "Add this thread to my watch list" link at the top of the page.

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Got questions?

Get answers.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreDiscover what MATLAB ^{®} can do for your career.

Opportunities for recent engineering grads.

Apply TodayThe newsgroups are a worldwide forum that is open to everyone. Newsgroups are used to discuss a huge range of topics, make announcements, and trade files.

Discussions are threaded, or grouped in a way that allows you to read a posted message and all of its replies in chronological order. This makes it easy to follow the thread of the conversation, and to see what’s already been said before you post your own reply or make a new posting.

Newsgroup content is distributed by servers hosted by various organizations on the Internet. Messages are exchanged and managed using open-standard protocols. No single entity “owns” the newsgroups.

There are thousands of newsgroups, each addressing a single topic or area of interest. The MATLAB Central Newsreader posts and displays messages in the comp.soft-sys.matlab newsgroup.

**MATLAB Central**

You can use the integrated newsreader at the MATLAB Central website to read and post messages in this newsgroup. MATLAB Central is hosted by MathWorks.

Messages posted through the MATLAB Central Newsreader are seen by everyone using the newsgroups, regardless of how they access the newsgroups. There are several advantages to using MATLAB Central.

**One Account**

Your MATLAB Central account is tied to your MathWorks Account for easy access.

**Use the Email Address of Your Choice**

The MATLAB Central Newsreader allows you to define an alternative email address as your posting address, avoiding clutter in your primary mailbox and reducing spam.

**Spam Control**

Most newsgroup spam is filtered out by the MATLAB Central Newsreader.

**Tagging**

Messages can be tagged with a relevant label by any signed-in user. Tags can be used as keywords to find particular files of interest, or as a way to categorize your bookmarked postings. You may choose to allow others to view your tags, and you can view or search others’ tags as well as those of the community at large. Tagging provides a way to see both the big trends and the smaller, more obscure ideas and applications.

**Watch lists**

Setting up watch lists allows you to be notified of updates made to postings selected by author, thread, or any search variable. Your watch list notifications can be sent by email (daily digest or immediate), displayed in My Newsreader, or sent via RSS feed.

- Use a newsreader through your school, employer, or internet service provider
- Pay for newsgroup access from a commercial provider
- Use Google Groups
- Mathforum.org provides a newsreader with access to the comp.soft sys.matlab newsgroup
- Run your own server. For typical instructions, see: http://www.slyck.com/ng.php?page=2

You can also select a location from the following list:

- Canada (English)
- United States (English)

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)