How do I use optimtool to optimize a tetris game?

Hello, I am trying to create a MATLAB adaptation of Tetris with an AI that efficiently plays the game. Currently I have a working AI, but it is lacking in terms of efficiency. I know that optimtool's fminsearch capability supposedly can help with finding correct weights, but the program that I have written, when loaded into fminsearch causes it to return the errors 'Error running optimization,' and 'Not enough input arguments.'
Any help with this endeavor would be hugely appreciated. Below is the program and the lines I put into optimtool's fminsearch.
@placementScore [-0.51,-0.36,0.16,-0.18]
function score = placementScore(agScore,holeScore,clearScore,bumpScore)
% uses agScore, holeScore, clearScore to make a composite score
score = (-.510066 * agScore) + (-.35663 * holeScore)+ (.161 * clearScore)+ (-.1845 * bumpScore);

 Accepted Answer

Like all optimization solvers, fminsearch requires ONE variable as its argument. Fortunately, that variable can be a vector.
So you just need to rewrite your function as follows:
function score = placementScore(x)
% uses agScore, holeScore, clearScore to make a composite score
agScore = x(1);
holeScore = x(2);
clearScore = x(3);
bumpScore = x(4);
score = (-.510066 * agScore) + (-.35663 * holeScore)+ (.161 * clearScore)+ (-.1845 * bumpScore);
Alan Weiss
MATLAB mathematical toolbox documentation

2 Comments

Thank you for the response! Now it works which is great. The only problem now, and likely it is something inherently wrong with the way I set up my program, is that fminsearch exits prematurely because "maximum number of function evaluations has been exceeded." The values that it returns for my weights are incredibly large, well past billions. I thought I had set up this program to return weights between 0 and 1, but obviously I did not.
Again, any help or feedback is greatly appreciated.
fminsearch does not take any constraints. Your objective function is linear in all the variables, so lacking constraints, the optimum occurs when the variables are plus or minus infinity.
You may need to use a constrained solver such as fmincon or linprog. You might want to think more about your model formulation.
It is also possible that you don't want to optimize over all the variables, that some are just parameters that the solver should treat as constants. In any case, more thought is definitely required.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (0)

Asked:

on 19 Apr 2016

Commented:

on 20 Apr 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!