问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

模拟退火算法解决路径优化 的源代码

发布网友 发布时间:2022-04-28 22:28

我来回答

1个回答

热心网友 时间:2022-06-24 04:16

我有 simulated annealing with metropolies(Monte Carlo)做的一个项目的代码,你要看看么?
void anneal(int nparam, int nstep, int nstep_per_block, double t0,

const double * param_in,

double cost_in, double * params_out, double * cost_out) {

int nblock;

int step;

int block;

int nactive;

int rank;

int n_accepted = 0;

int i, j, n;

double cost_current, cost_trial;

int * param_index;

double * param_current;

double * param_trial;

double * Q;

double * S;

double * u;

double * dp;

double * A;

FILE * fp_log_file;

char fname[FILENAME_MAX];

double temp = t0;

double tempmax = temp;

double ebar, evar, emin, eta, specific_heat;

double delta;

double chi = 0.8; // Annealing schele

double chi_s = 3.0; // Vanderbilt/Louie 'growth factor'

double rm;

double root3 = sqrt(3.0);

double p = 0.02/sqrt(3.0); //max size of annealing step

param_current = new double[nparam];

param_trial = new double[nparam];

cost_current = cost_in;

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

sprintf(fname, "a_%4.4d.log", rank);

fp_log_file = fopen(fname, "a");

if (fp_log_file == (FILE *) NULL) errorMessage("fopen(log) failed\n");

// Work out the number of active parameters, and set up the

// index table of the active parameters.

// Note that the complete array of parameters (param_trial) must

// be used to evaluate the cost function.

nactive = 0;

for (n = 0; n < nparam; n++) {

param_current[n] = param_in[n];

param_trial[n] = param_in[n];

if (P.is_active[n]) nactive++;

}

param_index = new int[nactive];

i = 0;

for (n = 0; n < nparam; n++) {

if (P.is_active[n]) param_index[i++] = n;

}

// Initialise the step distribution matrix Q_ij

Q = new double[nactive*nactive];

S = new double[nactive*nactive];

u = new double[nactive];

dp = new double[nactive];

A = new double[nactive];

double * Qtmp;

Qtmp = new double[nactive*nactive];

for (i = 0; i < nactive; i++) {

for (j = 0; j < nactive; j++) {

delta = (i == j);

Q[i*nactive + j] = p*delta*param_current[param_index[j]];

}

}

// carry out annealing points

nblock = nstep/nstep_per_block;

rm = 1.0/(double) nstep_per_block;

for (block = 0; block < nblock; block++) {

// Set the schele for this block, and initialise blockwise quantities.

// We also ensure the step distribution matrix is diagonal.

temp = chi*temp;

for (i = 0; i < nactive; i++) {

A[i] = 0.0;

for (j = 0; j < nactive; j++) {

S[i*nactive + j] = 0.0;

delta = (i == j);

Q[i*nactive + j] *= delta;

}

}

ebar = 0.0;

evar = 0.0;

emin = cost_current;

for (i = 0; i < nactive; i++) {

printf("Step: %d %g\n", i, Q[i*nactive + i]);

}

for (step = 0; step < nstep_per_block; step++) {

// Set the random vector u, and compute the step size dp

for (i = 0; i < nactive; i++) {

u[i] = root3*(r_uniform()*2.0 - 1.0);

}

for (i = 0; i < nactive; i++) {

dp[i] = 0.0;

for (j = 0; j < nactive; j++) {

dp[i] += Q[i*nactive + j]*u[j];

}

}

for (i = 0; i < nactive; i++) {

n = param_index[i];

param_trial[n] = param_current[n] + dp[i];

if (param_trial[n] < P.min[n]) param_trial[n] = P.min[n];

if (param_trial[n] > P.max[n]) param_trial[n] = P.max[n];

}

// calculate new cost function score

p_model->setParameters(param_trial);

cost_trial = p_costWild->getCost();

cost_trial += p_costLHY->getCost();

cost_trial += p_costTOC1->getCost();

cost_trial += p_costAPRR->getCost();

// Metropolis

delta = cost_trial - cost_current;

if (delta < 0.0 || r_uniform() < exp(-delta/temp)) {

for (n = 0; n < nparam; n++) {

param_current[n] = param_trial[n];

}

cost_current = cost_trial;

++n_accepted;

}

// 'Energy' statistics

ebar += cost_current;

evar += cost_current*cost_current;

if (cost_current < emin) emin = cost_current;

// Per time step log

fprintf(fp_log_file, "%6d %6d %10.4f %10.4f %10.4f %10.4f\n",

block, step, temp,

cost_current, cost_trial,

(float) n_accepted / (float) (block*nstep_per_block + step));

// Accumulate average, measured covariance

for (i = 0; i < nactive; i++) {

A[i] += param_current[param_index[i]];

for (j = 0; j < nactive; j++) {

S[i*nactive + j]

+= param_current[param_index[i]]*param_current[param_index[j]];

}

}

/* Next step*/

}

// Set the previous block average and measured covariance

for (i = 0; i < nactive; i++) {

A[i] = rm*A[i];

}

for (i = 0; i < nactive; i++) {

for (j = 0; j < nactive; j++) {

S[i*nactive + j] = rm*S[i*nactive + j] - A[i]*A[j];

if (i == j) printf("Average: %d %g %g\n", i, A[i], S[i*nactive+j]);

// Set the convarience for the next iteration s = 6 chi_s S / M

S[i*nactive + j] = 6.0*chi_s*rm*S[i*nactive + j];

}

}

// Reset the step distribution matrix for the next block

i = do_cholesky(nactive, S, Q);

j = test_cholesky(nactive, S, Q);

printf("Cholesky %d %d\n", i, j);

// Block statistics

ebar = rm*ebar;

evar = rm*evar;

specific_heat = (evar - ebar*ebar) / temp*temp;

eta = (ebar - emin)/ebar;

fprintf(fp_log_file, "%d %d %f %f %f %f %f %f\n",

block, nstep_per_block, temp, ebar, evar, emin,

specific_heat, eta);

/* Next block */

}

*cost_out = cost_current;

for (n = 0; n < nparam; n++) {

params_out[n] = param_current[n];

}

fclose(fp_log_file);

delete param_index;

delete param_current;

delete param_trial;

delete Q;

delete u;

delete dp;

delete S;

delete A;

return;

}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
离职几个月公司发短信说我离职生效叫我回去补办离职交接手续怎么 开除员工不办手续违法吗 辞退员工没有办手续违法吗 苹果13/6.1录制两个人脸,后面换锁屏密码认证还有什么提醒,或者是两个人... 很规律很威严的词语是什么 我老了又无能 想有个爱我一辈子的 到今没 就一个人 把爱放心里 用数字... 日语N2 20天复习够吗 为什么泥工先于木工 泥工做好隔多久做木工 从泥工做好到做木工,需要等待多久? 水泥操作人员作业流程是怎样的? 身上老是出油~背上长痘痘~是什么原因~怎么办? 求教:蚁群算法选择最短路径问题 小米空气净化器2手机无法连接怎么办? matlab中GA对多参数优化问题 继续蚁群算法 身上长暗疮是什么原因? matlab 多目标优化的实际例子 新手入门教程:MindManager怎么使用? 为什么身上长暗疮?? 身上多处长暗疮,怎么回事 php eta算法的扩展在哪里下载 没毕业可以回本地考教师资格证面试吗? 没毕业能考教师资格证吗? 考教师资格证有地域*吗?我的户口迁来学校了,但还没毕业,我可以在家乡报考教师资格证吗? 重庆市内以及周边有哪些地方好耍的呢? 离重庆比较近的省会有哪些 重庆市的北方,是指的哪些城市? 重庆永川西方有哪些城市 重庆附近有哪些城市 大学生留学新加坡一花多少钱 手机连不上小米空气净化器2 求助Matlab蚁群算法求一般函数极值的算法 身上长痘痘是怎么回事啊? λ演算的非形式化的描述 小米空气净化器2无法连接wifi 在机器学习中有哪些典型的Online算法 1500米远柴油发电机供电能不能实现?大家帮我看一看,最好结合实际经验,不要仅有数字理论 小米空气净化器2无法连接WIFI,求助 matlab蚁群算法路径优化 蚁群算法求解TSP问题遇到“索引超出矩阵维度。”的问题跪求大神能解答 小米空气净化器2经常离线,什么情况 身上长痘是什么原因 小米空气净化器2s怎么连接手机 xgboost的python包有多少参数 身上老是出油背上长痘痘是什么原因? 昨晚安庆上空飞机是怎么回事 华为手机怎么取消游戏免打扰?就是在游戏中收到信息有提示。 急求蚁群算法解决 VRPTW问题的matlab代码,最好是ACS或者MMAS的! 身上长痘痘是什么原因 蚁群算法求解最短路的C#代码